Cod sursa(job #2835746)

Utilizator NToniBoSSNicolae Tonitza NToniBoSS Data 19 ianuarie 2022 10:33:23
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.84 kb
#include <bits/stdc++.h>
#define LIM 1<<17
#define NMAX 1000000
#define MOD 666013
/// TONI BO$$ was here
/// #MLC

using namespace std;
char BUF[LIM];
int poz;

inline char getChar(){
    poz++;
    if(poz>=LIM){
    	fread(BUF,LIM,1,stdin);
    	poz=0;
    }
    return BUF[poz];
}

inline int getNr(){
    int r=0, semn=1;
    char ch=getChar();
    while(isdigit(ch)==0 && ch!='-') ch=getChar();
    if(ch=='-'){
        semn=-1;
        ch=getChar();
    }
    while(isdigit(ch)!=0){
        r=r*10+semn*(ch-'0');
        ch=getChar();
    }
    return r;
}

int v[NMAX + 1], r[MOD], urm[NMAX + 1];


int main()
{
    int n, m, i, q, x, cursor;
    freopen("hashuri.in","r",stdin);
    freopen("hashuri.out","w",stdout);
    n = getNr();
    m = 0;
    for(i = 1; i <= n; i++)
    {
        q = getNr();
        x = getNr();
        switch(q)
        {
            case 1: m++;
                    urm[m] = r[x % MOD];
                    r[x % MOD] = m;
                    v[m] = x;
                    break;
            case 2: cursor = r[x % MOD];
                    if(v[cursor] == x)
                    {
                        r[x % MOD] = urm[cursor];
                        break;
                    }
                    while(urm[cursor] != 0 && v[urm[cursor]] != x)
                        cursor = urm[cursor];
                    if(urm[cursor] != 0)
                        urm[cursor] = urm[urm[cursor]];
                    break;
            case 3: cursor = r[x % MOD];
                    while(cursor != 0 && v[cursor] != x)
                        cursor = urm[cursor];
                    if(cursor != 0)
                        printf("1\n");
                    else
                        printf("0\n");
                    break;
        }
    }

    return 0;
}