Cod sursa(job #1864092)

Utilizator TincaMateiTinca Matei TincaMatei Data 31 ianuarie 2017 15:08:44
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.87 kb
#include <cstdio>
#include <ctype.h>

class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
}fin("hashuri.in");

const int MAX_HASH = 2000000;
const int MAX_N = 1000000;

int start[MAX_HASH];
int last[MAX_HASH];
int next[1+MAX_N];
int val[1+MAX_N];

inline int hash(int x) {
  return x % MAX_HASH;
}

inline void add(int x, int poz) {
  int p, i;
  p = hash(x);
  i = start[p];
  while(i != 0 && val[i] != x)
    i = next[i];
  if(i == 0 && start[p] == 0)
    start[p] = last[p] = poz;
  else if(i == 0)
    last[p] = next[last[p]] = poz;
}

inline void del(int x) {
  int p, i;
  p = hash(x);
  if(val[start[p]] == x)
    start[p] = next[start[p]];
  else {
    i = start[p];
    while(next[i] != 0 && val[next[i]] != x)
      i = next[i];
    if(next[i] != 0)
      next[i] = next[next[i]];
  }
}

inline int query(int x) {
  int i, p;
  p = hash(x);
  i = start[p];
  while(i != 0 && val[i] != x)
    i = next[i];
  return i != 0;
}

int main() {
  int n, op, i;
  fin >> n;
  FILE *fout = fopen("hashuri.out", "w");
  for(i = 1; i <= n; i++) {
    fin >> op >> val[i];
    if(op == 1)
      add(val[i], i);
    else if(op == 2)
      del(val[i]);
    else
      fprintf(fout, "%d\n", query(val[i]));
  }
  fclose(fout);
}