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