HDU4010 Query on The Trees LCT
来源:互联网 发布:nodejs 搭配nginx 编辑:程序博客网 时间:2024/05/19 10:42
LCT模板题
代码
#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>#define L(i) (T[i].s[0])#define R(i) (T[i].s[1])#define F(i) (T[i].fa)#define V(i) (T[i].v)#define Loc(i) (R(F(i))==i)#define For(i,j,k) for(register int i=(j);i<=(int)k;i++)#define Forr(i,j,k) for(register int i=(j);i>=(int)k;i--)#define Set(a,b) memset(a,b,sizeof(a))using namespace std;const int N=300010;int Begin[N],Next[N],to[N],e,n,m;struct node{ int s[2],fa,rev,mx,v,add;};struct LCT{ node T[N]; inline void clear(){Set(T,0);} inline void pushup(int i){ T[i].mx=max(T[L(i)].mx,T[R(i)].mx); T[i].mx=max(T[i].mx,T[i].v); } inline void inc(int i,int val){ T[i].mx+=val,T[i].v+=val,T[i].add+=val; } inline void pushdown(int i){ if(T[i].rev){ T[i].rev=0,T[L(i)].rev^=1,T[R(i)].rev^=1; swap(L(i),R(i)); } if(T[i].add){ int d=T[i].add; if(L(i))inc(L(i),d); if(R(i))inc(R(i),d); T[i].add=0; } } inline bool isrt(int x){ return R(F(x))!=x&&L(F(x))!=x; } inline void Pushdown(int x){ if(!isrt(x))Pushdown(F(x)); pushdown(x); } inline void Rotate(int x){ int A=F(x),B=F(A),l=Loc(x),r=l^1,d=Loc(A); if(!isrt(A))T[B].s[d]=x;F(x)=B; F(A)=x,F(T[x].s[r])=A,T[A].s[l]=T[x].s[r],T[x].s[r]=A; pushup(A),pushup(x); } inline void splay(int x){ Pushdown(x); while(!isrt(x)){ if(!isrt(F(x)))Rotate(x); Rotate(x); } } inline void access(int x){ for(int i=0;x;i=x,x=F(x)) splay(x),R(x)=i,pushup(x); } inline void reverse(int x){ access(x);splay(x);T[x].rev^=1; } inline int findrt(int x){ access(x);splay(x); while(L(x))x=L(x); return x; } inline int link(int x,int y){ if(findrt(x)==findrt(y))return -1; reverse(x),F(x)=y; return 0; } inline int cut(int x,int y){ if(findrt(x)!=findrt(y)||x==y)return -1; reverse(x),access(y),splay(y),F(L(y))=0,L(y)=0;pushup(y); return 0; } inline int add(int x,int y,int val){ if(findrt(x)!=findrt(y))return -1; reverse(x),access(y),splay(y); inc(y,val); return 0; } inline int query(int x,int y){ if(findrt(x)!=findrt(y))return -1; reverse(x),access(y),splay(y); return T[y].mx; }}t;inline void adde(int x,int y){ to[++e]=y;Next[e]=Begin[x];Begin[x]=e;}inline void read(int &x){ x=0;char c=getchar();int f=(c=='-'); while(c<'0'||c>'9')c=getchar(),f|=(c=='-'); while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar(); if(f)x=-x;}void dfs(int u,int fa){ for(int i=Begin[u];i;i=Next[i]){ int v=to[i]; if(v==fa)continue; t.T[v].fa=u; dfs(v,u); }}int main(){ while(scanf("%d",&n)!=EOF){ t.clear();Set(Begin,0);e=0; For(i,1,n-1){ int x,y; read(x),read(y); adde(x,y),adde(y,x); } dfs(1,0); For(i,1,n){ int v; read(v); t.T[i].v=t.T[i].mx=v; } read(m); while(m--){ int op,u,v,w; read(op);read(u),read(v); if(op==1){ if(t.link(u,v)==-1)puts("-1"); }else if(op==2){ if(t.cut(u,v)==-1)puts("-1"); }else if(op==3){ read(w); if(t.add(v,w,u)==-1)puts("-1"); }else printf("%d\n",t.query(u,v)); } puts(""); } return 0;//}
0 0
- [HDU4010]Query on The Trees && LCT
- LCT - hdu4010 Query on The Trees
- HDU4010 Query on The Trees LCT
- hdu4010:Query on The Trees
- 【hdu4010】Query on The Trees
- [HDU4010]Query on The Trees-动态树LCT(Link Cut Tree)
- 动态树hdu4010 - Query on The Trees
- 【hdu4010】【link-cut tree】Query on The Trees
- 动态树不完整模板---hdu4010 Query on The Trees
- HDOJ 4010 Query on The Trees LCT
- HDU-4010 Query on The Trees(LCT)
- Hdu 4010 Query on The Trees lct link-cut tree
- hdu 4010 Query on The Trees (lct模板)
- HDOJ 4010 Query on The Trees(LCT动态树)
- HDU 4010 Query on The Trees 点权LCT
- Query on The Trees
- 【HDU】4010 Query on The Trees 动态树之Link Cut Tree(LCT)
- HUD4010 Query on The Trees
- Ubuntu下局域网拷贝文件
- c# json
- Android Services之启动
- QQ18年,解密8亿月活的QQ后台服务接口隔离技术
- 启动安卓中的定时功能
- HDU4010 Query on The Trees LCT
- 3.不常关注的java点
- spring源码初步探索
- URAL1553 Caves and Tunnels
- React核心内容归纳总结
- struts2
- Redis五种数据类型介绍
- 广度优先搜索——01迷宫
- 嵌入式Linux学习:u-boot源码分析(7)--AM335X系列的2014.10版