社区讨论
求助!!!实在不知道错哪!谢谢各位大佬!!!
P3384【模板】重链剖分 / 树链剖分参与者 5已保存回复 9
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 9 条
- 当前快照
- 1 份
- 快照标识符
- @mi85w4gz
- 此快照首次捕获于
- 2025/11/21 09:08 4 个月前
- 此快照最后确认于
- 2025/11/21 09:08 4 个月前
CPP
#include<iostream>
#include<cstdio>
#define MA 100005
#define L root<<1
#define R root<<1|1
using namespace std;
int n,m,r,p;
int nex[MA*2],fis[MA*2],go[MA*2],num[MA],tot;
int fa[MA],size[MA],dep[MA],son[MA],top[MA],cnt,seg[MA],rev[MA];
int t[MA],ad[MA];
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();};
while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();};;
return x*f;
}
////////////////////
inline void add(int u,int v){
nex[++tot]=fis[u];
fis[u]=tot;
go[tot]=v;
return ;
}
inline void dfs1(int u,int f){
size[u]=1;
fa[u]=f;
dep[u]=dep[f]+1;
for(int e=fis[u];e;e=nex[e]){
int v=go[e];
if(v!=f){
dfs1(v,u);
size[u]+=size[v];
if(size[v]>size[son[u]]){
son[u]=v;
}
}
}
return ;
}
inline void dfs2(int u,int f){
seg[u]=++cnt;
rev[cnt]=num[u];
top[u]=f;
if(!son[u]){
return ;
}
dfs2(son[u],f);
for(int e=fis[u];e;e=nex[e]){
int v=go[e];
if(v==fa[u]||v==son[u]){
continue;
}
dfs2(v,v);
}
return ;
}
/////////////////
inline void build(int l,int r,int root){
if(l==r){
t[root]=rev[l]%p;
return ;
}
int mid=(l+r)>>1;
build(l,mid,L);
build(mid+1,r,R);
t[root]=(t[L]+t[R])%p;
return ;
}
inline void pushdown(int l,int r,int root){
int mid=(l+r)>>1;
ad[L]=(ad[L]+ad[root])%p;
ad[R]=(ad[R]+ad[root])%p;
t[L]=(t[L]+(mid-l+1)*ad[root])%p;
t[R]=(t[R]+(r-mid)*t[root])%p;
ad[root]=0;
return ;
}
inline void add(int l,int r,int root,int x,int y,int k){
if(x<=l&&y>=r){
ad[root]=(ad[root]+k)%p;
t[root]=(t[root]+(r-l+1)*k)%p;
return ;
}
if(ad[root]){
pushdown(l,r,root);
}
int mid=(l+r)>>1;
if(x<=mid){
add(l,mid,L,x,y,k);
}
if(y>mid){
add(mid+1,r,R,x,y,k);
}
t[root]=(t[L]+t[R])%p;
return ;
}
inline int que(int l,int r,int root,int x,int y){
if(x<=l&&y>=r){
return t[root]%p;
}
int ans=0;
int mid=(l+r)>>1;
if(ad[root]){
pushdown(l,r,root);
}
if(x<=mid){
ans+=que(l,mid,L,x,y);
}
if(y>mid){
ans+=que(mid+1,r,R,x,y);
}
return ans%p;
}
///////////////
inline void updata(int x,int y,int k){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]){
swap(x,y);
}
add(1,n,1,seg[top[x]],seg[x],k);
x=fa[top[x]];
}
if(dep[top[x]]<dep[top[y]]){
swap(x,y);
}
add(1,n,1,seg[top[x]],seg[x],k);
return ;
}
inline int ask(int x,int y){
int ans=0;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]){
swap(x,y);
}
ans=(ans+que(1,n,1,seg[top[x]],seg[x]))%p;
x=fa[top[x]];
}
if(dep[top[x]]<dep[top[y]]){
swap(x,y);
}
ans=(ans+que(1,n,1,seg[top[x]],seg[x]))%p;
return ans%p;
}
inline void updata2(int x,int k){
add(1,n,1,seg[x],seg[x]+size[x]-1,k);
return ;
}
inline int ask2(int x){
return que(1,n,1,seg[x],seg[x]+size[x]-1);
}
//////////////
int main(){
int i;
int k,x,y,z;
n=read();m=read();
r=read();p=read();
for(i=1;i<=n;i++){
num[i]=read();
}
for(i=1;i<n;i++){
x=read();y=read();
add(x,y);
add(y,x);
}
dfs1(r,0);
dfs2(r,r);
build(1,n,1);
for(i=1;i<=m;i++){
k=read();
if(k==1){
x=read();y=read();z=read();
updata(x,y,z);
}else{
if(k==2){
x=read();y=read();
printf("%d\n",ask(x,y));
}else{
if(k==3){
x=read();z=read();
updata2(x,z);
}else{
x=read();
printf("%d\n",ask2(x));
}
}
}
}
return 0;
}
回复
共 9 条回复,欢迎继续交流。
正在加载回复...