注意事项:
- 线段树中 \(modify(),query()\) 中 \(>=,<=,>,<\) 以及\(l,r,L,R\)
#include<bits/stdc++.h>
#define ls p<<1
#define rs p<<1|1
#define lson ls,l,mid
#define rson rs,mid+1,r
#define root 1,1,n
using namespace std;
const int N = 1e5+9;
int n,m,s,mo,a[N];
int h[N],ver[N<<1],ne[N<<1],idx;
void add(int u,int v)
{
ver[++idx] = v,ne[idx] = h[u],h[u] = idx;
return;
}
int fa[N],hson[N],dep[N],siz[N];
void dfs1(int u,int pre)
{
fa[u] = pre,dep[u] = dep[pre]+1,siz[u]++;
for(int i = h[u];i;i = ne[i])
{
int v = ver[i];
if(v == pre)continue;
dfs1(v,u);
siz[u] += siz[v];
if(siz[hson[u]] < siz[v])hson[u] = v;
}
return;
}
int top[N],dfn[N],rnk[N],tim;
void dfs2(int u,int tp)
{
dfn[u] = ++tim,rnk[tim] = u,top[u] = tp;
if(!hson[u])return;
dfs2(hson[u],tp);
for(int i = h[u];i;i = ne[i])
{
int v = ver[i];
if(v == fa[u] || v == hson[u])continue;
dfs2(v,v);
}
return;
}
struct tree
{
int l,r,sum,add;
tree()
{
l = r = sum = add = 0;
return;
}
void col(int v)
{
sum = (1ll*sum+1ll*v*(r-l+1)%mo)%mo;
add = (1ll*add+v)%mo;
return;
}
}t[N<<2];
tree operator +(const tree &l,const tree &r)
{
tree p;
p.l = l.l,p.r = r.r,p.sum = (1ll*l.sum+r.sum)%mo,p.add = 0;
return p;
}
void pushup(int p)
{
t[p] = t[ls]+t[rs];
return;
}
void pushdown(int p)
{
int v = t[p].add;
if(v)
{
t[ls].col(v);
t[rs].col(v);
t[p].add = 0;
}
return;
}
void build(int p,int l,int r)
{
if(l == r)
{
t[p].l = t[p].r = l;
t[p].sum = a[rnk[l]];
return;
}
int mid = l+r>>1;
build(lson);
build(rson);
pushup(p);
return;
}
void modify(int p,int l,int r,int L,int R,int v)
{
if(L <= l && R >= r)
{
t[p].col(v);
return;
}
pushdown(p);
int mid = l+r>>1;
if(L <= mid)modify(lson,L,R,v);
if(R > mid)modify(rson,L,R,v);
pushup(p);
return;
}
tree query(int p,int l,int r,int L,int R)
{
if(L <= l && R >= r)return t[p];
pushdown(p);
int mid = l+r>>1;
if(R <= mid)return query(lson,L,R);
else if(L > mid)return query(rson,L,R);
else return query(lson,L,R) + query(rson,L,R);
}
void modifyline(int x,int y,int v)
{
while(top[x] != top[y])
{
if(dep[top[x]] < dep[top[y]])swap(x,y);
modify(root,dfn[top[x]],dfn[x],v);
x = fa[top[x]];
}
if(dep[x] < dep[y])swap(x,y);
modify(root,dfn[y],dfn[x],v);
return;
}
int queryline(int x,int y)
{
int ans = 0;
while(top[x] != top[y])
{
if(dep[top[x]] < dep[top[y]])swap(x,y);
ans = (1ll*ans + query(root,dfn[top[x]],dfn[x]).sum)%mo;
x = fa[top[x]];
}
if(dep[x] < dep[y])swap(x,y);
ans = (1ll*ans + query(root,dfn[y],dfn[x]).sum)%mo;
return ans;
}
void modifytree(int x,int v)
{
modify(root,dfn[x],dfn[x] + siz[x] - 1,v);
return;
}
int querytree(int x)
{
return query(root,dfn[x],dfn[x] + siz[x] - 1).sum;
}
// int LCA(int x,int y)
// {
// while(top[x] != top[y])
// {
// if(dep[top[x]] < dep[top[y]])swap(x,y);
// x = fa[top[x]];
// }
// if(dep[x] < dep[y])swap(x,y);
// return x;
// }
int main()
{
scanf("%d%d%d%d",&n,&m,&s,&mo);
for(int i = 1;i <= n;i++)scanf("%d",a+i),a[i]%=mo;
for(int i = 1;i <= n-1;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v),add(v,u);
}
dfs1(s,s);
dfs2(s,s);
build(root);
while(m--)
{
int op,x,y,v;
scanf("%d",&op);
if(op == 1)scanf("%d%d%d",&x,&y,&v),modifyline(x,y,v%mo);
else if(op == 2)scanf("%d%d",&x,&y),printf("%d\n",queryline(x,y));
else if(op == 3)scanf("%d%d",&x,&v),modifytree(x,v%mo);
else if(op == 4)scanf("%d",&x),printf("%d\n",querytree(x));
}
return 0;
}
标签:return,剖分,int,top,树链,dep,dfn,query
From: https://www.cnblogs.com/AC7-/p/17765675.html