最近在重写之前写过的一些板子,觉得之前树剖的码风太丑了,照着之前的代码重写了一份,结果发现样例的第二行输出 0。
#include<bits/stdc++.h>
#define int long long
#define mid ((l + r) >> 1)
#define lchild (root << 1)
#define rchild ((root << 1) + 1)
using namespace std;
const int N = 1e5 + 9;
int n,m,root,p;
int a[N];
struct edge{
int to,nex;
} e[N << 1];
int head[N],ecnt;
void addedge(int u,int v){
ecnt++;
e[ecnt] = (edge){v,head[u]};
head[u] = ecnt;
}
int fa[N],siz[N],dep[N];
int w_ch[N];
void dfs1(int u,int father){
fa[u] = father;
siz[u] = 1;
dep[u] = dep[father] + 1;
for(int i = head[u];i;i = e[i].nex){
int v = e[i].to;
if(v != father){
dfs1(v,u);
siz[u] += siz[v];
if(siz[v] > siz[w_ch[u]])
w_ch[u] = v;
}
}
}
int top[N];
int dfn[N],rdfn[N],id;
void dfs2(int u,int link_top){
id++;
dfn[u] = id;
rdfn[id] = u;
top[u] = link_top;
if(w_ch[u]){
dfs2(w_ch[u],link_top);
for(int i = head[u];i;i = e[i].nex){
int v = e[i].to;
if(v != fa[u] && v != w_ch[u])
dfs2(v,v);
}
}
}
struct node{
int sum;
int tag;//区间加标记
} t[N << 2];
int len(int l,int r){
return r - l + 1;
}
bool in_range(int l,int r,int L,int R){//[l,r]是否在修改/查询区间[L,R]中
return L <= l && r <= R;
}
bool out_range(int l,int r,int L,int R){//[l,r]是否与修改/查询区间[L,R]无交集
return l > R || r < L;
}
void push_up(int root){
t[root].sum = (t[lchild].sum + t[rchild].sum) % p;
}
void make_tag(int root,int len,int v){
(t[root].tag += v) %= p;
(t[root].sum += v * len) %= p;
}
void push_down(int root,int l,int r){
make_tag(lchild,len(l,mid),t[root].tag);
make_tag(rchild,len(mid + 1,r),t[root].tag);
t[root].tag = 0;
}
void build(int root,int l,int r){
if(l == r){//到达叶节点
t[root].sum = a[rdfn[l]] % p;
return;
}
build(lchild,l,mid);
build(rchild,mid + 1,r);
push_up(root);
}
//将区间 [L,R] 内的所有数加 v
void update(int root,int l,int r,int L,int R,int v){//root是当前节点,l、r是当前节点表示的区间,L、R是操作区间,v是区间加的值(下同)。
if(out_range(l,r,L,R))
return;
if(in_range(l,r,L,R)){
make_tag(root,len(l,r),v);
return;
}
push_down(root,l,r);
update(lchild,l,mid,L,R,v);
update(rchild,mid + 1,r,L,R,v);
push_up(root);
}
void update1(int u,int v,int w){//将路径u,v上的所有数加w
int l,r;
while(top[u] != top[v]){
if(dep[top[u]] < dep[top[v]])
swap(u,v);
l = dfn[top[u]],r = dfn[u];
update(1,1,n,l,r,w);
u = fa[top[u]];
}
if(dep[u] > dep[v])
swap(u,v);
l = dfn[u],r = dfn[v];
update(1,1,n,l,r,w);
}
void update2(int root,int w){//将以u为根的子树上的所有数加w
update(1,1,n,dfn[root],dfn[root] + siz[root] - 1,w);
}
//查询区间 [L,R] 中所有数的和
int query(int root,int l,int r,int L,int R){
if(out_range(l,r,L,R))
return 0;
if(in_range(l,r,L,R))
return t[root].sum;
push_down(root,l,r);
return (query(lchild,l,mid,L,R) + query(rchild,mid + 1,r,L,R)) % p;
}
int query1(int u,int v){//查询路径u,v的和
int l,r,ret = 0;
while(top[u] != top[v]){
if(dep[top[u]] < dep[top[v]])
swap(u,v);
l = dfn[top[u]],r = dfn[u];
(ret += query(1,1,n,l,r)) %= p;
u = fa[top[u]];
}
if(dep[u] > dep[v])
swap(u,v);
l = dfn[u],r = dfn[v];
(ret += query(1,1,n,l,r)) %= p;
return ret;
}
int query2(int root){//查询以root为根的子树的子树和
return query(1,1,n,dfn[root],dfn[root] + siz[root] - 1);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> n >> m >> root >> p;
for(int i = 1;i <= n;i++)
cin >> a[i];
for(int i = 1;i < n;i++){
int u,v;
cin >> u >> v;
addedge(u,v);
addedge(v,u);
}
dfs1(root,0);
dfs2(root,root);
// build(1,1,n);
while(m--){
int op;
cin >> op;
if(op == 1){
int u,v,w;
cin >> u >> v >> w;
update1(u,v,w % p);
}
else if(op == 2){
int u,v;
cout << query1(u,v) << '\n';
}
else if(op == 3){
int u,w;
cin >> u >> w;
update2(u,w % p);
}
else if(op == 4){
int u;
cin >> u;
cout << query2(u) << '\n';
}
}
return 0;
}