#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cmath>
#define ll long long
using namespace std;
const int N=1e5+10,mod=1e9+7,INF=0x3f3f3f3f;
vector<int> G[N];
int l[N],r[N],pos[N],dfs_clock;
ll val[N];
struct node {
int Left,Right;
ll sum;
}NODE[N<<2];
void PushUp(int idx) {
NODE[idx].sum=NODE[idx<<1].sum+NODE[idx<<1|1].sum;
}
void build(int l, int r, int idx) {
NODE[idx]={l,r,0};
if (l==r) {
NODE[idx].sum=val[pos[l]];
return;
}
int m=(l+r)>>1;
build(l,m,idx<<1),build(m+1,r,idx<<1|1);
PushUp(idx);
}
void Updata(int pos, int idx, int val) {
if (NODE[idx].Left==NODE[idx].Right) {
NODE[idx].sum=val;
return;
}
int m=(NODE[idx].Left+NODE[idx].Right)>>1;
if (pos<=m) Updata(pos,idx<<1,val);
else Updata(pos,idx<<1|1,val);
PushUp(idx);
}
ll query(int l, int r, int idx) {
if (NODE[idx].Left==l&&NODE[idx].Right==r) return NODE[idx].sum;
int m=(NODE[idx].Left+NODE[idx].Right)>>1;
if (r<=m) return query(l,r,idx<<1);
else if (l>m) return query(l,r,idx<<1|1);
else return query(l,m,idx<<1)+query(m+1,r,idx<<1|1);
}
void dfs(int u, int fa) {
l[u]=++dfs_clock;
pos[dfs_clock]=u;
for (int i=0; i<G[u].size(); i++) {
int v=G[u][i];
if (v==fa) continue;
dfs(v,u);
}
r[u]=dfs_clock;
}
int main() {
int n,q; scanf("%d%d",&n,&q);
for (int i=1; i<n; i++) {
int u,v; scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
for (int i=1; i<=n; i++) scanf("%lld",&val[i]);
dfs(1,0);
build(1,n,1);
while (q--) {
int op; scanf("%d",&op);
if (op==1) {
int x,y; scanf("%d%d",&x,&y);
Updata(l[x],1,y);
val[x]=y;
} else {
int x; scanf("%d",&x);
printf("%lld\n",query(l[x],r[x],1));
}
}
return 0;
}
场景二:参考代码C++
最新推荐文章于 2024-09-03 11:25:36 发布