您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  动态树