Cod sursa(job #1505066)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 18 octombrie 2015 18:29:37
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.92 kb
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

#define BUFFSIZE 65536
#define NIL -1
#define MAX_N 1000000

char buffer[BUFFSIZE];
int ptr = BUFFSIZE;

inline char GetChar(FILE *fin) {
  if ( ptr == BUFFSIZE ) {
    fread( buffer, 1, BUFFSIZE, fin );
    ptr = 0;
  }
  return buffer[ptr++];
}

inline int ReadInt(FILE *fin) {
  int q = 0;
  char c;

  do {
    c = GetChar(fin);
  } while ( !isdigit(c) );
  do {
    q = (10 * q) + (c - '0');
    c = GetChar(fin);
  } while ( isdigit(c) );
  return q;
}

typedef struct {
  int key, priority;
  int left, right;
} Treap;

Treap l[MAX_N];
int st[MAX_N], ss;
int pos;

int alloc(int key, int priority, int left, int right) {
  int p = ss ? st[--ss] : pos++;
  l[p].key = key;
  l[p].priority = priority;
  l[p].left = left;
  l[p].right = right;
  return p;
}

bool TreapSearch(int n, int key) {
  if ( n == NIL ) {
    return 0;
  }
  if ( key == l[n].key ) {
    return 1;
  }
  if ( key < l[n].key ) {
    return TreapSearch( l[n].left, key );
  } else {
    return TreapSearch( l[n].right, key );
  }
}

void rotLeft(int &n) {
  int T = l[n].left;
  l[n].left = l[T].right;
  l[T].right = n;
  n = T;
}

void rotRight(int &n) {
  int T = l[n].right;
  l[n].right = l[T].left;
  l[T].left = n;
  n = T;
}

void TreapBalance(int &n) {
  if ( l[n].left != NIL && l[l[n].left].priority > l[n].priority ) {
    rotLeft( n );
  } else if ( l[n].right != NIL && l[l[n].right].priority > l[n].priority ) {
    rotRight( n );
  }
}

void TreapInsert(int &n, int key, int priority) {
  if ( n == NIL ) {
    n = alloc(key, priority, NIL, NIL);
  } else {
    if ( key < l[n].key ) {
      TreapInsert( l[n].left, key, priority );
    } else if ( key > l[n].key ) {
      TreapInsert( l[n].right, key, priority );
    }
    TreapBalance( n );
  }
}

void TreapErase(int &n, int key) {
  if ( n == NIL ) {
    return;
  }
  if ( key < l[n].key ) {
    TreapErase( l[n].left, key );
  } else if ( key > l[n].key ) {
    TreapErase( l[n].right, key );
  } else {
    if ( l[n].left == NIL && l[n].right == NIL ) {
      st[ss++] = n;
      n = NIL;
    } else {
      if ( l[l[n].left].priority > l[l[n].right].priority ) {
        rotLeft(n);
      } else {
        rotRight(n);
      }
      TreapErase(n, key);
    }
  }
}

int main(void) {
  FILE *fin = fopen("hashuri.in", "r");
  FILE *fout = fopen("hashuri.out", "w");
  int n;
  int opType, x;
  int R;

  srand(time(0));

  R = alloc(0, 0, NIL, NIL);

  n = ReadInt(fin);
  while ( n-- ) {
    opType = ReadInt(fin);
    x = ReadInt(fin);
    if ( opType == 1 ) {
      TreapInsert( R, x, rand() + 1 );
    } else if ( opType == 2 ) {
      TreapErase( R, x );
    } else {
      fputc( '0' + TreapSearch( R, x ), fout );
      fputc( '\n', fout );
    }
  }
  fclose( fin );
  fclose( fout );
  return 0;
}