結果

問題 No.899 γatheree
ユーザー kwm_tkwm_t
提出日時 2023-05-06 18:13:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,203 bytes
コンパイル時間 4,340 ms
コンパイル使用メモリ 274,572 KB
実行使用メモリ 18,336 KB
最終ジャッジ日時 2024-05-03 03:48:48
合計ジャッジ時間 11,884 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 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>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
// 区間変更・区間和取得
struct S {
	long long value;
	int size;
};
using F = long long;
const F ID = 8e18;
S op(S a, S b) { return { a.value + b.value, a.size + b.size }; }
S e() { return { 0, 0 }; }
S mapping(F f, S x) {
	if (f != ID) x.value = f * x.size;
	return x;
}
F composition(F f, F g) { return (f == ID ? g : f); }
F id() { return ID; }
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n; cin >> n;
	vector<vector<int>>to(n);
	rep(i, n - 1) {
		int u, v; cin >> u >> v;
		to[u].push_back(v);
		to[v].push_back(u);
	}
	vector<int>idx(n, -1);
	vector<int>par(n, -1);
	vector<P>child(n, { INF,-INF });
	vector<P>child2(n, { INF,-INF });
	{
		queue<int>q;
		q.push(0);
		idx[0] = 0;
		int cnt = 0;
		while (!q.empty()) {
			auto top = q.front();
			q.pop();
			rep(i, to[top].size()) {
				if (-1 != idx[to[top][i]])continue;
				q.push(to[top][i]);
				cnt++;
				idx[to[top][i]] = cnt;
				chmin(child[top].first, cnt);
				chmax(child[top].second, cnt);
				par[to[top][i]] = top;
				int p = par[top];
				if (-1 != p) {
					chmin(child2[p].first, cnt);
					chmax(child2[p].second, cnt);
				}
			}
		}
		//rep(i, n) {
		//	cout << idx[i] << " " << child[i].first << " " << child[i].second << endl;
		//	cout << child2[i].first << " " << child2[i].second << endl;
		//}
	}
	vector<S> v(n, { 0, 1 });
	rep(i, n) {
		int a; cin >> a;
		v[idx[i]].value = a;
	}
	lazy_segtree<S, op, e, F, mapping, composition, id> seg(v);
	int q; cin >> q;
	while (q--) {
		int x; cin >> x;
		long long val = 0;
		{//自分
			auto get = seg.get(x);
			val += get.value;
			seg.set(x, { 0,1 });
		}
		{
			int p = par[x];
			rep(_, 2) {//親たち
				if (-1 == p)break;
				auto get = seg.get(p);
				val += get.value;
				seg.set(p, { 0,1 });
				if (INF != child[p].first) {
					get = seg.prod(child[p].first, child[p].second + 1);
					val += get.value;
					seg.apply(child[p].first, child[p].second + 1, 0);
				}
				p = par[p];
			}
		}
		if(INF != child[x].first){
			auto get = seg.prod(child[x].first, child[x].second + 1);
			val += get.value;
			seg.apply(child[x].first, child[x].second + 1, 0);
		}
		if (INF != child2[x].first) {
			auto get = seg.prod(child2[x].first, child2[x].second + 1);
			val += get.value;
			seg.apply(child2[x].first, child2[x].second + 1, 0);
		}
		cout << val << endl;
		seg.set(x, { val,1 });
	}
	return 0;
}
0