分析
由于我们每次是查询一个子树内的所有节点的权值和
很套路的想法就是将他映射到线性区间上
那么就想到了用dfn
序进行维护
由于我们每次有单点修改
那么我们可以使用码量比较小的树状数组直接维护
支持单调修改 + 区间查询
时间复杂度:
期望得分:100分
代码
//Nowcoder 204871 #include <algorithm> #include <iostream> #include <cstring> #include <vector> #include <cstdio> #include <cmath> #define ll long long #define cl(x, y) memset((x), (y), sizeof(x)) #define rep(i, a, b) for(int i = a; i <= b; i++) #define per(i, a, b) for(int i = a; i >= b; i--) #define de(x) cerr << #x << " = " << x << " " #define inc_mod(x, y) x = ((x - y) % mod + mod) % mod #define add_mod(x, y) x = (x + y) % mod #define lowbit(x) (x & (-x)) #define inf 0x3f3f3f3f #define mod 998244353 #define rson (x << 1 | 1) #define lson (x << 1) using namespace std; const int maxn = 1e6 + 10; int n, k, m; int dfn[maxn], dfn_num, R[maxn]; vector<int> G[maxn]; int val[maxn]; ll fro[maxn]; inline void dfs(int u, int fa) { dfn[u] = ++dfn_num; for(auto v : G[u]) if(v != fa) { dfs(v, u); } R[u] = dfn_num; } inline void update(int u, int a) { while(u <= n) fro[u] += a, u += lowbit(u); } inline ll get(int u) { ll ret = 0; while(u > 0) ret += fro[u], u -= lowbit(u); return ret; } signed main() { cin >> n >> m >> k; rep(i, 1, n) scanf("%d", &val[i]); int u, v, w; rep(i, 2, n) scanf("%d %d", &u, &v) , G[u].push_back(v), G[v].push_back(u); dfs(k, k); rep(i, 1, n) update(dfn[i], val[i]); while(m--) { scanf("%d %d", &u, &v); if(u == 1) { scanf("%d", &w); update(dfn[v], w); } else printf("%lld\n", get(R[v]) - get(dfn[v] - 1)); } system("pause"); return 0; }