結果

問題 No.899 γatheree
ユーザー kwm_tkwm_t
提出日時 2023-05-06 18:53:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 275 ms / 2,000 ms
コード長 3,473 bytes
コンパイル時間 4,940 ms
コンパイル使用メモリ 270,280 KB
実行使用メモリ 18,364 KB
最終ジャッジ日時 2023-08-15 17:06:36
合計ジャッジ時間 11,869 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 239 ms
18,040 KB
testcase_07 AC 232 ms
18,020 KB
testcase_08 AC 234 ms
18,040 KB
testcase_09 AC 237 ms
17,988 KB
testcase_10 AC 238 ms
18,040 KB
testcase_11 AC 236 ms
18,056 KB
testcase_12 AC 236 ms
17,988 KB
testcase_13 AC 239 ms
18,136 KB
testcase_14 AC 238 ms
18,140 KB
testcase_15 AC 234 ms
18,228 KB
testcase_16 AC 234 ms
18,088 KB
testcase_17 AC 240 ms
18,024 KB
testcase_18 AC 232 ms
18,012 KB
testcase_19 AC 234 ms
17,992 KB
testcase_20 AC 232 ms
18,028 KB
testcase_21 AC 275 ms
18,364 KB
testcase_22 AC 257 ms
18,268 KB
testcase_23 AC 267 ms
18,248 KB
権限があれば一括ダウンロードができます

ソースコード

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);//org->trans
	vector<int>rev(n, -1);//trans->org
	vector<int>par(n, -1);//trans
	vector<P>child(n, { INF,-INF });//trans
	vector<P>child2(n, { INF,-INF });//trans
	{
		queue<int>q;// trans
		int cnt = 0;
		q.push(cnt);
		idx[0] = cnt;
		rev[cnt] = 0;
		while (!q.empty()) {
			auto top = q.front();
			q.pop();
			rep(i, to[rev[top]].size()) {
				int org = to[rev[top]][i];
				int trans = idx[org];
				if (-1 != trans)continue;
				cnt++;
				trans = cnt;
				idx[org] = trans;
				rev[trans] = org;
				q.push(trans);
				chmin(child[top].first, trans);
				chmax(child[top].second, trans);
				par[trans] = top;
				int p = par[top];
				if (-1 != p) {
					chmin(child2[p].first, cnt);
					chmax(child2[p].second, cnt);
				}
			}
		}
		/*rep(i, n) {
			cout << i << " " << idx[i] << endl;
			cout << child[idx[i]].first << " " << child[idx[i]].second << endl;
			cout << child2[idx[i]].first << " " << child2[idx[i]].second << endl;
			cout << 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;
		x = idx[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 (1 == _)break;
				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