Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int INF = 1e9 + 7;
  6. const long long MOD = 1e9;
  7.  
  8. struct node {
  9.     int key, prior, size;
  10.     long long sum;
  11.     node* l;
  12.     node* r;
  13.     node (int x) {
  14.         key = x;
  15.         prior = rand();
  16.         r = l = nullptr;
  17.         size = 1;
  18.         sum = x;
  19.     }
  20. };
  21.  
  22. node* root;
  23.  
  24. long long get_sum(node* v) {
  25.     return v == nullptr ? 0 : v->sum;
  26. }
  27. int get_size(node* v) {
  28.     return v == nullptr ? 0 : v->size;
  29. }
  30.  
  31. void recalc(node* v) {
  32.     if (v == nullptr)
  33.         return;
  34.     v->size = get_size(v->l) + get_size(v->r) + 1;
  35.     v->sum = get_sum(v->l) + get_sum(v->r) + v->key;
  36. }
  37.  
  38. node* merge(node* l, node* r) {
  39.     if (l == nullptr)
  40.         return r;
  41.     if (r == nullptr)
  42.         return l;
  43.     if (l->prior > r->prior) {
  44.         l->r = merge(l->r, r);
  45.         recalc(l);
  46.         return l;
  47.     }
  48.     r->l = merge(l, r->l);
  49.     recalc(r);
  50.     return r;
  51. }
  52.  
  53. pair<node*, node*> split(node* v, int key) {
  54.     if (v == nullptr)
  55.         return make_pair(nullptr, nullptr);
  56.     if (get_size(v->l) >= key) {
  57.         auto p = split(v->l, key);
  58.         v->l = p.second;
  59.         recalc(v);
  60.         return make_pair(p.first, v);
  61.     }
  62.     auto p = split(v->r, key - get_size(v->l) - 1);
  63.     v->r = p.first;
  64.     recalc(v);
  65.     return make_pair(v, p.second);
  66. }
  67.  
  68. void insert(int x) {
  69.     node *v = new node(x);
  70.     auto p = split(root, x);
  71.     root = merge(merge(p.first, v), p.second);
  72. }
  73.  
  74. void del(node* v, int x) {
  75.     auto p = split(v, x);
  76.     auto a = p.first;
  77.     p = split(p.second, x + 1);
  78.     root = merge(a, p.second);
  79. }
  80.  
  81. bool check(node* v, int x) {
  82.     if (v == nullptr)
  83.         return false;
  84.     if (v->key == x)
  85.         return true;
  86.     if (v->key < x)
  87.         return check(v->r, x);
  88.     return check(v->l, x);
  89. }
  90.  
  91. int kth(node* v, int k) {
  92.     if (get_size(v->l) == k)
  93.         return v->key;
  94.     if (get_size(v->l) > k)
  95.         return kth(v->l, k);
  96.     return kth(v->r, k - get_size(v->l) - 1);
  97. }
  98.  
  99. int next(node* v, int x) {
  100.     if (v == nullptr)
  101.         return INF;
  102.     if (v->key > x)
  103.         return min(next(v->l, x), v->key);
  104.     return next(v->r, x);
  105. }
  106.  
  107. int prev(node* v, int x) {
  108.     if (v == nullptr)
  109.         return -INF;
  110.     if (v->key < x)
  111.         return max(prev(v->r, x), v->key);
  112.     return prev(v->l, x);
  113. }
  114.  
  115. void out(node* v) {
  116.     if (v == nullptr)
  117.         return;
  118.     out(v->l);
  119.     cout << v->key << ' ';
  120.     out(v->r);
  121. }
  122.  
  123. int main()
  124. {
  125.     //freopen("input.txt", "r", stdin);
  126.     //freopen("movetofront.in", "r", stdin);
  127.     //freopen("output.txt", "w", stdout);
  128.     //freopen("movetofront.out", "w", stdout);
  129.     srand(time(NULL));
  130.     int n, m;
  131.     cin >> n >> m;
  132.     for (int i = 1; i <= n; ++i)
  133.         insert(i);
  134. //    out(root);
  135. //    cout << endl;
  136.     for (int i = 0; i < m; ++i) {
  137.         int l, r;
  138.         cin >> l >> r;
  139. //        cout << "l = " << l << " r = " << r << endl;
  140.         auto a = split(root, r);
  141. //        cout << "a.first: ";
  142. //        out(a.first);
  143. //        cout << endl;
  144. //        cout << "a.second: ";
  145. //        out(a.second);
  146. //        cout << endl;
  147.         auto b = split(a.first, l - 1);
  148. //        cout << "b.first: ";
  149. //        out(b.first);
  150. //        cout << endl;
  151. //        cout << "b.second: ";
  152. //        out(b.second);
  153. //        cout << endl;
  154.         root = merge(b.second, merge(b.first, a.second));
  155. //        out(root);
  156. //        cout << endl;
  157.         //cout << get_size(root) << endl;
  158.     }
  159.     out(root);
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement