Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.57 KB | None | 0 0
  1. #pragma comment(linker, "/stack:200000000")
  2. #pragma GCC optimize("Ofast")
  3. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  4.  
  5. #include <bits/stdc++.h>
  6. #include <cmath>
  7. using namespace std;
  8. const int maxen = 106033;
  9. const int maxen2 = 60493;
  10. const int inf = -2e9;
  11. const int del = 2e9;
  12.  
  13.  
  14. int hashf(int x) {
  15.    
  16.     return x % maxen;
  17. }
  18.  
  19. bool exists (vector <int> &headPos,vector <int> &headNeg, int x) {
  20.  
  21.     if (x >= 0) {
  22.         int pos = hashf(x);
  23.         bool check = false;
  24.         while (true) {
  25.             if (headPos[pos] == inf) {
  26.                 break;
  27.             }
  28.             if (headPos[pos] == x) {
  29.                 check = true;
  30.                 break;
  31.             }
  32.             pos = (pos + 1) % maxen;
  33.         }
  34.         return check;
  35.     } else {
  36.         x = abs(x);
  37.         int pos = hashf(x);
  38.         bool check = false;
  39.         while (true) {
  40.             if (headNeg[pos] == inf) {
  41.                 break;
  42.             }
  43.             if (headNeg[pos] == x) {
  44.                 check = true;
  45.                 break;
  46.             }
  47.             pos = (pos + 1) % maxen;
  48.         }
  49.         return check;
  50.     }
  51.  
  52. }
  53. void add (vector <int> &headPos,vector <int> &headNeg, int x) {
  54.  
  55.     if (x >= 0) {
  56.         if (exists(headPos, headNeg, x) == false) {
  57.             int pos = hashf(x);
  58.             while (true) {
  59.                 if (headPos[pos] == inf) {
  60.                     headPos[pos] = x;
  61.                     break;
  62.                 } else {
  63.                     pos = (pos + 1) % maxen;
  64.                 }
  65.             }
  66.  
  67.         }
  68.     } else {
  69.  
  70.         if (exists(headPos, headNeg, x) == false) {
  71.             x = abs(x);
  72.             int pos = hashf(x);
  73.             while (true) {
  74.                 if (headNeg[pos] == inf) {
  75.                     headNeg[pos] = x;
  76.                     break;
  77.                 } else {
  78.                     pos = (pos + 1) % maxen;
  79.                 }
  80.             }
  81.  
  82.         }
  83.     }
  84.  
  85.  
  86.  
  87.  
  88. }
  89. void deletef(vector <int> &headPos,vector <int> &headNeg, int x) {
  90.  
  91.     if (x >= 0) {
  92.         if (exists(headPos, headNeg, x) == true) {
  93.             int pos = hashf(x);
  94.             while (true) {
  95.                 if (headPos[pos] == x) {
  96.                     headPos[pos] = del;
  97.                     break;
  98.                 } else {
  99.                     pos = (pos + 1) % maxen;
  100.                 }
  101.             }
  102.         }
  103.     } else {
  104.         if (exists(headPos, headNeg, x) == true) {
  105.             x = abs(x);
  106.             int pos = hashf(x);
  107.             while (true) {
  108.                 if (headNeg[pos] == x) {
  109.                     headNeg[pos] = del;
  110.                     break;
  111.                 } else {
  112.                     pos = (pos + 1) % maxen;
  113.                 }
  114.             }
  115.         }
  116.     }
  117.  
  118. }
  119. int main()
  120. {
  121.  
  122.     freopen("set.in", "r", stdin);
  123.     freopen("set.out", "w", stdout);
  124.     ios_base::sync_with_stdio(0);
  125.     cin.tie(0);
  126.     cout.tie(0);
  127.    
  128.     vector <int> headPos(maxen, inf);
  129.     vector <int> headNeg(maxen, inf);
  130.     string str;
  131.  
  132.     while (cin >> str) {
  133.         if (str == "insert") {
  134.             int x;
  135.             cin >> x;
  136.             add(headPos,headNeg, x);
  137.         }
  138.         if (str == "delete") {
  139.             int x;
  140.             cin >> x;
  141.             deletef(headPos,headNeg, x);
  142.         }
  143.         if (str == "exists") {
  144.             int x;
  145.             cin >> x;
  146.             if (exists(headPos,headNeg, x)) {
  147.                 cout << "true\n";
  148.             } else {
  149.                 cout << "false\n";
  150.             }
  151.  
  152.         }
  153.     }
  154.  
  155.  
  156.  
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement