CSES - Shared codeLink to this code:
https://cses.fi/paste/a8da2adebd6033b866719b/
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5;
vector<int> adj[N];
int n, val[N], bit[2 * N], timer = 0, in[N], out[N];
void update(int pos, int val){
while(pos <= 2 * n + 5){
bit[pos] += val;
pos += (pos & -pos);
}
}
void update(int l, int r, int val){
update(l, val);
update(r + 1, -val);
}
int get(int pos){
int res = 0;
while(pos > 0){
res += bit[pos];
pos -= (pos & -pos);
}
return res;
}
void dfs(int node, int par = 0) {
in[node] = ++timer;
for(int u : adj[node]) {
if(u != par) {
dfs(u, node);
}
}
out[node] = timer;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int x, y, q;
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> val[i];
}
for(int i = 1; i < n; i++){
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1);
for(int i = 1; i <= n; i++){
update(in[i], out[i], val[i]);
}
while(q--){
int type, s, delta;
cin >> type;
if(type == 1){
cin >> s >> x;
delta = x - val[s];
update(in[s], out[s], delta);
} else{
cin >> s;
cout << get(in[s]) << endl;
}
}
return 0;
}