Homework Introduction

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 5;
int n, m, r;
int a[N], id[N], siz[N], tree[N], tot, wt[N];
vector<int>e[N];

void dfs(int x, int f) {
	id[x] = ++tot;
	wt[tot] = a[x];
	siz[x] = 1;
	for (auto v : e[x]) {
		if (v == f)
			continue;
		dfs(v, x);
		siz[x] += siz[v];
	}
}

int lobit(int x) {
	return x & -x;
}

void update(int x, int c) {
	for (int i = x; i <= n; i += lobit(i)) {
		tree[i] += c;
	}
}

int query(int x) {
	int sum = 0;
	for (int i = x; i >= 1; i -= lobit(i))
		sum += tree[i];
	return sum;
}

signed main() {
	cin >> n >> m >> r;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	for (int i = 1; i < n; i++) {
		int x, y;
		cin >> x >> y;
		e[x].push_back(y);
		e[y].push_back(x);
	}
	dfs(r, 0);
	for (int i = 1; i <= n; i++) {
		update(id[i], a[i]);
	}
	while (m--) {
		int pos, x, y, z;
		cin >> pos >> x;
		if (pos == 1) {
			cin >> y;
			update(id[x], y);
		} else if (pos == 2) {
			cout << query(id[x] + siz[x] - 1) - query(id[x] - 1) << endl;
		}
	}
	return 0;
}
Status
Done
Problem
4
Open Since
2025-6-27 0:00
Deadline
2025-7-5 23:59
Extension
24 hour(s)