結果

問題 No.899 γatheree
ユーザー yurujirouyurujirou
提出日時 2021-10-16 01:37:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,842 bytes
コンパイル時間 4,966 ms
コンパイル使用メモリ 280,848 KB
実行使用メモリ 33,180 KB
最終ジャッジ日時 2023-10-17 21:58:57
合計ジャッジ時間 17,284 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
20,240 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 #

#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 P pair<int, int>
#define LP pair<long long ,long long>
#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;

struct S {
	LL s = 0, d = 1;
};

S op(S a, S b) {
	return S{ a.s + b.s,a.d + b.d };
}

S e() {
	return S{ 0,1 };
}

S mapping(LL f, S x) {
	if (f >= 0) x.s = f;
	return x;
}

LL composition(LL f, LL g) {
	return 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);
	par[0] = 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;
		REP(i, E[u].size()) {
			if (E[u][i] == par[u]) {
				Vi temp;
				for (int j = E[u].size() - 1; j > i; j--) {
					temp.push_back(E[u][j]);
					E[u].pop_back();
				}
				E[u].pop_back();
				for (auto e : temp) {
					E[u].push_back(e);
				}
				break;
			}
		}
		for (auto v : E[u]) {
			if (L[u] == -1) L[u] = v;
			R[u] = v;
			par[v] = u;
			que.push(v);
		}
	}
	lazy_segtree<S, op, e, LL, mapping, composition, id> seg(N);
	REP(i, N) {
		seg.set(i, S{ A[B[i]] ,1 });
	}

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

		int u = x;
		LL res = 0;

		res += seg.get(position[u]).s;
		seg.apply(position[u], position[u] + 1, 0);

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

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

		u = par[x];
		if (L[u] != -1) {
			res += seg.prod(position[L[u]], position[R[u]] + 1).s;
			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).s;
			seg.apply(position[L[u]], position[R[u]] + 1, 0);
		}

		u = L[x];
		if (u != -1) {
			u = L[u];
			if (u != -1) {
				if (right.find(u) == right.end()) {
					int i = position[u];
					for (; i < N; i++) {
						if (par[par[B[i]]] != x) {
							break;
						}
					}
					right[u] = B[i - 1];
				}
				res += seg.prod(position[u], position[right[u]] + 1).s;
				seg.apply(position[u], position[right[u]] + 1, 0);
			}
		}
		cout << res << endl;
		seg.apply(position[x], position[x] + 1, res);
	}
}
0