結果

問題 No.899 γatheree
ユーザー QCFiumQCFium
提出日時 2019-10-05 14:37:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,886 bytes
コンパイル時間 2,828 ms
コンパイル使用メモリ 187,860 KB
実行使用メモリ 13,696 KB
最終ジャッジ日時 2024-04-15 19:09:27
合計ジャッジ時間 12,311 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

struct SegTree {
	int n;
	std::vector<int64_t> sum;
	std::vector<bool> lazy0; // make 0
	SegTree (int n_) {
		for (n = 1; n < n_; n <<= 1);
		sum.resize(2 * n);
		lazy0.resize(2 * n);
	}
	void flush(int i) {
		if (lazy0[i]) {
			sum[i] = 0;
			if (i < n) {
				lazy0[i << 1] = true;
				lazy0[i << 1 | 1] = true;
			}
			lazy0[i] = false;
		}
	}
	void fetch(int node) {
		sum[node] = sum[node << 1] + sum[node << 1 | 1];
	}
	void set(int i, int64_t val, int node = 1, int node_l = 0, int node_r = 0) {
		if (!node_r) node_r = n;
		flush(node);
		if (node_l == i && node_r == i + 1) sum[node] = val;
		else {
			int mid = node_l + ((node_r - node_l) >> 1);
			if (i < mid) set(i, val, node << 1, node_l, mid);
			else set(i, val, node << 1 | 1, mid, node_r);
			fetch(node);
		}
	}
	int64_t sum0(int l, int r, int node = 1, int node_l = 0, int node_r = 0) {
		if (!node_r) node_r = n;
		flush(node);
		if (l <= node_l && r >= node_r) {
			int64_t res = sum[node];
			lazy0[node] = true;
			flush(node);
			return res;
		}
		if (l >= node_r || r <= node_l) return 0;
		int mid = node_l + ((node_r - node_l) >> 1);
		int64_t res = sum0(l, r, node << 1, node_l, mid) + sum0(l, r, node << 1 | 1, mid, node_r);
		fetch(node);
		return res;
	}
};

int main() {
	int n = ri();
	std::vector<int> hen[n];
	for (int i = 0; i + 1 < n; i++) {
		int a = ri(), b = ri();
		hen[a].push_back(b);
		hen[b].push_back(a);
	}
	std::queue<int> que;
	que.push(0);
	int cnt = 0;
	std::vector<int> ord(n, -1);
	std::vector<int> par(n, -1);
	while (que.size()) {
		int i = que.front();
		que.pop();
		ord[i] = cnt++;
		for (auto j : hen[i]) if (ord[j] == -1) que.push(j), par[j] = i;
	}
	std::vector<std::pair<int, int> > child(n, {1000000000, -1000000000});
	std::vector<std::pair<int, int> > child2(n, {1000000000, -1000000000});
	for (int i = 1; i < n; i++) {
		child[par[i]].first = std::min(child[par[i]].first, ord[i]);
		child[par[i]].second = std::max(child[par[i]].second, ord[i]);
		if (par[i]) {
			child2[par[par[i]]].first = std::min(child2[par[par[i]]].first, ord[i]);
			child2[par[par[i]]].second = std::max(child2[par[par[i]]].second, ord[i]);
		}
	}
	SegTree tree(n);
	for (int i = 0; i < n; i++) tree.set(ord[i], ri());
	int q = ri();
	for (int i = 0; i < q; i++) {
		int x = ri();
		int64_t num = 0;
		if (x) {
			num += tree.sum0(ord[par[x]], ord[par[x]] + 1);
			num += tree.sum0(child[par[x]].first, child[par[x]].second + 1);
		} else num += tree.sum0(ord[x], ord[x] + 1);
		if (x && par[x]) num += tree.sum0(ord[par[par[x]]], ord[par[par[x]]] + 1);
		if (child[x].first != 1000000000) num += tree.sum0(child[x].first, child[x].second + 1);
		if (child2[x].first != 1000000000) num += tree.sum0(child2[x].first, child2[x].second + 1);
		tree.set(ord[x], num);
		std::cout << num << std::endl;
	}
	
	return 0;
}
0