結果

問題 No.899 γatheree
ユーザー yurujirou
提出日時 2021-10-16 13:30:28
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 923 ms / 2,000 ms
コード長 2,623 bytes
コンパイル時間 4,995 ms
コンパイル使用メモリ 263,488 KB
最終ジャッジ日時 2025-01-25 01:34:51
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

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,0 };
}

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

LL composition(LL f, LL g) {
	if (f == -1) return g;
	return f;
}

LL id() {
	return -1;
}

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

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);
		for (auto v : E[u]) {
			if (v == par[u]) continue;
			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 });
	}
	REP(i, N) REP(j, 2) L[i][j] = -1;
	REP(i, N) {
		if (L[par[B[i]]][0] == -1) L[par[B[i]]][0] = B[i];
		R[par[B[i]]][0] = B[i];
	}
	REP(i, N) {
		if (L[par[par[B[i]]]][1] == -1) L[par[par[B[i]]]][1] = B[i];
		R[par[par[B[i]]]][1] = B[i];
	}

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

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

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