1. 程式人生 > >樹剖模板(點權)(洛谷P3384)

樹剖模板(點權)(洛谷P3384)

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100000+10;
int N,M,R,P,w[maxn];

struct
{
    int to,next;    
}e[maxn<<1];

int head[maxn],edgeNum;
void add(int u,int v)
{
    e[edgeNum].next = head[u];
    e[edgeNum].to = v;
    head[u] = edgeNum++;
}

/*-------------------------樹剖------------------------------*/
int deep[maxn],fa[maxn],siz[maxn],son[maxn]; void dfs1(int u,int pre,int d) { deep[u] = d; fa[u] = pre; siz[u] = 1; son[u] = 0; for(int i=head[u];~i;i=e[i].next) { int v = e[i].to; if(v!=pre) { dfs1(v,u,d+1); siz[u] += siz[v]; if
(siz[v]>siz[son[u]]) son[u] = v; } } } int top[maxn],id[maxn],rk[maxn],cnt; int &n = cnt; void dfs2(int u,int t) { top[u] = t; id[u] = ++cnt; rk[cnt] = u; if(!son[u]) return; dfs2(son[u],t); for(int i=head[u];~i;i=e[i].next) { int
v = e[i].to; if(v!=son[u]&&v!=fa[u]) dfs2(v,v); } } /*-------------------------樹剖------------------------------*/ /*-------------------------線段樹------------------------------*/ #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 int sum[maxn<<2],lazy[maxn<<2]; void pushup(int rt) { sum[rt] = (sum[rt<<1]%P + sum[rt<<1|1]%P) % P; } void build(int l,int r,int rt) { if(l==r) { sum[rt] = w[rk[l]]%P; return ; } int m = l+r>>1; build(lson); build(rson); pushup(rt); } void pushdown(int rt,int l,int r) { if(lazy[rt]) { lazy[rt<<1] = (lazy[rt<<1] + lazy[rt]) % P; lazy[rt<<1|1] = (lazy[rt<<1|1] + lazy[rt]) % P; sum[rt<<1] += (lazy[rt] * l)%P; sum[rt<<1] %= P; sum[rt<<1|1] += (lazy[rt] * r)%P; sum[rt<<1|1] %= P; lazy[rt] = 0; } } void update(int L,int R,int val,int l,int r,int rt) { if(L<=l&&r<=R) { sum[rt] = (sum[rt]%P + (val%P) * ((r-l+1)%P)) % P; lazy[rt] += val%P; return ; } int m = l + r >> 1; pushdown(rt,m-l+1,r-m); if(L<=m) update(L,R,val,lson); if(R>m) update(L,R,val,rson); pushup(rt); } int query(int L,int R,int l,int r,int rt) { if(L<=l&&r<=R) return sum[rt]; int m = l + r >> 1,ans = 0; pushdown(rt,m-l+1,r-m); if(L<=m) ans = (ans%P + query(L,R,lson)%P) % P; if(R>m) ans = (ans%P + query(L,R,rson)%P) % P; return ans%P; } /*-------------------------線段樹------------------------------*/ /*-----------------------樹剖加線段樹--------------------------*/ void update(int x,int y,int z) { z %= P; while(top[x]!=top[y]) { if(deep[top[x]]<deep[top[y]]) swap(x,y); update(id[top[x]],id[x],z,1,n,1); x = fa[top[x]]; } if(deep[x]>deep[y]) swap(x,y); update(id[x],id[y],z,1,n,1); } int query(int x,int y) { int ans = 0; while(top[x] != top[y]) { if(deep[top[x]] < deep[top[y]]) swap(x,y); ans = (ans%P + query(id[top[x]],id[x],1,n,1)%P) % P; x = fa[top[x]]; } if(deep[x]>deep[y]) swap(x,y); ans = (ans%P + query(id[x],id[y],1,n,1)%P) % P; return ans%P; } /*-----------------------樹剖加線段樹--------------------------*/ void init() { memset(head,-1,4*N+4); cnt = edgeNum = 0; } int u,v,x,y,z,op; int main() { scanf("%d%d%d%d",&N,&M,&R,&P); init(); for(int i=1;i<=N;++i) scanf("%d",&w[i]); for(int i=1;i<N;++i) { scanf("%d%d",&u,&v); add(u,v); add(v,u); } dfs1(R,0,0); dfs2(R,R); build(1,n,1); while(M--) { scanf("%d%d",&op,&x); if(op==1) { scanf("%d%d",&y,&z); update(x,y,z); } else if(op==2) { scanf("%d",&y); printf("%d\n",query(x,y)%P); } else if(op==3) { scanf("%d",&z); update(id[x],id[x]+siz[x]-1,z,1,n,1); } else printf("%d\n",query(id[x],id[x]+siz[x]-1,1,n,1)); } return 0; }