Cod sursa(job #804076)

Utilizator MKLOLDragos Ristache MKLOL Data 28 octombrie 2012 20:03:12
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#include<stdio.h>
#include<vector>
#define MOD 321991
#define MOD2 323879
using namespace std;


vector<int> g[MOD],p[MOD2];

int hash(int x)
{
    return x%MOD;
}
int hash2(int x)
{
    return x%MOD2;
}
int find(int x)
{
int y=hash(x);
for(int i=0;i<g[y].size();++i)
    if(g[y][i]==x)
        return 1;
y=hash2(x);
for(int i=0;i<p[y].size();++i)
    if(p[y][i]==x)
        return 1;

return 0;
}
int add(int x)
{
if(g[hash(x)].size() <p[hash2(x)].size())
g[hash(x)].push_back(x);
else p[hash2(x)].push_back(x);
}
int del(int x)
{
int y=hash(x);
    for(int i=0;i<g[y].size();++i)
        if(g[y][i]==x)
            g[y].erase(g[y].begin() + i);
y=hash2(x);
    for(int i=0;i<p[y].size();++i)
        if(p[y][i]==x)
            p[y].erase(p[y].begin() + i);
    return 1;
}

int main()
{
freopen("hashuri.in","r",stdin);
freopen("hashuri.out","w",stdout);
int N,x,y;
scanf("%d",&N);
for(int i=1;i<=N;++i)
{
    scanf("%d%d",&x,&y);
    if(x==1)
    {
    if(!find(y))
        add(y);
    }
    if(x==3)
        printf("%d\n",find(y));
    if(x==2)
        if(find(y))
            del(y);
}

return 0;

}