BZOJ 2631: tree Link_Cut_Tree
2016-12-28 21:56
375 查看
一道Link_Cut_Tree裸题,只要998,模板带回家
#include<cstdio> #include<cstdlib> #include<iostream> #include<iomanip> #include<cstring> #include<string> #include<ctime> #include<cmath> #include<algorithm> using namespace std; #define int unsigned int const int mod=51061; struct splay { splay *fa,*ls,*rs; int v,sum; int add_mark; bool rev_mark; int cheng_mark; int size; splay(); void rev(); void add(int val); void push_up(); void push_down(); void cheng(int val); }*null=new splay(),mempool[1000000]; splay :: splay() { size=null? 1 :0; fa=ls=rs=null; v=add_mark=sum=0; cheng_mark=1; } void splay :: add(int val) { val=val%mod; v=(val+v)%mod; sum=(sum+val*size)%mod; add_mark=(add_mark+val)%mod; } void splay :: rev() { swap(ls,rs); rev_mark^=1; } void splay :: cheng(int val) { val=val%mod; sum=(sum*val)%mod; v=(v*val)%mod; cheng_mark=(cheng_mark*val)%mod; add_mark=(add_mark*val)%mod; } void splay :: push_down() { if(cheng_mark!=1) { if(ls!=null) ls->cheng(cheng_mark); if(rs!=null) rs->cheng(cheng_mark); cheng_mark=1; } if(add_mark) { if(ls!=null) ls->add(add_mark); if(rs!=null) rs->add(add_mark); add_mark=0; } if(rev_mark) { if(ls!=null) ls->rev(); if(rs!=null) rs->rev(); rev_mark=0; } } void splay :: push_up() { size=(ls->size+rs->size+1)%mod; sum=(ls->sum+rs->sum+v)%mod; } void right(splay *x) { splay *y=x->fa; y->ls=x->rs; x->rs->fa=y; x->fa=y->fa; x->rs=y; if(y==y->fa->ls) y->fa->ls=x; else if(y==y->fa->rs) y->fa->rs=x; y->fa=x; y->push_up(); } void left(splay *x) { splay *y=x->fa; y->rs=x->ls; x->ls->fa=y; x->fa=y->fa; x->ls=y; if(y==y->fa->ls) y->fa->ls=x; else if(y==y->fa->rs) y->fa->rs=x; y->fa=x; y->push_up(); } void push_down(splay *x) { if(x==x->fa->ls || x==x->fa->rs) push_down(x->fa); x->push_down(); } void splaying(splay *x) { push_down(x); while(1) { splay *y=x->fa; splay *z=y->fa; if(x!=y->ls && x!=y->rs) break; if(y!=z->ls && y!=z->rs) { if(x==y->ls) right(x); else if(x==y->rs) left(x); break; } if(x==y->ls) { if(y==z->ls) right(y); right(x); } else if(x==y->rs) { if(y==z->rs) left(y); left(x); } } x->push_up(); } void access(splay *x) { splay *y=null; while(x!=null) { splaying(x); x->rs=y; x->push_up(); y=x; x=x->fa; } } void move_to_root(splay *x) { access(x); splaying(x); x->rev(); } void link(splay *x,splay *y) { move_to_root(x); x->fa=y; } void cut(splay *x,splay *y) { move_to_root(x); access(y); splaying(y); y->ls=null; y->push_up(); x->fa=null; } void jia() { int x,y,val; scanf("%d%d%d",&x,&y,&val); move_to_root(&mempool[x]); access(&mempool[y]); splaying(&mempool[y]); mempool[y].add(val); } void jian() { int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); cut(&mempool[x1],&mempool[y1]); link(&mempool[x2],&mempool[y2]); } void cheng() { int x,y,val; scanf("%d%d%d",&x,&y,&val); move_to_root(&mempool[x]); access(&mempool[y]); splaying(&mempool[y]); mempool[y].cheng(val); } void chu() { int x,y; scanf("%d%d",&x,&y); move_to_root(&mempool[x]); access(&mempool[y]); splaying(&mempool[y]); printf("%d\n",mempool[y].sum); } struct bian { int l,r; }a[1000000]; int tot=0; int fir[1000000]; int nex[1000000]; void add_edge(int l,int r) { a[++tot].l=l; a[tot].r=r; nex[tot]=fir[l]; fir[l]=tot; } int fa[1000000]; void dfs(int u,int fro) { fa[u]=fro; for(int o=fir[u];o!=0;o=nex[o]) { if(a[o].r!=fro) dfs(a[o].r,u); } } main() { int n,q; scanf("%d%d",&n,&q); for(int i=1;i<n;i++) { int l,r; scanf("%d%d",&l,&r); add_edge(l,r); add_edge(r,l); } dfs(1,0); for(int i=1;i<=n;i++) { mempool[i].v=mempool[i].sum=1; if(fa[i]) mempool[i].fa=&mempool[fa[i]]; } for(int i=1;i<=q;i++) { char s[5]; scanf("%s",s); if(s[0]=='+') jia(); if(s[0]=='-') jian(); if(s[0]=='*') cheng(); if(s[0]=='/') chu(); } return 0; }
相关文章推荐
- bzoj 2631: tree link-cut-tree
- BZOJ2631: tree link-cut-tree
- BZOJ-2631 tree Link-Cut-Tree
- Link-Cut-Tree:【BZOJ2631】Tree
- BZOJ 2631 tree 动态树(Link-Cut-Tree)
- BZOJ2631: tree Link-cut-tree
- [BZOJ 1036] [ZJOI2008] 树的统计Count 【Link Cut Tree】
- BZOJ 2002 弹飞绵羊 Link-Cut-Tree(LCT)
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测 (link-cut-tree)
- BZOJ 2631 Tree ——Link-Cut Tree
- 【BZOJ2594】 Wc2006 水管局长数据加强版 Link-Cut-Tree
- BZOJ 2759 一个动态树好题 Link-Cut-Tree+扩展欧几里得
- LinkCutTree试水之bzoj2049[Sdoi08]洞穴勘测题解
- BZOJ 3306 树 Link-Cut-Tree+set
- 【BZOJ2049】[Sdoi2008]Cave 洞穴勘测 Link-Cut-Tree
- BZOJ2157【Link Cut Tree】
- bzoj 2555(linkcuttree+后缀自动机)
- [bzoj2049][Sdoi2008]Cave 洞穴勘测 Link-Cut-Tree
- [bzoj2843]极地旅行社 Link-Cut-Tree
- 【bzoj2049】[Sdoi2008]Cave 洞穴勘测 link-cut-tree