BZOJ 1036: [ZJOI2008]树的统计Count

7 篇文章 0 订阅

题意就不说了,发现lct做这些题好像还挺方便的,也不长,挺好看的(黄学长说优美)。

不过好像比树剖慢了不少啊。。。

留个代码

#include
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
#include
       
       
         #include 
        
          #include 
         
           #include 
          
            #include 
           
             #define inf 1000000000 #define N 30010 using namespace std; typedef long long LL; int fa[N],n,m,c[N][2],st[N],a[N],b[N],mx[N],sum[N],w[N]; bool rev[N]; bool Rt(int x) { if(c[fa[x]][0]==x || c[fa[x]][1]==x)return 0; return 1; } void pushup(int x) { int l=c[x][0],r=c[x][1]; sum[x]=sum[l]+sum[r]+w[x]; mx[x]=max(max(mx[l],mx[r]),w[x]); } void pushdown(int x) { int l=c[x][0],r=c[x][1]; if(rev[x]) { rev[x]=0,rev[l]^=1,rev[r]^=1; swap(c[x][0],c[x][1]); } } void rotate(int x) { int y=fa[x],z=fa[y],a=c[y][1]==x,b=c[z][1]==y,g=c[x][!a]; if(!Rt(y))c[z][b]=x; fa[g]=y,c[y][a]=g; fa[y]=x,c[x][!a]=y; fa[x]=z; pushup(y); pushup(x); } void splay(int x) { int top=0,i; for(i=x;!Rt(i);i=fa[i])st[++top]=i; st[++top]=i; for(i=top;i;i--)pushdown(st[i]); while(!Rt(x)) { int y=fa[x],z=fa[y],a=c[y][1]==x,b=c[z][1]==y; if(!Rt(y)) { if(a==b)rotate(y); else rotate(x); } rotate(x); } } void access(int x) { int last=0; while(x) { splay(x); c[x][1]=last; pushup(x); last=x,x=fa[x]; } } void make_root(int x) { access(x); splay(x); rev[x]^=1; } void split(int x,int y) { make_root(x); access(y); splay(y); } void link(int x,int y) { make_root(x); fa[x]=y; } void cut(int x,int y) { split(x,y); fa[x]=c[y][0]=0; } int main() { int i,n,x,y,m; char ch[10]; mx[0]=-inf; scanf("%d",&n); for(i=1;i 
             
            
           
          
         
       
      
      
     
     
    
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值