Advertisement
GuilhermeCpp

Untitled

Aug 14th, 2023
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.63 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define NMAX 100010
  5. #define INF 1000000000
  6. #define fi first
  7. #define se second
  8.  
  9. typedef pair< int, int > pll;
  10.  
  11. pll invalid = {INF, INF};
  12.  
  13. int newId = 0;
  14.  
  15. typedef struct Treap
  16. {
  17.  
  18.     int y, val, me, tam;
  19.  
  20.     bool inv;
  21.  
  22.     Treap *pai, *lef, *rig;
  23.  
  24.     Treap(int pri, int v, bool status = false, Treap *p = NULL, Treap *l = NULL, Treap *r = NULL)
  25.     {
  26.    
  27.         y = pri;
  28.         val = v;
  29.         me = v;
  30.         inv = status;
  31.         tam = 1;
  32.         pai = p;
  33.         lef = l;
  34.         rig = r;
  35.    
  36.     }
  37.  
  38. }Tr;
  39.  
  40. Tr *node[NMAX];
  41. Tr *node2[2 * NMAX];
  42.  
  43. int Size(Tr *root)
  44. {
  45.  
  46.     return (root == NULL) ? 0 : root->tam;
  47.  
  48. }
  49.  
  50. int GetVal(Tr *root)
  51. {
  52.  
  53.     return (root == NULL) ? INF : root->val;
  54.  
  55. }
  56.  
  57. int GetMin(Tr *root)
  58. {
  59.  
  60.     return (root == NULL) ? INF : root->me;
  61.  
  62. }
  63.  
  64. void UpdatePai(Tr *root, Tr *p)
  65. {
  66.  
  67.     if(root != NULL) root->pai = p;
  68.    
  69.     return;
  70.  
  71. }
  72.  
  73. void ChangeInv(Tr *root)
  74. {
  75.  
  76.     if(root != NULL) root->inv ^= true;
  77.    
  78.     return;
  79.  
  80. }
  81.  
  82. void Lazy(Tr *root)
  83. {
  84.  
  85.     if(root == NULL) return;
  86.  
  87.     if(root->inv == true)
  88.     {
  89.    
  90.         swap(root->lef, root->rig);
  91.    
  92.         ChangeInv(root->lef);
  93.         ChangeInv(root->rig);
  94.    
  95.         root->inv = false;
  96.    
  97.     }
  98.    
  99.     return;
  100.    
  101. }
  102.  
  103. void Recalc(Tr *root, int who)
  104. {
  105.  
  106.     if(root == NULL) return;
  107.    
  108.     if(who == 0) node[root->val] = root;
  109.     if(who == 2) node2[root->val] = root;
  110.        
  111.     root->tam = Size(root->lef) + 1 + Size(root->rig);
  112.    
  113.     UpdatePai(root->lef, root);
  114.     UpdatePai(root->rig, root);
  115.    
  116.     root->me = min(root->val, min(GetMin(root->lef), GetMin(root->rig)));
  117.    
  118.     return;
  119.    
  120. }
  121.  
  122. void Print(Tr *root)
  123. {
  124.  
  125.     Lazy(root);
  126.  
  127.     if(root == NULL) return;
  128.  
  129.     Print(root->lef);
  130.     cout << abs(root->val) << " ";
  131.     Print(root->rig);
  132.    
  133.     return;
  134.  
  135. }
  136.  
  137. Tr* Merge(Tr* l, Tr* r, int who)
  138. {
  139.    
  140.     Recalc(l, who);
  141.     Recalc(r, who);
  142.     Lazy(l);
  143.     Lazy(r);
  144.  
  145.     if(l == NULL) return r;
  146.     if(r == NULL) return l;
  147.    
  148.     Tr *novo;
  149.    
  150.     if(l->y > r->y)
  151.     {
  152.    
  153.         novo = new Treap(l->y, l->val, l->inv, l->pai, l->lef, Merge(l->rig, r, who));
  154.        
  155.     }else
  156.     {
  157.    
  158.         novo = new Treap(r->y, r->val, r->inv, r->pai, Merge(l, r->lef, who), r->rig);
  159.    
  160.     }
  161.    
  162.     Recalc(novo, who);
  163.    
  164.     return novo;
  165.  
  166. }
  167.  
  168. void Split(Tr* root, int idx, Tr **l, Tr **r, int who)
  169. {
  170.    
  171.     Recalc(root, who);
  172.     Lazy(root);
  173.  
  174.     if(root == NULL)
  175.     {
  176.    
  177.         *l = NULL;
  178.         *r = NULL;
  179.    
  180.         return;
  181.    
  182.     }
  183.    
  184.     Tr *novo;
  185.    
  186.     int tl = Size(root->lef);
  187.    
  188.     if(tl + 1 <= idx)
  189.     {
  190.        
  191.         Split(root->rig, idx - (tl + 1), &novo, r, who);
  192.        
  193.         *l = new Treap(root->y, root->val, root->inv, root->pai, root->lef, novo);
  194.         Recalc(*l, who);
  195.    
  196.     }else
  197.     {
  198.    
  199.         Split(root->lef, idx, l, &novo, who);
  200.        
  201.         *r = new Treap(root->y, root->val, root->inv, root->pai, novo, root->rig);
  202.         Recalc(*r, who);
  203.    
  204.     }
  205.    
  206.     return;
  207.  
  208. }
  209.  
  210. void Add(Tr **rootId, Tr **rootVal, Tr **rootInOut, int pos1, int pos2, int val)
  211. {
  212.    
  213.     Tr *m, *m1, *m2, *l, *r;
  214.    
  215.     m = new Treap(rand() % NMAX, newId);
  216.     Split(*rootId, pos1, &l, &r, 0);
  217.     *rootId = Merge(Merge(l, m, 0), r, 0);
  218.    
  219.     m = new Treap(rand() % NMAX, val);
  220.     Split(*rootVal, pos1, &l, &r, 1);
  221.     *rootVal = Merge(Merge(l, m, 1), r, 1);
  222.    
  223.     m1 = new Treap(rand() % NMAX, newId);
  224.     m2 = new Treap(rand() % NMAX, newId + NMAX);
  225.     m = Merge(m1, m2, 2);
  226.     Split(*rootInOut, pos2, &l, &r, 2);
  227.     *rootInOut = Merge(Merge(l, m, 2), r, 2);
  228.    
  229.     newId++;
  230.    
  231.     return;
  232.  
  233. }
  234.  
  235. void Invert(Tr **root, int a, int b)
  236. {
  237.  
  238.     Tr *l, *r, *m, *aux;
  239.    
  240.     Split(*root, a, &l, &aux, 1);
  241.     Split(aux, b - a + 1, &m, &r, 1);
  242.        
  243.     ChangeInv(m);
  244.    
  245.     *root = Merge(Merge(l, m, 1), r, 1);
  246.    
  247.     return;
  248.  
  249. }
  250.  
  251. int GetPos(Tr *root)
  252. {
  253.  
  254.     if(root->pai == NULL) return 0;
  255.  
  256.     if(root->pai->lef == root)  return GetPos(root->pai);
  257.     else                        return GetPos(root->pai) + Size(root->pai->lef) + 1;
  258.  
  259. }
  260.  
  261. int GetPos(int u)
  262. {
  263.  
  264.     return GetPos(node[u]) + Size(node[u]->lef);
  265.  
  266. }
  267.  
  268. int GetPos2(Tr *root)
  269. {
  270.  
  271.     if(root->pai == NULL) return 0;
  272.  
  273.     if(root->pai->lef == root)  return GetPos2(root->pai);
  274.     else                        return GetPos2(root->pai) + Size(root->pai->lef) + 1;
  275.  
  276. }
  277.  
  278. int GetPos2(int u)
  279. {
  280.  
  281.     return GetPos2(node2[u]) + Size(node2[u]->lef);
  282.  
  283. }
  284.  
  285. int GetNxt(Tr *root)
  286. {
  287.  
  288.     if(GetMin(root->lef) < NMAX) return GetNxt(root->lef);
  289.    
  290.     if(root->val < NMAX) return Size(root->lef);
  291.    
  292.     return GetNxt(root->rig) + Size(root->lef) + 1;
  293.    
  294.  
  295. }
  296.  
  297. int GetNxt(Tr **root, int u)
  298. {
  299.  
  300.     Tr *before, *after, *l, *m, *r, *aux;
  301.    
  302.     int p = GetPos2(u + NMAX);
  303.  
  304.     Split(*root, p, &before, &after, 2);
  305.    
  306.     p = GetNxt(after);
  307.    
  308.     Split(after, p, &l, &aux, 2);
  309.     Split(aux, 1, &m, &r, 2);
  310.  
  311.     int x = m->val;
  312.    
  313.     *root = Merge(Merge(Merge(before, l, 2), m, 2), r, 2);
  314.    
  315.     return x;
  316.  
  317. }
  318.  
  319. int Solv(Tr *root, int idx)
  320. {
  321.  
  322.     Tr *l, *r, *aux;
  323.  
  324.     Split(root, idx, &l, &aux, 1);
  325.     Split(aux, 1, &l, &r, 1);
  326.    
  327.     return l->val;
  328.  
  329. }
  330.  
  331. int main()
  332. {
  333.  
  334.     ios_base::sync_with_stdio(0);
  335.     cin.tie(0);
  336.     cout.tie(0);
  337.    
  338.     int t, n, op, u, v, val, l, r, p, i, j;
  339.    
  340.     vector< int > resp;
  341.    
  342.     Treap *treapId, *treapVal, *treapInOut;
  343.    
  344.     srand(time(NULL));
  345.  
  346.     treapId = NULL;
  347.     treapVal = NULL;
  348.     treapInOut = NULL;
  349.    
  350.     cin >> val >> n;
  351.    
  352.     Add(&treapId, &treapVal, &treapInOut, 0, 0, 0);
  353.     Add(&treapId, &treapVal, &treapInOut, 0, 0, val);
  354.    
  355.     while(n--)
  356.     {
  357.  
  358.         cin >> op;
  359.        
  360.         if(op == 1)
  361.         {
  362.        
  363.             cin >> u >> val;
  364.            
  365.             v = GetNxt(&treapInOut, u);
  366.            
  367.             Add(&treapId, &treapVal, &treapInOut, GetPos(v), GetPos2(u + NMAX), val);
  368.            
  369.         }
  370.        
  371.         if(op == 2)
  372.         {
  373.        
  374.             cin >> l >> r;
  375.            
  376.             l--;
  377.             r--;
  378.        
  379.             Invert(&treapVal, l, r);
  380.        
  381.         }
  382.        
  383.         if(op == 3)
  384.         {
  385.        
  386.             cin >> u;
  387.        
  388.             resp.push_back(Solv(treapVal, GetPos(u)));
  389.            
  390.         }
  391.        
  392.     }
  393.    
  394.     for(auto cur : resp) cout << cur << endl;  
  395.    
  396.     return 0;
  397.  
  398. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement