結果
問題 | No.899 γatheree |
ユーザー | koi_kotya |
提出日時 | 2019-10-06 14:02:04 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,224 bytes |
コンパイル時間 | 2,429 ms |
コンパイル使用メモリ | 191,860 KB |
実行使用メモリ | 16,480 KB |
最終ジャッジ日時 | 2024-10-09 19:47:21 |
合計ジャッジ時間 | 17,886 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 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 | - |
ソースコード
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> P; #define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0) #define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0) struct LazySegmentTree { private: int n; vector<ll> node,lazy; vector<bool> upd; public: LazySegmentTree(vector<ll> v) { int sz = v.size(); n = 1; while (n < sz) n *= 2; node.resize(2*n-1); lazy.resize(2*n-1,0); upd.resize(2*n-1,false); for (int i = 0;i < sz;++i) node[i+n-1] = v[i]; for (int i = n-2;i >= 0;--i) node[i] = node[i*2+1]+node[i*2+2]; } void eval(int k,int l,int r) { if (upd[k]) { node[k] = lazy[k]*(r-l); if (r-l > 1) { lazy[2*k+1] = lazy[2*k+2] = lazy[k]; upd[2*k+1] = upd[2*k+2] = true; } lazy[k] = 0; upd[k] = false; } } void update(int a,int b,ll x,int k = 0,int l = 0,int r = -1) { if (r < 0) r = n; eval(k,l,r); if (b <= l || r <= a) return; if (a <= l && r <= b) { lazy[k] = x; upd[k] = true; eval(k,l,r); } else { update(a,b,x,2*k+1,l,(l+r)/2); update(a,b,x,2*k+2,(l+r)/2,r); node[k] = node[2*k+1]+node[2*k+2]; } } ll getsum(int a,int b,int k = 0,int l = 0,int r = -1) { if (r < 0) r = n; if (b <= l || r <= a) return 0; eval(k,l,r); if (a <= l && r <= b) return node[k]; ll vl = getsum(a,b,k*2+1,l,(l+r)/2); ll vr = getsum(a,b,2*k+2,(l+r)/2,r); return vl+vr; } }; int main() { int n; cin >> n; vector<vector<int>> edges(n); vector<int> a(n),idx(n),par(n); deque<int> que; vector<bool> used(n,false); vector<P> child(n,P(-1,-1)),child2(n,P(-1,-1));; LazySegmentTree seg(vector<ll>(n,0)); for (int i = 0;i < n-1;++i) { int u,v; cin >> u >> v; edges[u].push_back(v); edges[v].push_back(u); } for (int i = 0;i < n;++i) cin >> a[i]; que.push_back(0); idx[0] = 0; par[0] = -1; int c = 0; while (!que.empty()) { int t = que.front(); que.pop_front(); used[t] = true; for (int& i : edges[t]) if (!used[i]) { c++; idx[c] = i; par[c] = idx[t]; if (child[idx[t]].first < 0) child[idx[t]] = P(c,c); child[idx[t]].second = c; if (par[idx[t]] >= 0) { if (child2[par[idx[t]]].first < 0) child2[par[idx[t]]] = P(c,c); child2[par[idx[t]]].second = c; } que.push_back(i); } } for (int i = 0;i < n;++i) { } vector<int> idx2(n); for (int i = 0;i < n;++i) idx2[idx[i]] = i; for (int i = 0;i < n;++i) seg.update(idx2[i],idx2[i]+1,a[i]); int q; cin >> q; for (int z = 0;z < q;++z) { int x; cin >> x; int y = idx2[x]; ll sum = seg.getsum(y,y+1); seg.update(y,y+1,0); if (par[y] >= 0) { sum += seg.getsum(par[y],par[y]+1); sum += seg.getsum(child[par[y]].first,child[par[y]].second+1); seg.update(par[y],par[y]+1,0); seg.update(child[par[y]].first,child[par[y]].second+1,0); if (par[par[y]] >= 0) { int k = par[par[y]]; sum += seg.getsum(k,k+1); seg.update(k,k+1,0); } } if (child[y].first >= 0) { sum += seg.getsum(child[y].first,child[y].second+1); seg.update(child[y].first,child[y].second+1,0); if (child2[y].first >= 0) { sum += seg.getsum(child2[y].first,child2[y].second+1); seg.update(child2[y].first,child2[y].second+1,0); } } seg.update(y,y+1,sum); cout << sum << endl; } }