Guest User

treap.cpp

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