|
| 1 | +#include <bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | +typedef long long ll; |
| 4 | + |
| 5 | +const int MAX = 1e5; |
| 6 | +int tree[MAX*4]; |
| 7 | +int lazy[MAX*4]; |
| 8 | + |
| 9 | +vector<vector<int>> edges(MAX+1); |
| 10 | + |
| 11 | +int node_count; |
| 12 | +int start[MAX+1]; |
| 13 | +int last[MAX+1]; |
| 14 | +void dfs(int x, int parent) { |
| 15 | + start[x] = ++node_count; |
| 16 | + for (auto node : edges[x]) { |
| 17 | + dfs(node, x); |
| 18 | + } |
| 19 | + last[x] = node_count; |
| 20 | +} |
| 21 | + |
| 22 | +int N, M; |
| 23 | + |
| 24 | +void propagation(int s, int e, int node) { |
| 25 | + if (!lazy[node]) return; |
| 26 | + tree[node] += lazy[node]; |
| 27 | + if (s != e) for (int i = node*2; i <= node*2+1; i++) lazy[i] += lazy[node]; |
| 28 | + lazy[node] = 0; |
| 29 | +} |
| 30 | + |
| 31 | +void update(int s, int e, int node, int l, int r, int diff) { |
| 32 | + if (s > r || e < l) return; |
| 33 | + if (s >= l && e <= r) { |
| 34 | + lazy[node] += diff; |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + int m = (s+e)/2; |
| 39 | + update(s, m, node*2, l, r, diff); |
| 40 | + update(m+1, e, node*2+1, l, r, diff); |
| 41 | +} |
| 42 | + |
| 43 | +int query(int s, int e, int node, int l, int r) { |
| 44 | + if (s > r || e < l) return 0; |
| 45 | + propagation(s, e, node); |
| 46 | + if (s >= l && e <= r) return tree[node]; |
| 47 | + int m = (s+e)/2; |
| 48 | + return query(s, m, node*2, l, r) + query(m+1, e, node*2+1, l , r); |
| 49 | +} |
| 50 | + |
| 51 | +void solve(){ |
| 52 | + cin >> N >> M; |
| 53 | + for (int i = 1; i <= N; i++) { |
| 54 | + int parent; cin >> parent; |
| 55 | + if (i > 1) edges[parent].push_back(i); |
| 56 | + } |
| 57 | + |
| 58 | + dfs(1, -1); |
| 59 | + |
| 60 | + while (M--) { |
| 61 | + int a,b; |
| 62 | + cin >> a >> b; |
| 63 | + update(1, N, 1, start[a], last[a], b); |
| 64 | + } |
| 65 | + for (int i = 1; i <= N; i++) { |
| 66 | + cout << query(1, N, 1, start[i], start[i]); |
| 67 | + cout << ' '; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +int main() |
| 72 | +{ |
| 73 | + cin.tie(0)->sync_with_stdio(false); |
| 74 | + solve(); |
| 75 | + return 0; |
| 76 | +} |
0 commit comments