Advertisement
Guest User

Untitled

a guest
May 25th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define INF (1<<30)
  4.  
  5. using namespace std;
  6.  
  7. int id_cnt = 0; // Each Node has a unique ID (for debugging purpouses)
  8.  
  9. struct Node{
  10.     Node *left, *right, *parent;
  11.     Node *thin_parent;
  12.     bool rotated;
  13.     int value;
  14.     int size;
  15.     int subtree_min;
  16.     int lazy_add;
  17.     int id;
  18.  
  19.     Node(int v = 0): left(0), right(0), parent(0), thin_parent(0), rotated(false), value(v), size(1), subtree_min(v), lazy_add(0){
  20.         id = id_cnt++;
  21.     }
  22. };
  23.  
  24. bool is_left_child(Node *n){
  25.     assert(n->parent);
  26.     return n->parent->left == n;
  27. }
  28.  
  29. void set_parent(Node *n, Node *p){
  30.     if(!n)return;
  31.     n->parent = p;
  32. }
  33.  
  34. // Propagates lazy flags
  35. void touch(Node *n){
  36.     if(!n)return;
  37.     if(n->rotated){
  38.         swap(n->left, n->right);
  39.         if(n->left)n->left->rotated = !n->left->rotated;
  40.         if(n->right)n->right->rotated = !n->right->rotated;
  41.         n->rotated = false;
  42.     }
  43.     if(n->lazy_add != 0){
  44.         int a = n->lazy_add;
  45.         if(n->left){
  46.             n->left->lazy_add+=a;
  47.             n->left->value+=a;
  48.             n->left->subtree_min+=a;
  49.         }
  50.         if(n->right){
  51.             n->right->lazy_add+=a;
  52.             n->right->value+=a;
  53.             n->right->subtree_min+=a;
  54.         }
  55.         n->lazy_add=0;
  56.     }
  57. }
  58.  
  59. int size(Node *n){
  60.     return n ? n->size : 0;
  61. }
  62.  
  63. int subtree_min(Node *n){
  64.     return n ? n->subtree_min : INF;
  65. }
  66.  
  67. void soak(Node *n){
  68.     n->size = 1 + size(n->left) + size(n->right);
  69.     n->subtree_min = min(n->value, min(subtree_min(n->left), subtree_min(n->right)));
  70. }
  71.  
  72. Node * splay(Node *n){
  73.     assert(n);
  74.  
  75.     stack<Node*> s;
  76.     Node *l_root = new Node;
  77.     Node *r_root = new Node;
  78.  
  79.     Node *l = l_root, *r = r_root;
  80.  
  81.     Node *x = n;
  82.  
  83.     while(x->parent){
  84.         s.push(x);
  85.         x = x->parent;
  86.     }
  87.  
  88.     Node *thin_parent = x->thin_parent;
  89.     x->thin_parent = NULL;
  90.  
  91.     Node *y, *z;
  92.     while(!s.empty()){
  93.         y = s.top();
  94.         s.pop();
  95.         touch(x);
  96.         touch(y);
  97.  
  98.         if(s.empty()){ // There was only one vertex remaining
  99.             if(is_left_child(y)){
  100.                 r->left = x;
  101.                 set_parent(x, r);
  102.                 r = x;
  103.             } else{
  104.                 l->right = x;
  105.                 set_parent(x, l);
  106.                 l = x;
  107.             }
  108.             x = y;
  109.             x->parent=NULL;
  110.             break;
  111.         }
  112.  
  113.         z = s.top();
  114.         s.pop();       
  115.  
  116.         touch(z);
  117.  
  118.         if(is_left_child(y) && is_left_child(z)){
  119.             Node *tmp = y->right;
  120.             x->left = tmp;
  121.             set_parent(tmp, x);
  122.             y->left = NULL;
  123.             y->right = x;
  124.             set_parent(x, y);
  125.             r->left = y;
  126.             set_parent(y, r);
  127.             soak(x);
  128.             r = y;
  129.             x = z;
  130.         } else if(!is_left_child(y) && !is_left_child(z)){
  131.             Node *tmp = y->left;
  132.             x->right = tmp;
  133.             set_parent(tmp, x);
  134.             y->right = NULL;
  135.             y->left = x;
  136.             set_parent(x, y);
  137.             l->right = y;
  138.             set_parent(y, l);
  139.             soak(x);
  140.             l = y;
  141.             x = z;
  142.         } else if(is_left_child(y) && !is_left_child(z)){
  143.             s.push(z);
  144.             r->left = x;
  145.             set_parent(x, r);
  146.             x->left = NULL;
  147.             r = x;
  148.             x = y;
  149.         } else /*if(!is_left_child(y) && is_left_child(z))*/{
  150.             s.push(z);
  151.             l->right = x;
  152.             set_parent(x, l);
  153.             x->right = NULL;
  154.             l = x;
  155.             x = y;
  156.         }
  157.         x->parent=NULL;
  158.     }
  159.  
  160.     touch(x);
  161.     Node *A = x->left, *B = x->right;
  162.  
  163.     l->right = A;
  164.     set_parent(A, l);
  165.     while(l){
  166.         soak(l);
  167.         l = l->parent;
  168.     }
  169.  
  170.     r->left = B;
  171.     set_parent(B, r);
  172.     while(r){
  173.         soak(r);
  174.         r = r->parent;
  175.     }
  176.  
  177.     x->left = l_root->right;
  178.     set_parent(x->left, x);
  179.     x->right = r_root->left;
  180.     set_parent(x->right, x);
  181.     soak(x);
  182.  
  183.  
  184.     x->thin_parent = thin_parent;
  185.  
  186.     assert(x == n);
  187.  
  188.     delete l_root;
  189.     delete r_root;
  190.  
  191.     return x;
  192. }
  193.  
  194. Node * join(Node *l, Node *r){
  195.     if(!l)return r;
  196.     if(!r)return l;
  197.     while(l->parent)l = l->parent;
  198.     touch(l);
  199.     while(l->right){
  200.         l = l->right;
  201.         touch(l);
  202.     }
  203.     splay(l);
  204.     splay(r);
  205.     l->right = r;
  206.     set_parent(r, l);
  207.     soak(l);
  208.     return l;
  209. }
  210.  
  211. Node * prev(Node *n){
  212.     splay(n);
  213.     if(!n->left)return 0;
  214.     n=n->left;
  215.     touch(n);
  216.     while(n->right){
  217.         n=n->right;
  218.         touch(n);
  219.     }
  220.     return splay(n);
  221. }
  222. Node * next(Node *n){
  223.     splay(n);
  224.     if(!n->right)return 0;
  225.     n=n->right;
  226.     touch(n);
  227.     while(n->left){
  228.         n=n->left;
  229.         touch(n);
  230.     }
  231.     return splay(n);
  232. }
  233. Node * first(Node *n){
  234.     assert(n);
  235.     while(n->parent)n=n->parent;
  236.     touch(n);
  237.     while(n->left){
  238.         n=n->left;
  239.         touch(n);
  240.     }
  241.     return splay(n);
  242. }
  243. Node * last(Node *n){
  244.     assert(n);
  245.     while(n->parent)n=n->parent;
  246.     touch(n);
  247.     while(n->right){
  248.         n=n->right;
  249.         touch(n);
  250.     }
  251.     return splay(n);
  252. }
  253.  
  254. pair<Node*, Node*> path_cut_before(Node *n){
  255.     splay(n);
  256.     Node *l = n->left;
  257.     set_parent(l, 0);
  258.     n->left=0;
  259.     soak(n);
  260.     return make_pair(l, n);
  261. }
  262.  
  263. pair<Node*, Node*> path_cut(Node *n){
  264.     splay(n);
  265.     Node *r = n->right;
  266.     set_parent(r, 0);
  267.     r->thin_parent = n->thin_parent;
  268.     n->thin_parent = 0;
  269.     n->right=0;
  270.     soak(n);
  271.     return make_pair(n, r);
  272. }
  273.  
  274. void path_link(Node *a, Node *b){
  275.     splay(a); splay(b);
  276.     Node *thin_parent = b->thin_parent;
  277.     b->thin_parent = NULL;
  278.     Node *tmp = join(a, b);
  279.  
  280.     tmp->thin_parent = thin_parent;
  281. }
  282.  
  283. void reverse(Node *n){
  284.     auto tmp = path_cut_before(n);
  285.     tmp.second->rotated=!tmp.second->rotated;
  286.     join(tmp.first, tmp.second);
  287. }
  288.  
  289. int cost(Node *n){
  290.     return splay(n)->value;
  291. }
  292.  
  293. int path_min(Node *n){
  294.     splay(n);
  295.     return min(n->value, subtree_min(n->right));
  296. }
  297.  
  298. void set_cost(Node *n, int cost){
  299.     splay(n);
  300.     n->value = cost;
  301.     soak(n);
  302. }
  303. void path_update(Node *n, int delta){
  304.     auto tmp = path_cut_before(n);
  305.     tmp.second->value+=delta;
  306.     tmp.second->subtree_min+=delta;
  307.     tmp.second->lazy_add+=delta;
  308.     join(tmp.first, tmp.second);
  309. }
  310.  
  311. Node *parent(Node *n){
  312.     splay(n);
  313.     Node *x = next(n);
  314.     if(x)return splay(x);
  315.     return n->thin_parent ? splay(n->thin_parent) : 0;
  316. }
  317.  
  318. Node *root(Node *n){
  319.     n = last(n);
  320.     while(n->thin_parent){
  321.         n = n->thin_parent;
  322.         n = last(n);
  323.     }
  324.     return splay(n);
  325. }
  326.  
  327. void cut(Node *n){
  328.     if(next(n)){
  329.         path_cut(n);
  330.     } else{
  331.         splay(n);
  332.         n->thin_parent=0;
  333.     }
  334. }
  335.  
  336. void link(Node *u, Node *v){
  337.     splay(u);
  338.     u->thin_parent = v;
  339. }
  340.  
  341. void expose(Node *n){
  342.     auto tmp = path_cut_before(n);
  343.     if(tmp.first)tmp.first->thin_parent=n;
  344.     splay(n);
  345.     Node *p;
  346.     while(n->thin_parent){
  347.         p=n->thin_parent;
  348.         n->thin_parent=0;
  349.         tmp = path_cut_before(p);
  350.         if(tmp.first)tmp.first->thin_parent=p;
  351.         path_link(n, p);
  352.         n=splay(p);
  353.     }
  354. }
  355.  
  356. void evert(Node *n){
  357.     expose(n);
  358.     splay(n);
  359.     n->rotated = !n->rotated;
  360. }
  361.  
  362. Node *nodes[111111];
  363. int n, m, a, b;
  364.  
  365. int main(){
  366.     scanf("%d %d", &n, &m);
  367.     nodes[0] = NULL;
  368.     for (int i = 1; i <= n; ++i) {
  369.         nodes[i] = new Node(i);
  370.         nodes[i]->thin_parent = nodes[i];
  371.     }
  372.  
  373.     for (int i = 1; i <= m; ++i) {
  374.         string s;
  375.         cin >> s;
  376.         scanf("%d %d", &a, &b);
  377.         if(s[1]=='o'){ // connected
  378.             printf(root(nodes[a]) == root(nodes[b]) ? "1\n" : "0\n");
  379.         }
  380.         if(s[1]=='u'){ // cut
  381.             evert(nodes[a]);
  382.             assert(parent(nodes[b]) == nodes[a]);
  383.             cut(nodes[b]);
  384.         }
  385.         if(s[1]=='i'){ // link
  386.             evert(nodes[a]);
  387.             assert(root(nodes[b]) != root(nodes[a]));
  388.             link(nodes[a], nodes[b]);
  389.         }                                  
  390.     }
  391.  
  392.  
  393.     return 0;
  394.  
  395. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement