結果
問題 | No.899 γatheree |
ユーザー | koi_kotya |
提出日時 | 2019-10-06 16:16:12 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 797 ms / 2,000 ms |
コード長 | 4,094 bytes |
コンパイル時間 | 2,399 ms |
コンパイル使用メモリ | 191,988 KB |
実行使用メモリ | 16,096 KB |
最終ジャッジ日時 | 2024-10-10 05:02:35 |
合計ジャッジ時間 | 18,285 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 2 ms
6,816 KB |
testcase_03 | AC | 2 ms
6,820 KB |
testcase_04 | AC | 2 ms
6,816 KB |
testcase_05 | AC | 2 ms
6,820 KB |
testcase_06 | AC | 760 ms
15,712 KB |
testcase_07 | AC | 775 ms
15,836 KB |
testcase_08 | AC | 769 ms
15,712 KB |
testcase_09 | AC | 751 ms
15,708 KB |
testcase_10 | AC | 778 ms
15,712 KB |
testcase_11 | AC | 797 ms
15,716 KB |
testcase_12 | AC | 780 ms
15,840 KB |
testcase_13 | AC | 780 ms
15,688 KB |
testcase_14 | AC | 778 ms
15,716 KB |
testcase_15 | AC | 779 ms
15,584 KB |
testcase_16 | AC | 781 ms
15,708 KB |
testcase_17 | AC | 761 ms
15,712 KB |
testcase_18 | AC | 768 ms
15,840 KB |
testcase_19 | AC | 760 ms
15,712 KB |
testcase_20 | AC | 752 ms
15,708 KB |
testcase_21 | AC | 765 ms
15,968 KB |
testcase_22 | AC | 769 ms
16,096 KB |
testcase_23 | AC | 769 ms
16,096 KB |
ソースコード
#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; int s = idx[t]; for (int& i : edges[t]) if (!used[i]) { c++; idx[i] = c; par[c] = s; if (child[s].first < 0) child[s] = P(c,c); child[s].second = c; if (par[s] >= 0) { if (child2[par[s]].first < 0) child2[par[s]] = P(c,c); child2[par[s]].second = c; } que.push_back(i); } } for (int i = 0;i < n;++i) seg.update(idx[i],idx[i]+1,a[i]); int q; cin >> q; for (int z = 0;z < q;++z) { int x; cin >> x; int y = idx[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; } }