orazma

a.cpp

Oct 12th, 2026
16
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 val)
  21.     {
  22.         l = r = 0;
  23.         cnt = 1;
  24.         minn = val;
  25.         key = val;
  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)
  59.         l = r = 0;
  60.     else{
  61.         if (get_cnt(t -> l) < x){
  62.             split(t -> r, x - get_cnt(t -> l) - 1, t -> r, r);
  63.             l = t;
  64.         }
  65.         else{
  66.             split(t -> l, x, l, t -> l);
  67.             r = t;
  68.         }
  69.     }
  70.     update(l);
  71.     update(r);
  72. }
  73.  
  74. void gocout(pnode t)
  75. {
  76.     if (t == 0) return;
  77.  
  78.     gocout(t -> l);
  79.     cout << t -> key << ' ';
  80.     gocout(t -> r);
  81. }
  82.  
  83. void merge(pnode &t, pnode l, pnode r)
  84. {
  85.     if (l == 0) t = r;
  86.     else if (r == 0) t = l;
  87.     else
  88.     {
  89.         if (l -> prior > r -> prior)
  90.         {
  91.             merge(l -> r, l -> r, r);
  92.             t = l;
  93.         }
  94.         else
  95.         {
  96.             merge(r -> l, l, r -> l);
  97.             t = r;
  98.         }
  99.     }
  100.     update(t);
  101. }
  102.  
  103. int main()
  104. {
  105.         srand( time (0) );
  106.  
  107.         cin >> n;
  108.  
  109.         for (int i = 1; i <= n; i++)
  110.         {
  111.             cin >> s >> x >> y;
  112.             if (s == '+')
  113.             {
  114.                 t3 = new node(y);
  115.                 split(T, x, t1, t2);
  116.                 merge(t1, t1, t3);
  117.                 merge(T, t1, t2);
  118.             }
  119.             else
  120.             {
  121.                 if (x > y) swap(x, y);
  122.                 split(T, y, t1, t2);
  123.                 split(t1, x - 1, t1, t3);
  124.                 //gocout(t3);
  125.                 //cout << endl;
  126.                 cout << t3 -> minn << endl;
  127.                 merge(t1, t1, t3);
  128.                 merge(T, t1, t2);
  129.             }
  130.         }
  131.  
  132.     return 0;
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment