Advertisement
MathQ_

Untitled

Aug 17th, 2021
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
  2.  
  3. struct Node {
  4.     int key, prior, val = 1;
  5.     Node *l = nullptr, *m = nullptr, *r = nullptr;
  6.     Node(int _key) { key = _key; prior = rd(); }
  7. };
  8.  
  9. typedef pair<Node*, Node*> nodepair;
  10.  
  11. const int MAXK = 1e6 + 1, inf = 2e9;
  12. vector<Node*> color(MAXK);
  13.  
  14. int val(Node *p) { return (p == nullptr ? 0 : p->val); }
  15. void upd(Node *p) {
  16.     if (p == nullptr) return;
  17.     p->val = val(p->l) + val(p->m) + val(p->r) + 1;
  18. }
  19. void push(Node *p);
  20. nodepair split(Node *p, int x);
  21. Node* merge(Node *p1, Node *p2);
  22.  
  23. void push(Node *p) {
  24.     if (p == nullptr || p->m == nullptr) return;
  25.     push(p->l);
  26.     push(p->r);
  27.     nodepair q = split(p->m, p->key);
  28.     if (p->l) {
  29.         p->l->m = q.fi;
  30.     } else {
  31.         p->l = q.fi;
  32.     }
  33.     if (p->r) {
  34.         p->r->m = q.se;
  35.     } else {
  36.         p->r = q.se;
  37.     }
  38.     p->m = nullptr;
  39.     upd(p); upd(p->l); upd(p->r);
  40. }
  41.  
  42. nodepair split(Node *p, int x) {
  43.     if (p == nullptr) return {nullptr, nullptr};
  44.     push(p);
  45.     if (p->key <= x) {
  46.         nodepair q = split(p->r, x);
  47.         p->r = q.fi;
  48.         upd(p);
  49.         return {p, q.se};
  50.     } else {
  51.         nodepair q = split(p->l, x);
  52.         p->l = q.se;
  53.         upd(p);
  54.         return {q.fi, p};
  55.     }
  56. }
  57.  
  58. Node* merge(Node *p1, Node *p2) {
  59.     if (p1 == nullptr) return p2;
  60.     if (p2 == nullptr) return p1;
  61.     push(p1); push(p2);
  62.     if (p1->prior > p2->prior) {
  63.         p1->r = merge(p1->r, p2);
  64.         upd(p1);
  65.         return p1;
  66.     } else {
  67.         p2->l = merge(p1, p2->l);
  68.         upd(p2);
  69.         return p2;
  70.     }
  71. }
  72.  
  73. Node* unite(Node *p1, Node *p2) {
  74.     if (p1 == nullptr) return p2;
  75.     if (p2 == nullptr) return p1;
  76.     if (p1->prior < p2->prior) swap(p1, p2);
  77.     push(p1);
  78.     p1->m = p2;
  79.     upd(p1);
  80.     return p1;
  81. }
  82.  
  83. void insert(int c, int k) {
  84.     Node *p = new Node(k);
  85.     nodepair q = split(color[c], k);
  86.     color[c] = merge(merge(q.fi, p), q.se);
  87. }
  88.  
  89. Node* remove_segment(int c, int l, int r) {
  90.     nodepair q = split(color[c], r);
  91.     nodepair p = split(q.fi, l - 1);
  92.     color[c] = merge(p.fi, q.se);
  93.     return p.se;
  94. }
  95.  
  96. int update(int l, int r, int c1, int c2) {
  97.     Node *p = remove_segment(c1, l, r);
  98.     int ans = val(p);
  99.     color[c2] = unite(color[c2], p);
  100.     return ans;
  101. }
  102.  
  103. int main() {
  104.     fast
  105.     // file_in
  106.  
  107.     int n, q;
  108.     cin >> n >> q;
  109.     for (int i = 1; i <= n; ++i) {
  110.         insert(0, i);
  111.     }
  112.     int l, r, c1, c2;
  113.     while (q--) {
  114.         cin >> l >> r >> c1 >> c2;
  115.         cout << update(l, r, c1, c2) << '\n';
  116.     }
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement