Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma comment(linker, "/stack:200000000")
- #pragma GCC optimize("Ofast")
- #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
- #include <bits/stdc++.h>
- #include <cmath>
- using namespace std;
- const int maxen = 106033;
- const int maxen2 = 60493;
- const int inf = -2e9;
- const int del = 2e9;
- int hashf(int x) {
- return x % maxen;
- }
- bool exists (vector <int> &headPos,vector <int> &headNeg, int x) {
- if (x >= 0) {
- int pos = hashf(x);
- bool check = false;
- while (true) {
- if (headPos[pos] == inf) {
- break;
- }
- if (headPos[pos] == x) {
- check = true;
- break;
- }
- pos = (pos + 1) % maxen;
- }
- return check;
- } else {
- x = abs(x);
- int pos = hashf(x);
- bool check = false;
- while (true) {
- if (headNeg[pos] == inf) {
- break;
- }
- if (headNeg[pos] == x) {
- check = true;
- break;
- }
- pos = (pos + 1) % maxen;
- }
- return check;
- }
- }
- void add (vector <int> &headPos,vector <int> &headNeg, int x) {
- if (x >= 0) {
- if (exists(headPos, headNeg, x) == false) {
- int pos = hashf(x);
- while (true) {
- if (headPos[pos] == inf) {
- headPos[pos] = x;
- break;
- } else {
- pos = (pos + 1) % maxen;
- }
- }
- }
- } else {
- if (exists(headPos, headNeg, x) == false) {
- x = abs(x);
- int pos = hashf(x);
- while (true) {
- if (headNeg[pos] == inf) {
- headNeg[pos] = x;
- break;
- } else {
- pos = (pos + 1) % maxen;
- }
- }
- }
- }
- }
- void deletef(vector <int> &headPos,vector <int> &headNeg, int x) {
- if (x >= 0) {
- if (exists(headPos, headNeg, x) == true) {
- int pos = hashf(x);
- while (true) {
- if (headPos[pos] == x) {
- headPos[pos] = del;
- break;
- } else {
- pos = (pos + 1) % maxen;
- }
- }
- }
- } else {
- if (exists(headPos, headNeg, x) == true) {
- x = abs(x);
- int pos = hashf(x);
- while (true) {
- if (headNeg[pos] == x) {
- headNeg[pos] = del;
- break;
- } else {
- pos = (pos + 1) % maxen;
- }
- }
- }
- }
- }
- int main()
- {
- freopen("set.in", "r", stdin);
- freopen("set.out", "w", stdout);
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- vector <int> headPos(maxen, inf);
- vector <int> headNeg(maxen, inf);
- string str;
- while (cin >> str) {
- if (str == "insert") {
- int x;
- cin >> x;
- add(headPos,headNeg, x);
- }
- if (str == "delete") {
- int x;
- cin >> x;
- deletef(headPos,headNeg, x);
- }
- if (str == "exists") {
- int x;
- cin >> x;
- if (exists(headPos,headNeg, x)) {
- cout << "true\n";
- } else {
- cout << "false\n";
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement