传送门
Description
有一棵点数为 N 的树,以点 1 为根,且树点有边权。然后有 M 个
操作,分为三种:
操作 1 :把某个节点 x 的点权增加 a 。
操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 a 。
操作 3 :询问某个节点 x 到根的路径中所有点的点权和。
Input
第一行包含两个整数 N, M 。表示点数和操作数。接下来一行 N 个整数,表示树中节点的初始权值。接下来 N-1
行每行三个正整数 fr, to , 表示该树中存在一条边 (fr, to) 。再接下来 M 行,每行分别表示一次操作。其中
第一个数表示该操作的种类( 1-3 ) ,之后接这个操作的参数( x 或者 x a ) 。
Output
对于每个询问操作,输出该询问的答案。答案之间用换行隔开。
Sample Input
5 5
1 2 3 4 5
1 2
1 4
2 3
2 5
3 3
1 2 1
3 5
2 1 2
3 3
Sample Output
6
9
13
HINT
对于 100% 的数据, N,M<=100000 ,且所有输入数据的绝对值都不会超过 10^6 。
题解
貌似比上一道洛谷的板子题还简单?
sb的我竟然还WA了一次
CODE:
#include<cstdio>
#include<cstring>
#define LL long long
const int INF=1e9;
const int maxn=1e5+10;
struct aa
{
LL num,plus;
}t[400001];
struct edge
{
int next,to;
}a[2*maxn];
int s[maxn],head[maxn];
int top[maxn],son[maxn],pos[maxn],deep[maxn],f[maxn],size[maxn],num[maxn];
int n,m,tot,e_num=-1,x,y,k;
inline int max(int a,int b) {if(a>=b)return a;return b;}
inline int min(int a,int b) {if(a<=b)return a;return b;}
inline void swap(int &a,int &b) {int c=a;a=b;b=c;}
inline void read(int &n)
{
n=0;char c=getchar();bool b=0;
while(c<'0'||c>'9'){if(c=='-')b=1;c=getchar();}
while(c>='0'&&c<='9') n=n*10+c-48,c=getchar();
if(b) n*=-1;
}
inline void add(int from,int to)
{
a[++e_num].next=head[from],a[e_num].to=to,head[from]=e_num;
a[++e_num].next=head[to],a[e_num].to=from,head[to]=e_num;
}
void dfs(int now,int fa,int depth)
{
deep[now]=depth;
size[now]=1;
f[now]=fa;
int tmp=-INF;
for(int i=head[now];i!=-1;i=a[i].next)
if(a[i].to!=fa)
{
dfs(a[i].to,now,depth+1);
size[now]+=size[a[i].to];
if(size[a[i].to]>tmp) tmp=size[a[i].to],son[now]=a[i].to;
}
}
void dfs2(int now,int high)
{
top[now]=high;
pos[now]=++tot;
num[tot]=s[now];
if(son[now]) dfs2(son[now],high);
for(int i=head[now];i!=-1;i=a[i].next)
if(a[i].to!=f[now]&&a[i].to!=son[now]) dfs2(a[i].to,a[i].to);
}
inline void pushdown(int l,int r,int now)
{
if(l==r||!t[now].plus) return;
int mid=(l+r)>>1,s1=now<<1,s2=now<<1|1;
t[s1].plus+=t[now].plus;
t[s2].plus+=t[now].plus;
t[s1].num+=1ll*(mid-l+1)*t[now].plus;
t[s2].num+=1ll*(r-mid)*t[now].plus;
t[now].plus=0;
}
inline void update(int now)
{
t[now].num=t[now<<1].num+t[now<<1|1].num;
}
void build(int l,int r,int now)
{
if(l==r)
{
t[now].num=num[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,now<<1);
build(mid+1,r,now<<1|1);
update(now);
}
void Add(int L,int R,int l,int r,int now,int num)
{
if(L<=l&&r<=R)
{
t[now].num+=(r-l+1)*num;
t[now].plus+=num;
return;
}
int mid=(l+r)>>1;
pushdown(l,r,now);
if(L<=mid) Add(L,R,l,mid,now<<1,num);
if(R>mid) Add(L,R,mid+1,r,now<<1|1,num);
update(now);
}
LL ask(int L,int R,int l,int r,int now)
{
if(L<=l&&r<=R) return t[now].num;
int mid=(l+r)>>1;
LL ans=0;
pushdown(l,r,now);
if(L<=mid) ans+=ask(L,R,l,mid,now<<1);
if(R>mid) ans+=ask(L,R,mid+1,r,now<<1|1);
return ans;
}
void addnum(int x,int y)
{
int L=pos[x],R=pos[x]+size[x]-1;
Add(L,R,1,n,1,y);
}
LL askpath(int x,int y)
{
int L,R;
if(top[x]==top[y])
{
L=min(pos[x],pos[y]);
R=max(pos[x],pos[y]);
return ask(L,R,1,n,1);
}
if(deep[top[x]]<deep[top[y]]) swap(x,y);
L=min(pos[x],pos[top[x]]);
R=max(pos[x],pos[top[x]]);
LL ans=ask(L,R,1,n,1);
return ans+askpath(f[top[x]],y);
}
int main()
{
memset(head,-1,sizeof(head));
read(n),read(m);
for(int i=1;i<=n;i++)
read(s[i]);
for(int i=1;i<n;i++)
read(x),read(y),add(x,y);
deep[0]=-INF;
dfs(1,0,1);
dfs2(1,1);
build(1,n,1);
for(int i=1;i<=m;i++)
{
read(k),read(x);
if(k==1) read(y),Add(pos[x],pos[x],1,n,1,y);
else if(k==2) read(y),addnum(x,y);
else printf("%lld\n",askpath(x,1));
}
return 0;
}