Cod sursa(job #1505082)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 18 octombrie 2015 18:43:17
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.16 kb
#include <bits/stdc++.h>

const int Nmax = 1000000;
const int MOD = 666013;
const int NIL = -1;
const int BUFFSIZE = 1048576;
const int MAX_V = 300000000;

struct h {
  int v, next;
};

h l[Nmax];
int lsize;
int h[MOD];
int st[Nmax], s;
char buff[BUFFSIZE];
int ptr = BUFFSIZE;

__attribute__((aligned(16))) unsigned long long S[(MAX_V >> 6) + 1];

inline bool hashFind(const int &x) {
  int i = h[x - (x / MOD) * MOD];

  while ((i != NIL) && (l[i].v != x))
    i = l[i].next;
  return (i != NIL);
}

inline void hashInsert(const int &x) {
  int p = s ? st[--s] : lsize++;
  int hash = x - (x / MOD) * MOD;

  l[p].v = x;
  l[p].next = h[hash];
  h[hash] = p;
}

inline void hashExtract(const int &x) {
  int hash = x - (x / MOD) * MOD;
  int i = h[hash];

  if (l[i].v == x)
    h[hash] = l[i].next;
  else {
    while ((l[i].next != NIL) && (l[l[i].next].v != x))
      i = l[i].next;
    if (l[i].next != NIL) {
      st[s++] = l[i].next;
      l[i].next = l[l[i].next].next;
    }
  }
}

inline char getChr() {
  if (ptr == BUFFSIZE) {
    fread(buff, 1, BUFFSIZE, stdin);
    ptr = 0;
  }
  return buff[ ptr++ ];
}

inline int readInt() {
  register int q = 0;
  char c;

  do {
    c = getChr();
  } while (!isdigit(c));
  do {
    q = (q << 1) + (q << 3) + (c - '0');
    c = getChr();
  } while (isdigit(c));
  return q;
}

int main() {
  freopen("hashuri.in", "r", stdin);
  freopen("hashuri.out", "w", stdout);
  int testNum;
  char opType;
  int x;

  memset(h, NIL, sizeof(h));

  testNum = readInt();
  for (; testNum; testNum--) {
    do {
      opType = getChr();
    } while (!isdigit(opType));
    x = readInt();
    if (opType == '1') {
      if ( x >= MAX_V ) {
        hashInsert(x);
      } else {
        S[x >> 6] |= (1ULL << (x & 63));
      }
    }
    else if (opType == '2') {
      if ( x >= MAX_V ) {
        hashExtract(x);
      } else {
        S[x >> 6] &= ~(1ULL << (x & 63));
      }
    }
    else {
      putchar(((x >= MAX_V) ? hashFind(x) : ((S[x >> 6] >> (x & 63)) & 1)) + '0');
      putchar('\n');
    }
  }
  fclose(stdin);
  fclose(stdout);
  return 0;
}