洛谷3384 树链剖分(模板)

我 服 我 自 己

【题目分析】

板子题调一天系列。。。。qwq已经被蠢死。

路径操作直接上树链剖分,子树操作直接在线段树上区间修改询问(因为x及其子树的dfs序为dfn[x]~dfn[x]+siz[x]-1)

然后就是一群人对着这份代码看了半天没挑出错来。。。。

【代码~】

#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+10;
const int MAXM=2e5+10;

int n,q,mod,rt,cnt;
int head[MAXN],depth[MAXN],fa[MAXN],son[MAXN],siz[MAXN],top[MAXN];
int nxt[MAXM],to[MAXM];
int dfn[MAXN],ys[MAXN],tot;
int a[MAXN];
struct Tree{
	int l,r;
	int sum,add;
}tr[MAXN<<2];

int Read(){
	int i=0,f=1;
	char c;
	for(c=getchar();(c>'9'||c<'0')&&c!='-';c=getchar());
	if(c=='-')
	  f=-1,c=getchar();
	for(;c>='0'&&c<='9';c=getchar())
	  i=(i<<3)+(i<<1)+c-'0';
	return i*f;
}

void add(int x,int y){
	nxt[cnt]=head[x];
	head[x]=cnt;
	to[cnt]=y;
	cnt++;
}

void dfs1(int u,int f){
	siz[u]=1;
	for(int i=head[u];i!=-1;i=nxt[i]){
		int v=to[i];
		if(v==f)
		  continue;
		fa[v]=u,depth[v]=depth[u]+1;
		dfs1(v,u);
		siz[u]+=siz[v];
		if(siz[v]>siz[son[u]])
		  son[u]=v;
	}
}

void dfs2(int u,int tp){
	top[u]=tp;
	dfn[u]=++tot;
	ys[tot]=u;
	if(!son[u])
	  return ;
	dfs2(son[u],tp);
	for(int i=head[u];i!=-1;i=nxt[i]){
		int v=to[i];
		if(v==fa[u]||v==son[u])
		  continue;
		dfs2(v,v);
	}
}

void push_up(int root){
	tr[root].sum=(tr[root<<1].sum+tr[root<<1|1].sum)%mod;
}

void push_now(int root,int v){
	tr[root].add=(tr[root].add+v)%mod;
	tr[root].sum=(tr[root].sum+v*(tr[root].r-tr[root].l+1))%mod;
}

void push_down(int root){
	if(tr[root].add){
		push_now(root<<1,tr[root].add);
		push_now(root<<1|1,tr[root].add);
		tr[root].add=0;
	}
}

void build(int root,int l,int r){
	tr[root].l=l,tr[root].r=r;
	if(l==r){
		tr[root].sum=a[ys[l]];
		return ;
	}
	int mid=l+r>>1;
	build(root<<1,l,mid);
	build(root<<1|1,mid+1,r);
	push_up(root);
}

void update(int root,int l,int r,int L,int R,int key){
	if(l>R||r<L)
	  return ;
	if(L<=l&&r<=R){
		push_now(root,key);
		return ;
	}
	push_down(root);
	int mid=l+r>>1;
	if(R<=mid)
	  update(root<<1,l,mid,L,R,key);
	else{
		if(L>mid)
		  update(root<<1|1,mid+1,r,L,R,key);
		else
		  update(root<<1,l,mid,L,mid,key),update(root<<1|1,mid+1,r,mid+1,R,key);
	}
	push_up(root);
}

int query(int root,int l,int r,int L,int R){
	if(l>R||r<L)
	  return 0;
	if(L<=l&&r<=R)
	  return tr[root].sum;
	push_down(root);
	int mid=l+r>>1;
	if(R<=mid)
	  return query(root<<1,l,mid,L,R);
	else{
		if(L>mid)
		  return query(root<<1|1,mid+1,r,L,R);
		else
		  return query(root<<1,l,mid,L,mid)+query(root<<1|1,mid+1,r,mid+1,R);
	}
}

void updatepath(int x,int y,int key){
	while(top[x]!=top[y]){
		if(depth[top[x]]<depth[top[y]])
		  swap(x,y);
		update(1,1,n,dfn[top[x]],dfn[x],key);
		x=fa[top[x]];
	}
	if(depth[x]>depth[y])
	  swap(x,y);
	update(1,1,n,dfn[x],dfn[y],key);
}

void updateson(int x,int key){
	update(1,1,n,dfn[x],dfn[x]+siz[x]-1,key);
}

int querypath(int x,int y){
	int ret=0;
	while(top[x]!=top[y]){
		if(depth[top[x]]<depth[top[y]])
		  swap(x,y);
		ret=(ret+query(1,1,n,dfn[top[x]],dfn[x]))%mod;
		x=fa[top[x]];
	}
	if(depth[x]>depth[y])
	  swap(x,y);
	return (ret+query(1,1,n,dfn[x],dfn[y]))%mod;
}

int queryson(int x){
	return query(1,1,n,dfn[x],dfn[x]+siz[x]-1)%mod;
}

int main(){
	memset(head,-1,sizeof(head));
	n=Read(),q=Read(),rt=Read(),mod=Read();
	for(int i=1;i<=n;++i)
	  a[i]=Read();
	for(int i=1;i<n;++i){
		int x=Read(),y=Read();
		add(x,y),add(y,x);
	}
	dfs1(rt,-1);
	dfs2(rt,rt);
	build(1,1,n);
	while(q--){
		int cz=Read();
		if(cz==1){
			int x=Read(),y=Read(),k=Read();
			updatepath(x,y,k);
		}
		if(cz==2){
			int x=Read(),y=Read();
			cout<<querypath(x,y)<<'\n';
		}
		if(cz==3){
			int x=Read(),k=Read();
			updateson(x,k);
		}
		if(cz==4){
			int x=Read();
			cout<<queryson(x)<<'\n';
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/g21glf/article/details/83505920
今日推荐