Advertisement
willy108

pst but the bad version

May 21st, 2022
933
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.34 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. //misaka will carry me to master
  6. #include <iostream>
  7. #include <cstdio>
  8. #include <cstring>
  9. #include <cmath>
  10. #include <utility>
  11. #include <cassert>
  12. #include <algorithm>
  13. #include <vector>
  14. #include <functional>
  15. #include <numeric>
  16. #include <set>
  17. #include <map>
  18.  
  19. #define ll long long
  20. #define lb long double
  21. #define sz(vec) ((int)(vec.size()))
  22. #define all(x) x.begin(), x.end()
  23. #define pb push_back
  24. #define mp make_pair
  25. #define kill(x, s) {int COND = x; if(COND){ cout << s << "\n"; return ; }}
  26.  
  27. const lb eps = 1e-9;
  28. //const ll mod = 1e18, ll_max = 1e18;
  29. const ll mod = (1 << (23)) * 119 +1, ll_max = 1e18;
  30. const int MX = 'HUG', int_max = 0x3f3f3f3f;
  31.  
  32. struct {
  33.   template<class T>
  34.   operator T() {
  35.     T x; std::cin >> x; return x;
  36.   }
  37. } in;
  38.  
  39. using namespace std;
  40.  
  41.  
  42. unsigned long nrng(void) {
  43.     static unsigned long x=123456789, y=362436069, z=521288629;
  44.  
  45.     unsigned long t;
  46.     x ^= x << 16;
  47.     x ^= x >> 5;
  48.     x ^= x << 1;
  49.  
  50.     t = x;
  51.     x = y;
  52.     y = z;
  53.     z = t ^ x ^ y;
  54.  
  55.     return z;
  56. }
  57.  
  58. //#define lc c[0]
  59. //#define rc c[1]
  60. #define ile(u) (tr[tr[u].par].lc != u)
  61. struct treap{
  62.     struct value{
  63.         ll val; int sz;
  64.         ll aggr;
  65.         value(){val = sz = 0; aggr = 0ll;}
  66.         value(ll c, ll b, int s){val = c, aggr = b, sz = s; }
  67.         void operator = (const value& b) { val = b.val; sz = b.sz; aggr = b.aggr; }
  68.  
  69.     };
  70.     struct tag{
  71.         int rev;       
  72.         tag(){ rev = 0;}
  73.         tag(int a){ rev = a;}
  74.         void operator = (const tag& b){ rev = b.rev; }
  75.         tag operator * (const tag& b) const{
  76.             return tag(rev^b.rev);
  77.         }
  78.         bool isid() const{ return (rev == 0); }
  79.     } ;
  80.     struct node{
  81.         int pr;
  82.         int lc, rc;
  83.         value v;
  84.         tag t;
  85.         node(){ lc = rc = 0; pr = nrng();}
  86.         void operator = (const node& b){
  87.             //node tmp;
  88.             lc = b.lc;
  89.             rc = b.rc;
  90.             v = b.v;
  91.             t = b.t;
  92.             pr = b.pr;
  93.             //return *this;
  94.         }
  95.     } zero;
  96.  
  97.     void pull(int& u){
  98.         tr[u].v.aggr = (tr[tr[u].lc].v.aggr + tr[tr[u].rc].v.aggr + tr[u].v.val);
  99.         tr[u].v.sz = tr[tr[u].lc].v.sz + tr[tr[u].rc].v.sz + 1;
  100.     }
  101.     void apply(int& u, const tag& t){
  102.         if(!u) return ;
  103.         u = dup(u);
  104.         tr[u].t = t * tr[u].t;
  105.         //tr[u].v = t + tr[u].v;
  106.     }
  107.     void push(int& u){
  108.         if((!u) || tr[u].t.isid()) return ;
  109.        
  110.         apply(tr[u].lc, tr[u].t);
  111.         apply(tr[u].rc, tr[u].t);
  112.         if(tr[u].t.rev){
  113.             swap(tr[u].lc, tr[u].rc);
  114.         }
  115.         tr[u].t = tag();
  116.     }
  117.    
  118.     vector<node> tr;
  119.     int zs, ind;
  120.     vector<int> st;
  121.     treap(){
  122.         zs = ind = 0;
  123.         zero = node();
  124.         zero.t = tag();
  125.         zero.v = value();
  126.         tr.pb(zero);
  127.     }
  128.  
  129.     int nd(int k = 0){
  130.         int x;
  131.         //if(zs){ x = st.back(); st.pop_back(); }
  132.         //else x = ++ind;
  133.         x = ++ind;
  134.         tr.pb(node());
  135.         tr[x].v = value(k, k, 1);
  136.         tr[x].t = tag();
  137.         tr[x].pr = nrng();
  138.         return x;
  139.     }
  140.     int dup(int& k){
  141.         int a = ++ind;
  142.         node tmp = tr[k];
  143.         tr.pb(tmp);
  144.         return a;
  145.     }
  146.     void pop(int x){
  147.         tr[x] = zero;
  148.         st.pb(x);
  149.     }
  150.  
  151.     void split(int x, int k, int& L, int& R){
  152.         static string TT;
  153.  
  154.        
  155.  
  156.         if(!x){ L = R = 0; return ;}
  157.         push(x);
  158.         int dist = tr[tr[x].lc].v.sz ;
  159.  
  160.         if(dist < k){
  161.             L = dup(x);
  162.             split(tr[L].rc, k - dist - 1, tr[L].rc, R);
  163.             pull(L);
  164.         }else{
  165.             R = dup(x);
  166.             split(tr[R].lc, k, L, tr[R].lc);   
  167.             pull(R);
  168.         }
  169.     }
  170.  
  171.     int merge(int& L, int& R){
  172.         if(min(L, R) == 0) return L^R;
  173.         push(L); push(R);
  174.         if(tr[L].pr > tr[R].pr){
  175.             tr[L].rc = merge(tr[L].rc, R);
  176.             pull(L);
  177.             return L;
  178.         }else{
  179.             tr[R].lc = merge(L, tr[R].lc);
  180.             pull(R);
  181.             return R;
  182.         }
  183.     }
  184.  
  185.     void merge(int& rt, int& L, int& R){
  186.         rt = merge(L, R);
  187.     }
  188.  
  189.     void pr(int rt){
  190.         static int dep = 0;
  191.         if(dep == 0) cout << rt << ": ";
  192.         if(!rt) return ;
  193.  
  194.         dep++;
  195.         //push(rt);
  196.         pr(tr[rt].lc);
  197.         //if(tr[rt].v.val)
  198.             cout << "(" << rt << ", " << tr[rt].v.val << ") ";
  199.             //cout << tr[rt].v.val << " " ;
  200.         pr(tr[rt].rc);
  201.         dep--;
  202.         if(dep == 0) cout << "\n";
  203.     }
  204.    
  205. };
  206.  
  207. //#undef lc
  208. //#undef rc
  209. //#undef ile
  210.  
  211. int root[MX];
  212.  
  213. void solve(){
  214.     int n, q;
  215.     cin >> q;
  216.     treap bbst = treap();
  217.     bbst.tr.reserve(30000000 +10);
  218.     ll last=  0;
  219.     for(int i = 1; i<=q; i++){
  220.         int ver = in;
  221.         int op = in;
  222.         root[i] = root[ver];
  223.         int rt = root[i];
  224.         if(op == 1){
  225.             ll ind, val;
  226.             cin >> ind >> val;
  227.             ind ^= last;
  228.             val ^= last;
  229.             int t1 = 0, t2 = 0;
  230.             bbst.split(rt, ind, t1, t2);
  231.             int t3 = bbst.nd(val), t4 = 0, t5 = 0;
  232.             bbst.merge(t4, t1, t3);
  233.             bbst.merge(t5, t4, t2);
  234.             rt = t5;
  235.         }else if(op == 2){
  236.             ll ind;
  237.             cin >> ind;
  238.             ind ^= last;
  239.             int t1, t2;
  240.             bbst.split(rt, ind-1, t1, t2);
  241.             int t3, t4;
  242.             bbst.split(t2, 1, t3, t4);
  243.             bbst.merge(rt, t1, t4);
  244.         }else if(op == 3){
  245.             ll l = in, r = in;
  246.             l ^= last;
  247.             r ^= last;
  248.             int t1 = 0, t2 = 0;
  249.             bbst.split(rt, r, t1, t2);
  250.             int t3, t4, t5;
  251.             bbst.split(t1, l-1, t3, t4);
  252.             bbst.apply(t4, treap::tag(1));
  253.             bbst.merge(t5, t3, t4);
  254.             bbst.merge(rt, t5, t2);
  255.  
  256.         }else{
  257.             ll l = in, r = in;
  258.             l ^= last;
  259.             r ^= last;
  260.             //l--, r--;
  261.             int t1 = 0, t2 = 0;
  262.             bbst.split(rt, r, t1, t2);
  263.             int t3, t4;
  264.             bbst.split(t1, l-1, t3, t4);
  265.             ll ans = bbst.tr[t4].v.aggr;
  266.             int t5, t6;
  267.             bbst.merge(t5, t3, t4);
  268.             bbst.merge(t6, t5, t2);
  269.             rt = t6;
  270.             cerr << "query: ";
  271.             last = ans;
  272.             cout << ans << "\n";
  273.         }
  274.         root[i] = rt;
  275.         if(i <= 100) cerr << "pkinghiiiiiiii\n";
  276.         assert(bbst.tr[0].v.val == 0);
  277.     }
  278. }
  279.  
  280. int main(){
  281.     cin.tie(0) -> sync_with_stdio(0);
  282.  
  283.     int T = 1;
  284.     //cin >> T;
  285.     for(int i = 1; i<=T; i++){
  286.         solve();
  287.     }
  288.     return 0;
  289. }
  290.  
  291.  
  292.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement