結果
| 問題 |
No.899 γatheree
|
| コンテスト | |
| ユーザー |
kwm_t
|
| 提出日時 | 2023-05-06 18:53:50 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 389 ms / 2,000 ms |
| コード長 | 3,473 bytes |
| コンパイル時間 | 4,476 ms |
| コンパイル使用メモリ | 261,100 KB |
| 最終ジャッジ日時 | 2025-02-12 20:36:50 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 23 |
ソースコード
#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;
}
kwm_t