Dang_Quan_10_Tin

Trie

Jul 28th, 2020
779
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #define task ""
  4. using namespace std;
  5. using ll = long long;
  6. using ld = long double;
  7.  
  8. const int N = 2e5 + 2;
  9. struct Trie{
  10.     Trie *child[2];
  11.     int st = 0;
  12.     Trie() {child[0] = child[1] = NULL;}
  13.     void Add(int id, int lim, ll &x, int val){
  14.         if(id == lim){
  15.             st += val;
  16.             return;
  17.         }
  18.         if(!child[(x >> id) & 1])
  19.             child[(x >> id) & 1] = new Trie;
  20.         child[(x >> id) & 1] -> Add(id - 1, lim, x, val);
  21.         if(child[(x >> id) & 1] -> st == 0){
  22.             delete child[(x >> id) & 1];
  23.             child[(x >> id) & 1] = NULL;
  24.         }
  25.         st += val;
  26.     }
  27.     int lower(int id, int lim, ll &x){
  28.         if(id == lim)
  29.             return st;
  30.         int ans = 0;
  31.         if((x >> id) & 1){
  32.             if(child[(x >> id) & 1])
  33.                 ans += child[(x >> id) & 1] -> lower(id - 1, lim, x);
  34.         }
  35.         else{
  36.             if(child[(x >> id) & 1])
  37.                 ans += child[(x >> id) & 1] -> lower(id - 1, lim, x);
  38.             if(child[!((x >> id) & 1)])
  39.                 ans += child[!((x >> id) & 1)] -> st;
  40.         }
  41.         return ans;
  42.     }
  43.     int cnt(int id, int lim, ll &x){
  44.         if(id == lim)
  45.             return st;
  46.         if(child[(x >> id) & 1])
  47.             return child[(x >> id) & 1] -> cnt(id - 1, lim, x);
  48.         return 0;
  49.     }
  50. };
  51.  
  52. const ll mod = 1e9 + 1;
  53. int n, q;
  54. ll a[N];
  55. ll lst(0);
  56. Trie beg;
  57.  
  58. void Read(){
  59.     cin >> n;
  60.     for(int i = 0; i < n; ++i)
  61.         cin >> a[i],
  62.         beg.Add(60, -1, a[i], 1);
  63. }
  64.  
  65. void Solve(){
  66.     cin >> q;
  67.     while(q--){
  68.         //cerr << q << "\n";
  69.         int type;
  70.         cin >> type;
  71.         if(type == 1){
  72.             int x; ll y;
  73.             cin >> x >> y;
  74.             x = (x + lst) % n;
  75.             ll t = (y + (ll)lst) % mod;
  76.             beg.Add(60, -1, a[x], -1);
  77.             beg.Add(60, -1, t, 1);
  78.             a[x] = t;
  79.         }
  80.         else{
  81.             ll x, y;
  82.             cin >> x >> y;
  83.             int u = beg.lower(60, -1, x),
  84.                 v = beg.lower(60, -1, y),
  85.                 t = beg.cnt(60, -1, y);
  86.             //cout << u << " " << v << " " << t << "\n";
  87.             lst = u - v + t;
  88.             cout << lst << "\n";
  89.         }
  90.     }
  91. }
  92.  
  93. main(){
  94.     ios::sync_with_stdio(0);
  95.     cin.tie(0);
  96.     cout.tie(0); if(fopen(task".INP", "r"))
  97.     freopen(task".INP", "r", stdin),
  98.     freopen(task".OUT", "w", stdout);
  99.     Read();
  100.     Solve();
  101. }
Advertisement
Add Comment
Please, Sign In to add comment