Guest User

reverseTreap.cpp

a guest
Mar 14th, 2026
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <ctime>
  7. #include <string>
  8.  
  9. #define INF (int)(1e9 + 7)
  10.  
  11. using namespace std;
  12.  
  13. struct node {
  14.     int cnt, prior, key, minn;
  15.     bool rev;
  16.  
  17.     node *l, *r;
  18.  
  19.     node (int x)
  20.     {
  21.         l = r = 0;
  22.         rev = false;
  23.         cnt = 1;
  24.         prior = rand();
  25.         key = minn = x;
  26.     }
  27. };
  28.  
  29. typedef node* pnode;
  30.  
  31. pnode T, t1, t2, t3, t4;
  32. int n, x, y, m, z;
  33. char s;
  34.  
  35.  
  36.  
  37. void rever(pnode t)
  38. {
  39.     if (t)
  40.     {
  41.         if (t -> rev == false) t -> rev = true;
  42.         else t -> rev = false; 
  43.     }
  44. }
  45.  
  46. int get_cnt(pnode t)
  47. {
  48.     if (t == 0) return 0;
  49.         return t -> cnt;
  50. }
  51.  
  52. int get_minn(pnode t)
  53. {
  54.     if (t == 0) return INF;
  55.     return t -> minn;
  56. }
  57.  
  58. void push(pnode t)
  59. {
  60.     if (t && t -> rev == true)
  61.     {
  62.             t -> rev = false;
  63.             swap(t -> l, t -> r);
  64.             if (t -> l) rever(t -> l);
  65.             if (t -> r) rever(t -> r);
  66.     }
  67. }
  68.  
  69. void update(pnode t)
  70. {
  71.     if (t)
  72.     {
  73.         push(t);
  74.         t -> cnt = get_cnt(t -> l) + get_cnt(t -> r) + 1;
  75.         t -> minn = min(min(get_minn(t -> l), t -> key), get_minn(t -> r));
  76.     }
  77. }                        
  78.  
  79. void split(pnode t, int x, pnode &l, pnode &r)
  80. {
  81.     if (t == 0) l = r = 0;
  82.     else
  83.     {       push(t);
  84.         if (get_cnt(t -> l) < x)
  85.         {
  86.             split(t -> r, x - get_cnt(t -> l) - 1, t -> r, r);
  87.             l = t;             
  88.         }
  89.         else
  90.         {
  91.             split(t -> l, x, l, t -> l);
  92.             r = t;
  93.         }
  94.     }
  95.     update(l);
  96.     update(r);
  97. }
  98.  
  99. void merge(pnode &t, pnode l, pnode r)
  100. {
  101.     update(l);
  102.     update(r);
  103.     if (l == 0) t = r;
  104.     else if (r == 0) t = l;
  105.     else
  106.     {
  107.         if (l -> prior > r -> prior)
  108.         {
  109.                 merge(l -> r, l -> r, r);
  110.             t = l;
  111.         }
  112.         else
  113.         {
  114.             merge(r -> l, l, r -> l);
  115.             t = r;     
  116.         }
  117.     }
  118.     update(t);
  119. }
  120.  
  121. void gocout(pnode t)
  122. {
  123.     if (t == 0) return;
  124.     //push(t);
  125.     gocout(t -> l);
  126.     cout << t -> key << ' ';
  127.     gocout(t -> r);
  128. }
  129.  
  130. int main()
  131. {
  132.     srand( time(0) );
  133.  
  134.     cin >> n >> m;
  135.  
  136.     for (int i = 1; i <= n; i++)
  137.     {
  138.         cin >> x;
  139.             t1 = new node(x);
  140.             merge(T, T, t1);
  141.     }
  142.  
  143.         for (int i = 1; i <= m; i++)
  144.         {
  145.             cin >> z >> x >> y;
  146.             split(T, y, t1, t3);
  147.             split(t1, x - 1, t1, t2);
  148.                
  149.             if (z == 2)
  150.                 cout << t2 -> minn << endl;
  151.             else
  152.                 rever(t2);
  153.  
  154.             merge(t1, t1, t2);         
  155.                 merge(T, t1, t3);
  156.  
  157.  //               gocout(T);
  158.    //             cout << endl;
  159.         }
  160.  
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment