AC日记——【模板】树链剖分 洛谷 P3384
题目描述
如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作:
操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节点的值都加上z
操作2: 格式: 2 x y 表示求树从x到y结点最短路径上所有节点的值之和
操作3: 格式: 3 x z 表示将以x为根节点的子树内所有节点值都加上z
操作4: 格式: 4 x 表示求以x为根节点的子树内所有节点值之和
输入输出格式
输入格式:
第一行包含4个正整数N、M、R、P,分别表示树的结点个数、操作个数、根节点序号和取模数(即所有的输出结果均对此取模)。
接下来一行包含N个非负整数,分别依次表示各个节点上初始的数值。
接下来N-1行每行包含两个整数x、y,表示点x和点y之间连有一条边(保证无环且连通)
接下来M行每行包含若干个正整数,每行表示一个操作,格式如下:
操作1: 1 x y z
操作2: 2 x y
操作3: 3 x z
操作4: 4 x
输出格式:
输出包含若干行,分别依次表示每个操作2或操作4所得的结果(对P取模)
输入输出样例
输入样例#1:
5 5 2 24 7 3 7 8 0 1 2 1 5 3 1 4 1 3 4 2 3 2 2 4 5 1 5 1 3 2 1 3
输出样例#1:
2 21
说明
时空限制:1s,128M
数据规模:
对于30%的数据:N<=10,M<=10
对于70%的数据:N<=1000,M<=1000
对于100%的数据:N<=100000,M<=100000
(其实,纯随机生成的树LCA+暴力是能过的,可是,你觉得可能是纯随机的么233)
样例说明:
树的结构如下:
各个操作如下:
故输出应依次为2、21(重要的事情说三遍:记得取模)
思路:
裸树剖;
来,上代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define maxn 100001 #define LL long long int using namespace std; struct EdgeType { LL to,next; }; struct EdgeType edge[maxn<<1]; struct TreeNodeType { LL l,r,dis,mid,flag; }; struct TreeNodeType tree[maxn<<2]; LL if_z,tree_num,tree_dis[maxn],deep[maxn],cnt; LL f[maxn],n,m,s,p,dis[maxn],flag[maxn],Enum; LL size[maxn],end[maxn],belong[maxn],head[maxn]; char Cget; inline void read_int(LL &now) { now=0,if_z=1,Cget=getchar(); while(Cget>'9'||Cget<'0') { if(Cget=='-') if_z=-1; Cget=getchar(); } while(Cget>='0'&&Cget<='9') { now=now*10+Cget-'0'; Cget=getchar(); } now*=if_z; } inline void edge_add(LL from,LL to) { edge[++Enum].to=from,edge[Enum].next=head[to],head[to]=Enum; edge[++Enum].to=to,edge[Enum].next=head[from],head[from]=Enum; } inline void tree_up(LL now) { tree[now].dis=tree[now<<1].dis+tree[now<<1|1].dis; } void tree_build(LL now,LL l,LL r) { tree[now].l=l,tree[now].r=r; if(l==r) { tree[now].dis=tree_dis[++tree_num]; return ; } tree[now].mid=(tree[now].l+tree[now].r)>>1; tree_build(now<<1,l,tree[now].mid); tree_build(now<<1|1,tree[now].mid+1,r); tree_up(now); } inline void tree_down(LL now) { if(tree[now].l==tree[now].r) return ; tree[now<<1].dis+=(tree[now<<1].r-tree[now<<1].l+1)*tree[now].flag; tree[now<<1].flag+=tree[now].flag; tree[now<<1|1].dis+=(tree[now<<1|1].r-tree[now<<1|1].l+1)*tree[now].flag; tree[now<<1|1].flag+=tree[now].flag; tree[now].flag=0; } void tree_change(LL now,LL l,LL r,LL x) { if(tree[now].l==l&&tree[now].r==r) { tree[now].dis+=(r-l+1)*x; tree[now].flag+=x; return ; } if(tree[now].flag) tree_down(now); if(l>tree[now].mid) tree_change(now<<1|1,l,r,x); else if(r<=tree[now].mid) tree_change(now<<1,l,r,x); else { tree_change(now<<1,l,tree[now].mid,x); tree_change(now<<1|1,tree[now].mid+1,r,x); } tree_up(now); } LL tree_query(LL now,LL l,LL r) { if(tree[now].l==l&&tree[now].r==r) { return tree[now].dis; } if(tree[now].flag) tree_down(now); tree_up(now); if(l>tree[now].mid) return tree_query(now<<1|1,l,r); else if(r<=tree[now].mid) return tree_query(now<<1,l,r); else return tree_query(now<<1,l,tree[now].mid)+tree_query(now<<1|1,tree[now].mid+1,r); } void search(LL now,LL fa) { LL pos=cnt++; deep[now]=deep[fa]+1,f[now]=fa; for(LL i=head[now];i;i=edge[i].next) { if(edge[i].to==fa) continue; search(edge[i].to,now); } size[now]=cnt-pos; } void search_(LL now,LL chain) { belong[now]=chain,flag[now]=++cnt; tree_dis[flag[now]]=dis[now]; LL pos=0; for(LL i=head[now];i;i=edge[i].next) { if(flag[edge[i].to]!=0) continue; if(size[edge[i].to]>size[pos]) pos=edge[i].to; } if(pos!=0) search_(pos,chain); for(LL i=head[now];i;i=edge[i].next) { if(flag[edge[i].to]!=0) continue; search_(edge[i].to,edge[i].to); } end[now]=cnt; } inline void solve_change(LL x,LL y,LL z) { while(belong[x]!=belong[y]) { if(deep[belong[x]]<deep[belong[y]]) swap(x,y); tree_change(1,flag[belong[x]],flag[x],z); x=f[belong[x]]; } if(deep[x]<deep[y]) swap(x,y); tree_change(1,flag[y],flag[x],z); } inline LL solve_query(LL x,LL y) { LL ans=0; while(belong[x]!=belong[y]) { if(deep[belong[x]]<deep[belong[y]]) swap(x,y); ans=(ans+tree_query(1,flag[belong[x]],flag[x]))%p; x=f[belong[x]]; } if(deep[x]<deep[y]) swap(x,y); ans=(ans+tree_query(1,flag[y],flag[x]))%p; return ans; } int main() { read_int(n),read_int(m),read_int(s),read_int(p); for(LL i=1;i<=n;i++) read_int(dis[i]); LL type,from,to,cur; for(LL i=1;i<n;i++) { read_int(from),read_int(to); edge_add(from,to); } search(s,0),cnt=0,search_(s,s); cnt=0,tree_build(1,1,n); for(LL i=1;i<=m;i++) { read_int(type); if(type==1) { read_int(from),read_int(to),read_int(cur); solve_change(from,to,cur); } if(type==2) { read_int(from),read_int(to); printf("%d ",solve_query(from,to)%p); } if(type==3) { read_int(from),read_int(to); tree_change(1,flag[from],end[from],to); } if(type==4) { read_int(from); printf("%d ",tree_query(1,flag[from],end[from])%p); } } return 0; }