Cod sursa(job #3287802)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 19 martie 2025 13:36:10
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <fstream>
#include <ctime>
#include <chrono>
#include <random>
#include <vector>
#include <algorithm>
using namespace std;
ifstream cin("hashuri.in");
ofstream cout("hashuri.out");
mt19937 mt(time(nullptr));
const int nmax = 1e6 + 3;
vector <int> v[nmax];
int rnd[32], q, op, x;
int hsh(int x){
    int ans = 0;
    for(int i = 0 ; i < 32 ; ++i)
        if(x&(1<<i)) ans ^= rnd[i];
    if(ans < 0){
        int adaos = (-ans)/nmax;
        if((-ans)%nmax > 0) ++adaos;
        ans = ans + adaos*nmax;
    }
    return ans%nmax;
}
bool is(int x){
    int y = hsh(x);
    for(auto it : v[y])
        if(it == x) return true;
    return false;
}
signed main()
{
    for(int i = 0 ; i < 32 ; ++i) rnd[i] = mt();
    cin >> q;
    while(q--){
        cin >> op >> x;
        if(op == 1)
            if(!is(x)) v[hsh(x)].push_back(x);
        if(op == 2){
            int y = hsh(x);
            if(is(x)) v[y].erase(find(v[y].begin(),v[y].end(),x));
        }
        if(op == 3){
            if(is(x)) cout << 1 << '\n';
            else cout << 0 << '\n';
        }
    }
    return 0;
}