Advertisement
artemgf

НОД 2010 (ДО)

Jun 11th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #pragma once
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #define _USE_MATH_DEFINES
  4. #include <iostream>
  5. #include <string>
  6. #include <map>
  7. #include <set>
  8. #include <algorithm>
  9. #include <vector>
  10. #include <stdio.h>
  11. #include <cmath>
  12. #include <math.h>
  13. #include <queue>
  14. #include <stack>
  15. #include <climits>
  16. #include <deque>
  17. #include <ctime>
  18. #include <iomanip>
  19. #include <bitset>
  20. #include <unordered_map>
  21. #include <unordered_set>
  22.  
  23. using namespace std;
  24.  
  25. typedef long long ll;
  26. typedef unsigned long long ull;
  27. typedef unsigned int ui;
  28.  
  29. #define mh() make_heap()
  30. #define poph() pop_heap()
  31. #define pushh() push_heap()
  32. #define sor(n) n.begin(), n.end()
  33. #define rsor(n) n.rbegin(), n.rend()
  34. #define mp make_pair
  35. #define files freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout)
  36. #define p(T) pair<T,T>
  37. #define toch(x) cout.precision(x), cout.setf(ios::fixed)
  38. #define znac(l) abs(l)/l
  39. #define IOS ios::sync_with_stdio(false)
  40. #define IOSB cin.tie(0), cout.tie(0);
  41. const ll ok = ll(1e9 + 7);
  42. int t[131073*2];
  43. int n = 131072;
  44.  
  45. ll gcd(ll a, ll b) {
  46.     if (a == 0 || b == 0)
  47.     {
  48.         return max(a, b);
  49.     }
  50.     if (a == 1 || b == 1)
  51.         return min(a, b);
  52.     if (!b)
  53.         return a;
  54.     else return gcd(b, a%b);
  55. }
  56.  
  57. void add(int pos, int new_val) {
  58.     pos += n;
  59.     t[pos] = new_val;
  60.     for (pos = pos >> 1; pos; pos = pos >> 1)
  61.     {
  62.         int p = t[pos];
  63.         t[pos] = gcd(t[(pos << 1 )+ 1], t[pos << 1]);
  64.         if (p == t[pos])
  65.             break;
  66.     }
  67. }
  68.  
  69. void del(int pos) {
  70.     pos += n;
  71.     t[pos] = 0;
  72.     for (pos= pos >> 1;pos; pos = pos >> 1)
  73.     {
  74.         int p = t[pos];
  75.         t[pos] = gcd(t[(pos << 1) + 1], t[pos << 1]);
  76.         if (p == t[pos])
  77.             break;
  78.     }
  79. }
  80. int main()
  81. {
  82.     IOSB;
  83.     IOS;
  84. #ifdef TheCompiler
  85.     files;
  86. #endif
  87.     ll q;
  88.     cin >> q;
  89.     ll mxpos = 1;
  90.     unordered_map<ll, vector<ll>>pos;
  91.     for (int i = 1; i <= q; i++)
  92.     {
  93.         char a;
  94.         ll b;
  95.         cin >> a;
  96.         if (a == '+')
  97.         {
  98.             cin >> b;
  99.             pos[b].push_back(mxpos);
  100.             add(mxpos, b);
  101.             mxpos++;
  102.             if (t[1] == 0)
  103.                 cout << 1 << endl;
  104.             else
  105.                 cout << t[1] << endl;
  106.         }
  107.         else
  108.             if (a == '-')
  109.             {
  110.                 cin >> b;
  111.                 ll p= pos[b].back();
  112.                 del(p);
  113.                 pos[b].pop_back();
  114.                 if (t[1] == 0)
  115.                     cout << 1 << endl;
  116.                 else
  117.                     cout << t[1] << endl;
  118.             }
  119.     }
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement