tuki2501

So23_Bai3.cpp

Nov 1st, 2021
937
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int gcd(int a, int b) {
  5.   if (b == 0) return a;
  6.   return gcd(b, a % b);
  7. }
  8.  
  9. struct node {
  10.   int val, cnt; node *pl, *pr;
  11.   node(): val(0), cnt(0), pl(nullptr), pr(nullptr) {}
  12. };
  13.  
  14. struct PSeg {
  15.   int n; node *root;
  16.   PSeg(int n): n(n) {
  17.     root = new node;
  18.   }
  19.   void update(node *&cur, int l, int r, int val, int cnt) {
  20.     if (cur == nullptr) {
  21.       cur = new node;
  22.     }
  23.     if (r < val || l > val) {
  24.       return;
  25.     }
  26.     if (l == r) {
  27.       cur->cnt += cnt;
  28.       cur->val = (cur->cnt ? val : 0);
  29.       return;
  30.     }
  31.     int m = (l + r) / 2;
  32.     update(cur->pl, l, m, val, cnt);
  33.     update(cur->pr, m + 1, r, val, cnt);
  34.     cur->val = gcd(cur->pl->val, cur->pr->val);
  35.   }
  36.   void update(int val, int cnt) {
  37.     update(root, 1, n, val, cnt);
  38.   }
  39. };
  40.  
  41. int main() {
  42.   cin.tie(0)->sync_with_stdio(0);
  43.   PSeg ps(1000000000);
  44.   int q; cin >> q;
  45.   while (q--) {
  46.     int t, x;
  47.     cin >> t >> x;
  48.     ps.update(x, (t == 1 ? 1 : -1));
  49.     int res = ps.root->val;
  50.     cout << (res ? res : 1) << '\n';
  51.   }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment