Cod sursa(job #1164593)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 2 aprilie 2014 10:26:27
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.17 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>

using namespace std;

const char infile[] = "hashuri.in";
const char outfile[] = "hashuri.out";

ofstream fout(outfile);

const int MOD = 666013;
const int oo = 0x3f3f3f3f;

typedef vector<int> Graph[MOD + 5];
typedef vector<int> :: iterator It;

const inline int min(const int &a, const int &b) { if( a > b ) return b;   return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b;   return a; }
const inline void Get_min(int &a, const int b)    { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b)    { if( a < b ) a = b; }

Graph Hash;
int M;

inline bool Find(int x) {
    int key = x % MOD;
    for(It it = Hash[key].begin(), fin = Hash[key].end(); it != fin ; ++ it)\
        if(*it == x)
            return 1;
    return 0;
}

inline void Insert(int x) {
    int key = x % MOD;
    Hash[key].push_back(x);
}

inline void Remove(int x) {
    int key = x % MOD;
    for(It it = Hash[key].begin(), fin = Hash[key].end(); it != fin ; ++ it)
        if(*it == x)
            Hash[key].erase(it);
}

const int lim = (1 << 20);
char buff[lim];
int pos;

inline void get(int &x) {
    x = 0;
    while(!('0' <= buff[pos] && buff[pos] <= '9'))
        if(++ pos == lim) {
            fread(buff, 1, lim, stdin);
            pos = 0;
        }
    while('0' <= buff[pos] && buff[pos] <= '9') {
        x = x * 10 + buff[pos] - '0';
        if(++ pos == lim) {
            fread(buff, 1, lim, stdin);
            pos = 0;
        }
    }
}

int main() {
    freopen(infile, "r", stdin);
    get(M);
    for(int i = 1 ; i <= M ; ++ i) {
        int op, x;
        get(op); get(x);
        switch(op) {
            case 1:
            if(!Find(x))
                Insert(x);
            break;
            case 2:
            if(Find(x))
                Remove(x);
            break;
            case 3:
            fout << Find(x) << '\n';
            break;
        }
    }
    fout.close();
    return 0;
}