Cod sursa(job #2410234)

Utilizator LucaSeriSeritan Luca LucaSeri Data 19 aprilie 2019 20:28:15
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.8 kb
#include <bits/stdc++.h>
 
using namespace std;

const int mod = 666013;

namespace Treap {
	struct treapNode {
		treapNode *l = 0, *r = 0;
		int x = 0;
		int y = 0;
		treapNode(int _x = 0) : x(_x), y(rand()) {}
	} *root[mod];

 	treapNode *join(treapNode *l, treapNode *r) {
 		if(!l) return r;
 		if(!r) return l;

 		if(l -> y > r -> y) {
 			l -> r = join(l -> r, r);
 			return l;
 		} else {
 			r -> l = join(l, r -> l);
 			return r;
 		}
 	} 

 	pair< treapNode*, treapNode* > split(treapNode *t, int k) {
 		 if(!t) return {};
 		 if(t->x <= k) {
 		 	auto pa = split(t->r, k);
 		 	t->r = pa.first;
 		 	return {t, pa.second};
 		 } else {
 		 	auto pa = split(t->l, k);
 		 	t->l = pa.second;
 		 	return {pa.first, t};
 		 }
 	}

 	treapNode * insert(treapNode *t, int k) {
 		auto pa = split(t, k);
 		return join(pa.first, join(new treapNode(k), pa.second));
 	}

 	treapNode * erase(treapNode *t, int k) {
 		auto pa = split(t, k-1);
 		auto u = split(pa.second, k);
 		return join(pa.first, u.second);
 	}

 	bool find(treapNode *t, int k) {
 		if(!t) return false;
 		if(t->x == k) return true;
 		if(t->x > k) return find(t->l, k);
 		return find(t->r, k);
 	}
}

int main() {
	#ifdef BLAT
		freopen("input", "r", stdin);
	#else
		freopen("hashuri.in", "r", stdin);
		freopen("hashuri.out", "w", stdout);
	#endif
 
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	srand(time(nullptr));

	int n;
	cin >> n;

	for(int i = 0; i < n; ++i) {
		int t, x;
		cin >> t >> x;

		int where = x % mod;
		if(t == 1) {
			if(!Treap::find(Treap::root[where], x)) Treap::root[where] = Treap::insert(Treap::root[where], x); 
		}
		if(t == 2) {
			if(Treap::find(Treap::root[where], x)) Treap::root[where] = Treap::erase(Treap::root[where], x);
		}
		if(t == 3) {
			cout << Treap::find(Treap::root[where], x) << '\n';
		}
	}
	return 0;
}