zoukankan      html  css  js  c++  java
  • Link-Cut Tree指针模板

    模板:

      以下为弹飞绵羊代码:

      1 #define Troy
      2 
      3 #include "bits/stdc++.h"
      4 
      5 using namespace std;
      6 
      7 const int N=2e5+5;
      8 
      9 inline int read(){
     10     int s=0,k=1;char ch=getchar();
     11     while(ch<'0'|ch>'9')    ch=='-'?k=-1:0,ch=getchar();
     12     while(ch>47&ch<='9')    s=s*10+(ch^48),ch=getchar();
     13     return s*k;
     14 }
     15 
     16 #define size(t) (t?t->size:0)
     17 #define rev(t)  (t?t->rev^=1:0)
     18 
     19 struct Node{
     20     int size,rev;
     21     Node *fa,*son[2];
     22     Node(){fa=son[0]=son[1]=NULL;size=1,rev=0;}
     23     inline void update(){
     24         size=1+size(son[0])+size(son[1]);
     25     }
     26     inline void pushdown(){
     27         if(!rev)    return;
     28         rev=0,swap(son[0],son[1]);
     29         rev(son[0]),rev(son[1]);    
     30     }
     31 }*pool[N],*tmp[N],tree[N];int top;
     32 
     33 inline void init_pool(){for(;top<N;++top)   pool[top]=tree+top;}
     34 
     35 inline void newNode(Node *&p,Node *f){p=pool[--top];*p=Node();p->fa=f;}
     36 inline void freeNode(Node *&p){pool[top++]=p,p=NULL;}
     37 
     38 int to[N],n;
     39 class LinkCutTree{
     40 public:
     41     Node *node[N];
     42     inline void init(int n){for(register int i=1;i<=n;++i)newNode(node[i],NULL);}
     43     
     44     #define son(p)  (p->fa->son[1]==p)
     45     #define is_root(p)  ((!p->fa)||(p->fa->son[0]!=p&&p->fa->son[1]!=p))
     46 
     47     inline void rotate(Node *p){
     48         int a=son(p)^1;Node *f=p->fa;
     49         f->son[a^1]=p->son[a];
     50         if(p->son[a])   p->son[a]->fa=f;
     51         p->fa=f->fa;
     52         if(!is_root(f))   p->fa->son[son(f)]=p;
     53         f->fa=p,p->son[a]=f,f->update(),p->update();
     54     }
     55 
     56     inline void splay(Node *p){
     57         register int pos=0;
     58         for(Node *t=p;;t=t->fa){
     59             tmp[++pos]=t;
     60             if(is_root(t)) break;
     61         }
     62         for(;pos;--pos) tmp[pos]->pushdown();
     63         for(;!is_root(p);rotate(p))
     64             if(!is_root(p->fa)) rotate(son(p)==son(p->fa)?p->fa:p);
     65     }
     66 
     67     inline void access(Node *p){
     68         for(Node *pre=NULL;p;pre=p,p=p->fa)
     69             splay(p),p->son[1]=pre,p->update();
     70     }
     71 
     72     inline void make_root(Node *p){
     73         access(p),splay(p),rev(p);
     74     }
     75 
     76     inline void cut(Node *x,Node *y){
     77         make_root(x),access(y),splay(y);
     78         x->fa=y->son[0]=NULL;y->update();
     79     }
     80 
     81     inline void link(Node *x,Node *y){make_root(x);x->fa=y;}
     82     inline void link(int x,int y){node[x]->fa=node[y];}
     83 
     84     inline void op1(int x){
     85         make_root(node[n+1]),access(node[x]),splay(node[x]);
     86         printf("%d
    ",node[x]->size-1);
     87     }
     88 
     89     inline void op2(int x,int y){
     90         cut(node[x],node[min(n+1,x+to[x])]);
     91         link(node[x],node[min(n+1,y+x)]);to[x]=y;
     92     }
     93 }lct;
     94 
     95 int main(){
     96     n=read();
     97     init_pool();
     98     lct.init(n+1);
     99     register int i;
    100     for(i=1;i<=n;++i){
    101         to[i]=read();
    102         lct.link(i,min(i+to[i],n+1));
    103     }
    104     int q=read(),x,y;
    105     while(q--){
    106         if(read()==1)
    107             lct.op1(read()+1);
    108         else{
    109             x=read(),y=read();
    110             lct.op2(x+1,y);
    111         }
    112     }return 0;
    113 }
  • 相关阅读:
    27 树莓派做直播平台
    1 视频压缩软件
    (9-4 )deepsort在ubuntu1604下配置
    利用mvn进行多环境配置
    fidder及Charles使用
    轻松搞定面试中的二叉树题目(java&python)
    数据结构与算法系列 目录
    [转]揭秘webdriver实现原理
    作用域安全的构造函数
    JavaScript函数绑定
  • 原文地址:https://www.cnblogs.com/Troywar/p/8042524.html
Copyright © 2011-2022 走看看