Cod sursa(job #2506612)

Utilizator euyoTukanul euyo Data 8 decembrie 2019 15:33:19
Problema Hashuri Scor 30
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <stdio.h>
#define P 666013
#define MAXN 1000000
#define NIL 0

int in[P], next[MAXN];

int main() {
  FILE *fin = fopen( "hashuri.in", "r" );
  FILE *fout = fopen( "hashuri.out", "w" );
  int n, i, op, x, nr, ant;

  fscanf( fin, "%d", &n );
  for ( i = 0; i < n; ++i ) {
    fscanf( fin, "%d%d", &op, &x );
    switch( op ) {
    case 1:
      nr = in[x % P];
      while ( nr != NIL && nr != x ) {
        nr = next[nr];
      }
      if ( nr == NIL ) {
        next[x] = in[x % P];
        in[x % P] = x;
      }
      break;
    case 2:
      nr = in[x % P];
      ant = NIL;
      while ( nr != NIL && nr != x ) {
        ant = nr;
        nr = next[nr];
      }
      if ( nr == x && ant != NIL ) {
        next[ant] = next[next[ant]];
      } else if ( ant == NIL ) {
        in[x % P] = next[nr];
        next[in[x % P]] = next[next[nr]];
      }
      break;
    case 3:
      nr = in[x % P];
      while ( nr != NIL && nr != x ) {
        nr = next[nr];
      }
      if ( nr == NIL ) {
        fprintf( fout, "0\n" );
      } else {
        fprintf( fout, "1\n" );
      }
      break;
    }
  }
  fclose( fin );
  fclose( fout );
  return 0;
}