結果

問題 No.899 γatheree
ユーザー yurujirouyurujirou
提出日時 2021-10-13 00:08:21
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,149 bytes
コンパイル時間 5,411 ms
コンパイル使用メモリ 270,892 KB
実行使用メモリ 30,288 KB
最終ジャッジ日時 2023-10-17 13:55:49
合計ジャッジ時間 16,760 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#include <atcoder/all>
using namespace atcoder;

#define REP(i,n) for(int i = 0; i < (int)n; i++)
#define RREP(i,n) for(int i = (int)n-1; i >= 0; i--)
#define LREP(i,n) for(LL i = 0; i < (LL)n; i++)

#define Vi vector<int>
#define Vl vector<long long>
#define LP pair<long long ,long long>
#define P pair<int, int>
#define T3 tuple<LL, LL, LL>
#define T4 tuple<LL, LL, LL, LL>
#define INF 1000000007
#define SIZE	400010
#define MOD 998244353
typedef long long LL;

LL op(LL a, LL b) {
	return a + b;
}

LL e() {
	return 0;
}

LL mapping(LL f, LL x) {
	return f * x;
}

LL composition(LL f, LL g) {
	return f * g;
}

LL id() {
	return 1;
}

int N, Q;
Vi E[SIZE];
LL A[SIZE];
int par[SIZE];
int L[SIZE], R[SIZE];

bool used[SIZE];
int position[SIZE];
Vi B;

int main() {
	cin >> N;
	REP(i, N - 1) {
		int u, v;
		cin >> u >> v;
		E[u].push_back(v);
		E[v].push_back(u);
	}
	REP(i, N) cin >> A[i];

	queue<int> que;
	que.push(0);
	while (que.size()) {
		int u = que.front(); que.pop();
		used[u] = true;
		position[u] = B.size();
		B.push_back(u);

		L[u] = -1, R[u] = -1;
		for (auto v : E[u]) {
			if (v == par[u]) continue;
			if (L[u] == -1) L[u] = v;
			R[u] = v;
			par[v] = u;
			que.push(v);
		}
	}
	lazy_segtree<LL, op, e, LL, mapping, composition, id> seg(N);
	REP(i, N) {
		seg.set(i, A[B[i]]);
	}

	cin >> Q;
	REP(q, Q) {
		int x;
		cin >> x;

		int u = x;
		LL res = 0;

		res += seg.get(position[u]);
		seg.set(position[u], 0);

		u = par[u];
		res += seg.get(position[u]);
		seg.set(position[u], 0);

		u = par[u];
		res += seg.get(position[u]);
		seg.set(position[u], 0);

		u = par[x];
		if (L[u] != -1) {
			res += seg.prod(position[L[u]], position[R[u]] + 1);
			seg.apply(position[L[u]], position[R[u]] + 1, 0);
		}

		u = x;
		if (L[u] != -1) {
			res += seg.prod(position[L[u]], position[R[u]] + 1);
			seg.apply(position[L[u]], position[R[u]] + 1, 0);
		}

		int l = L[L[x]], r = R[R[x]];
		if (l != -1) {
			res += seg.prod(l, r + 1);
			seg.apply(l, r + 1, 0);
		}

		cout << res << endl;
		seg.set(position[x], res);
	}
}
0