Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- const ll nmax = 1e9+7;
- const ll nmax2 = 998244353;
- int main(){
- std::ios_base::sync_with_stdio(false);
- std::cin.tie(0);
- std::cout.tie(0);
- ll n, q;
- std::cin >> n >> q;
- ll a[n + 1], b[n + 1];
- for (ll i = 1; i <= n; i++){
- std::cin >> b[i];
- a[i] = 0;
- }
- std::vector<ll> adj[n + 1];
- for (ll i = 1, x, y; i < n; i++){
- std::cin >> x >> y;
- adj[x].push_back(y);
- adj[y].push_back(x);
- }
- ll siz[n + 1];
- for (ll i = 1; i <= n; i++) siz[i] = 0;
- std::vector<ll> euler_tour;
- std::function<ll(ll, ll)> dfs = [&] (ll u, ll p) {
- siz[u] = 1;
- euler_tour.push_back(u);
- for (auto v : adj[u]){
- if (v != p){
- siz[u] += dfs(v, u);
- }
- }
- return siz[u];
- };
- siz[0] = dfs(1, 0);
- ll rev[n + 1];
- for (ll i = 0; i < euler_tour.size(); i++){
- rev[euler_tour[i]] = i + 1;
- a[i + 1] = b[euler_tour[i]];
- }
- ll tree[2 * n + 3];
- for (ll i = 1; i <= n; i++) tree[n + i - 1] = a[i];
- for (ll i = n - 1; i > 0; i--) tree[i] = tree[i << 1] + tree[i << 1 | 1];
- auto query = [&] (ll l_, ll r_) -> ll {
- ll res = 0;
- for (l_ += n - 1, r_ += n - 1; l_ < r_; l_ >>= 1, r_ >>= 1){
- if (l_&1) res += tree[l_++];
- if (r_&1) res += tree[--r_];
- }
- return res;
- };
- auto update = [&] (ll ind, ll val) -> void {
- for (tree[ind += n - 1] += val; ind > 1; ind >>= 1) tree[ind >> 1] = tree[ind] + tree[ind ^ 1];
- };
- for (ll i = 1, z; i <= q; i++){
- std::cin >> z;
- if (z == 1){
- ll ind, val;
- std::cin >> ind >> val;
- update(rev[ind], val);
- } else {
- ll ind;
- std::cin >> ind;
- std::cout << query(rev[ind], rev[ind] + siz[ind]) << '\n';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement