Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.66 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3.  
  4. using namespace std;
  5. typedef long long ll;
  6.  
  7. const ll pot = 1<<17;
  8.  
  9. int n, q;
  10.  
  11. vector<int> tree[(pot<<1)];
  12. vector<int> up_date[(pot<<1)];
  13.  
  14. inline ll bit_to_int(vector<int> wiki)
  15. {
  16.     ll wynik = 0;
  17.     ll dodawacz = 1;
  18.  
  19.     for(ll e : wiki)
  20.     {
  21.         if(e)
  22.             wynik += dodawacz;
  23.         dodawacz<<=1;
  24.     }
  25.     return wynik;
  26. }
  27.  
  28. inline vector<int> int_to_bit(ll li)
  29. {
  30.     vector<int> ans;
  31.     while(li)
  32.     {
  33.         ans.push_back(li&1);
  34.         li>>=1;
  35.     }
  36.     return ans;
  37. }
  38.  
  39. inline vector<int> add(int i, int j)
  40. {
  41.     while(tree[i].size() < tree[j].size())
  42.         tree[i].push_back(0);
  43.     while(tree[j].size() < tree[i].size())
  44.         tree[j].push_back(0);
  45.  
  46.     vector<int> ans;
  47.     for(int x = 0; x < tree[i].size(); x++)
  48.         ans.push_back(tree[i][x] + tree[j][x]);
  49.     return ans;
  50. }
  51.  
  52. inline vector<int> heal(vector<int> wiki)
  53. {
  54.     vector<int> ans;
  55.     int pom[100];
  56.     for(int i = 0; i < 100; i++)
  57.         pom[i] = 0;
  58.  
  59.     for(int i = 0; i < wiki.size(); i++)
  60.         pom[i] = wiki[i];
  61.    
  62.     for(int i = 0; i < 100; i++)
  63.     {
  64.         ans.push_back(pom[i]&1);
  65.         pom[i+1] += (pom[i]>>1);
  66.     }
  67.     return ans;
  68. }
  69.  
  70. inline vector<int> xorens(vector<int> one, vector<int> two_x, ll ile)
  71. {
  72.     vector<int> ans;
  73.     while(one.size() < two_x.size())
  74.         one.push_back(0);
  75.     while(two_x.size() < one.size())
  76.         two_x.push_back(0);
  77.  
  78.     for(int i = 0; i < one.size(); i++)
  79.     {
  80.         if(!two_x[i])
  81.             ans.push_back(one[i]);
  82.         if(two_x[i])
  83.             ans.push_back(ile-one[i]);
  84.     }
  85.     return ans;
  86. }
  87.  
  88. void update(int a, int b, int lo, int hi, int e, int val)
  89. {
  90.     if(a == lo and b == hi)
  91.     {
  92.         tree[e] = xorens(tree[e], int_to_bit(val), hi-lo);
  93.         up_date[e].push_back(val);
  94.         return;
  95.     }
  96.     if(b <= a) return;
  97.  
  98.     int mid = (hi+lo)/2;
  99.  
  100.     if(up_date[e].size())
  101.     {
  102.         for(auto up : up_date[e])
  103.         {
  104.             tree[e<<1] = xorens(tree[e<<1], int_to_bit(up), (hi-lo)/2);
  105.             up_date[e<<1].push_back(up);
  106.             tree[(e<<1)+1] = xorens(tree[(e<<1)+1], int_to_bit(up), (hi-lo)/2);
  107.             up_date[(e<<1)+1].push_back(up);
  108.         }
  109.         up_date[e].clear();
  110.     }
  111.     update(a, min(mid, b), lo, mid, e<<1, val);
  112.     update(max(a, mid), b, mid, hi, (e<<1)+1, val);
  113.     tree[e] = add(e<<1, (e<<1)+1);
  114. }
  115.  
  116. ll query(int a, int b, int lo, int hi, int e)
  117. {
  118.     if(a == lo and b == hi)
  119.         return bit_to_int(heal(tree[e]));
  120.     if(b <= a) return 0;
  121.  
  122.     int mid = (lo+hi)/2;
  123.  
  124.     if(up_date[e].size())
  125.     {
  126.         for(auto up : up_date[e])
  127.         {
  128.             tree[e<<1] = xorens(tree[e<<1], int_to_bit(up), (hi-lo)/2);
  129.             up_date[e<<1].push_back(up);
  130.             tree[(e<<1)+1] = xorens(tree[(e<<1)+1], int_to_bit(up), (hi-lo)/2);
  131.             up_date[(e<<1)+1].push_back(up);
  132.         }
  133.         up_date[e].clear();
  134.     }
  135.  
  136.     ll l = query(a, min(b, mid), lo, mid, e<<1);
  137.     ll r = query(max(a, mid), b, mid, hi, (e<<1)+1);
  138.     return l+r;
  139. }
  140.  
  141. inline void init()
  142. {
  143.     cin >> n;
  144.     for(int i = 1; i <= n; i++)
  145.     {
  146.         ll x;
  147.         cin >> x;
  148.         tree[i+pot] = int_to_bit(x);
  149.     }
  150.     for(int i = pot-1; i; i--)
  151.         tree[i] = add(i<<1, (i<<1)+1);
  152.     cin >> q;
  153. }
  154.  
  155. int main()
  156. {
  157.     ios_base::sync_with_stdio(false);
  158.     init();
  159.     while(q--)
  160.     {
  161.         int co, a, b, w;
  162.         cin >> co >> a >> b;
  163.         if(co == 1)
  164.             cout << query(a , b+1, 0, pot, 1) << "\n";
  165.         if(co == 2)
  166.         {
  167.             cin >> w;
  168.             update(a, b+1, 0, pot, 1, w);
  169.         }
  170.     }
  171.     return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement