結果
問題 | No.899 γatheree |
ユーザー | koi_kotya |
提出日時 | 2019-10-06 18:40:24 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 805 ms / 2,000 ms |
コード長 | 3,953 bytes |
コンパイル時間 | 2,218 ms |
コンパイル使用メモリ | 188,636 KB |
実行使用メモリ | 16,036 KB |
最終ジャッジ日時 | 2024-10-10 21:32:14 |
合計ジャッジ時間 | 18,801 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 3 ms
6,820 KB |
testcase_02 | AC | 2 ms
6,816 KB |
testcase_03 | AC | 2 ms
6,816 KB |
testcase_04 | AC | 2 ms
6,820 KB |
testcase_05 | AC | 2 ms
6,816 KB |
testcase_06 | AC | 771 ms
15,648 KB |
testcase_07 | AC | 768 ms
15,780 KB |
testcase_08 | AC | 793 ms
15,644 KB |
testcase_09 | AC | 778 ms
15,776 KB |
testcase_10 | AC | 777 ms
15,772 KB |
testcase_11 | AC | 756 ms
15,904 KB |
testcase_12 | AC | 766 ms
15,776 KB |
testcase_13 | AC | 790 ms
15,676 KB |
testcase_14 | AC | 779 ms
15,648 KB |
testcase_15 | AC | 805 ms
15,772 KB |
testcase_16 | AC | 794 ms
15,772 KB |
testcase_17 | AC | 784 ms
15,776 KB |
testcase_18 | AC | 781 ms
15,648 KB |
testcase_19 | AC | 768 ms
15,776 KB |
testcase_20 | AC | 776 ms
15,776 KB |
testcase_21 | AC | 772 ms
16,032 KB |
testcase_22 | AC | 773 ms
16,032 KB |
testcase_23 | AC | 758 ms
16,036 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); 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]; vector<int> idx(n,-1),par(n),cl(n,-1),cr(n,-1),cl2(n,-1),cr2(n,-1); queue<int> que; // vector<P> child(n,P(-1,-1)),child2(n,P(-1,-1)); // vector<bool> used(n,false); que.push(0); idx[0] = 0; par[0] = -1; int c = 0; while (!que.empty()) { int t = que.front(); que.pop(); // used[t] = true; int s = idx[t]; for (int& i : edges[t]) if (idx[i] < 0) { c++; idx[i] = c; par[c] = s; if (cl[s] < 0) cl[s] = c; cr[s] = c+1; if (par[s] >= 0) { if (cl2[par[s]] < 0) cl2[par[s]] = c; cr2[par[s]] = c+1; } que.push(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(cl[par[y]],cr[par[y]]); seg.update(par[y],par[y]+1,0); seg.update(cl[par[y]],cr[par[y]],0); if (par[par[y]] >= 0) { int k = par[par[y]]; sum += seg.getsum(k,k+1); seg.update(k,k+1,0); } } if (cl[y] >= 0) { sum += seg.getsum(cl[y],cr[y]); seg.update(cl[y],cr[y],0); if (cl2[y] >= 0) { sum += seg.getsum(cl2[y],cr2[y]); seg.update(cl2[y],cr2[y],0); } } seg.update(y,y+1,sum); cout << sum << endl; } }