作业介绍

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
const int N = 1000005;
int n,m,r,cnt;
int fa[N],son[N],siz[N],top[N],dep[N],id[N];
ll w[N],wt[N],tree[N*4],tag[N*4];
vector<int>e[N*4];
void dfs1(int x,int f,int deep)
{
	dep[x] = deep;
	fa[x] = f;
	siz[x]=1;
	int maxx = 0;
	for(int i=0;i<e[x].size();i++){
		int v= e[x][i];
		if(v==f)continue;
		dfs1(v,x,deep+1);
		siz[x]+=siz[v];
		if(maxx<siz[v]){
			maxx=siz[v];
			son[x]=v;
		}
	}	
}
void dfs2(int x,int topfa)
{
	top[x] = topfa;
	id[x]=++cnt;
	wt[id[x]] = w[x];
	if(!son[x])return;
	dfs2(son[x],topfa);
	for(int i=0;i<e[x].size();i++){
		int v=e[x][i];
		if(v==son[x] || v==fa[x])continue;
		dfs2(v,v);
	}
}
void pushup(int rt)
{
	tree[rt] = tree[rt<<1]+tree[rt<<1|1];
}
void pushdown(int l,int r,int rt)
{
	if(tag[rt]!=0){
		tag[rt<<1]+=tag[rt];
		tag[rt<<1|1]+=tag[rt];
		int mid = (l+r)>>1;
		tree[rt<<1]+=tag[rt]*(mid-l+1);
		tree[rt<<1|1]+=tag[rt]*(r-mid);
		tag[rt] = 0;
	}
}
void build(int l,int r,int rt)
{
	if(l==r){
		tree[rt] = wt[l];
		return;
	}
	int mid = (l+r)>>1;
	build(l,mid,rt<<1);
	build(mid+1,r,rt<<1|1);
	pushup(rt);
}
void update(int l,int r,int rt,int L,int R,int c)
{
	if(L<=l && r<=R){
		tree[rt]+=c*(r-l+1);
		tag[rt]+=c;
		return;
	}
	int mid = (l+r)>>1;
	pushdown(l,r,rt);
	if(L<=mid)update(l,mid,rt<<1,L,R,c);
	if(R>mid)update(mid+1,r,rt<<1|1,L,R,c);
	pushup(rt);
}
ll query(int l,int r,int rt,int L,int R)
{
	if(L<=l && r<=R){
		return tree[rt];
	}
	int mid = (l+r)>>1;
	ll sum = 0;
	pushdown(l,r,rt);
	if(L<=mid)sum+=query(l,mid,rt<<1,L,R);
	if(R>mid)sum+=query(mid+1,r,rt<<1|1,L,R);
	return sum;
}
void pos1(int x,int y,int k)
{
	while(top[x]!=top[y]){
		if(dep[top[x]]>dep[top[y]])swap(x,y);
		update(1,n,1,id[top[y]],id[y],k);
		y=fa[top[y]];
	}
	if(dep[x]>dep[y])swap(x,y);
	update(1,n,1,id[x],id[y],k);
}
ll pos2(int x)
{
	return query(1,n,1,id[x],id[x]);
}
ll pos3(int x)
{
	return query(1,n,1,id[x],id[x]+siz[x]-1);
}
signed main()
{
	cin>>n>>m>>r;
	for(int i=1;i<=n;i++)cin>>w[i];
	for(int i=1;i<n;i++){
		int x,y;
		cin>>x>>y;
		e[x].push_back(y);
		e[y].push_back(x);
	}
	dfs1(r,0,0);
	dfs2(r,r);
	build(1,n,1);
	while(m--){
		int pos,x,y,z;
		cin>>pos;
		if(pos==1){
			cin>>x>>y>>z;
			pos1(x,y,z);
		}
		if(pos==2){
			cin>>x;
			cout<<pos2(x)<<endl; 
		}
		if(pos==3){
			cin>>x;
			cout<<pos3(x)<<endl;
		}
	}
	return 0;
}
状态
已结束
题目
4
开始时间
2023-3-18 8:00
截止时间
2023-3-26 11:59
可延期
168 小时