結果
問題 | No.899 γatheree |
ユーザー | 0w1 |
提出日時 | 2019-10-10 18:56:16 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,705 bytes |
コンパイル時間 | 2,357 ms |
コンパイル使用メモリ | 220,916 KB |
実行使用メモリ | 17,064 KB |
最終ジャッジ日時 | 2024-11-21 17:52:34 |
合計ジャッジ時間 | 12,052 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 5 ms
5,888 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; template<int n> struct Segt { bitset<(n << 2)> ctag; int64_t sumv[n << 2]; Segt(vector<int> a) { a.resize(n); function<void(int, int, int)> build = [&](int lb, int rb, int t) { ctag[t] = 0; if (rb - lb == 1) return void(sumv[t] = a[lb]); int mb = lb + rb >> 1; build(lb, mb, t << 1); build(mb, rb, t << 1 | 1); sumv[t] = sumv[t << 1] + sumv[t << 1 | 1]; }; build(0, n, 1); } void push(int t) { if (ctag[t]) { for (int c = 0; c < 2; ++c) { ctag[t << 1 | c] = 1; sumv[t << 1 | c] = 0; } ctag[t] = 0; } } void add(int64_t v, int k, int lb = 0, int rb = n, int t = 1) { if (rb - lb == 1) { sumv[t] += v; return; } push(t); int mb = lb + rb >> 1; if (k < mb) add(v, k, lb, mb, t << 1); else add(v, k, mb, rb, t << 1 | 1); sumv[t] = sumv[t << 1] + sumv[t << 1 | 1]; } void clear(int ql, int qr, int lb = 0, int rb = n, int t = 1) { if (qr <= lb || rb <= ql) return; if (ql <= lb && rb <= qr) { ctag[t] = 1; sumv[t] = 0; return; } push(t); int mb = lb + rb >> 1; clear(ql, qr, lb, mb, t << 1); clear(ql, qr, mb, rb, t << 1 | 1); sumv[t] = sumv[t << 1] + sumv[t << 1 | 1]; } int64_t query(int ql, int qr, int lb = 0, int rb = n, int t = 1) { if (qr <= lb || rb <= ql) return 0; if (ql <= lb && rb <= qr) return sumv[t]; push(t); int mb = lb + rb >> 1; return query(ql, qr, lb, mb, t << 1) + query(ql, qr, mb, rb, t << 1 | 1); }; }; int main() { ios::sync_with_stdio(false); int N; cin >> N; vector<vector<int>> G(N); for (int i = 0; i < N - 1; ++i) { int U, V; cin >> U >> V; G[U].push_back(V); G[V].push_back(U); } vector<int> idx(N, -1); vector<int> fa1(N, -1); vector<int> fa2(N, -1); vector<int> lb1(N, -1), rb1(N, -1); vector<int> lb2(N, -1), rb2(N, -1); { int idx_cnt = 0; queue<int> que; que.push(0); idx[0] = idx_cnt++; while (que.size()) { int u = que.front(); que.pop(); for (int v : G[u]) { if (v == fa1[u]) continue; idx[v] = idx_cnt++; fa1[v] = u; fa2[v] = fa1[u]; if (lb1[u] == -1) lb1[u] = v; rb1[u] = v; if (fa2[v] != -1) { if (lb2[fa2[v]] == -1) lb2[fa2[v]] = v; rb2[fa2[v]] = v; } que.push(v); } } } vector<int> A(N); for (int i = 0; i < N; ++i) { cin >> A[i]; } vector<int> euler_a(N); { for (int u = 0; u < N; ++u) { euler_a[idx[u]] = A[u]; } } Segt<1 << 17> tree(euler_a); int Q; cin >> Q; while (Q--) { int X; cin >> X; int64_t sum = 0; if (fa2[X] != -1) { int lb = idx[fa2[X]]; int rb = idx[fa2[X]] + 1; sum += tree.query(lb, rb); tree.clear(lb, rb); } if (fa1[X] != -1) { int lb = idx[lb1[fa1[X]]]; int rb = idx[rb1[fa1[X]]] + 1; sum += tree.query(lb, rb); tree.clear(lb, rb); } if (lb1[X] != -1) { int lb = idx[fa1[X]]; int rb = idx[fa1[X]] + 1; sum += tree.query(lb, rb); tree.clear(lb, rb); lb = idx[lb1[X]]; rb = idx[rb1[X]] + 1; sum += tree.query(lb, rb); tree.clear(lb, rb); } else { int lb = idx[X]; int rb = idx[X] + 1; sum += tree.query(lb, rb); tree.clear(lb, rb); } if (lb2[X] != -1) { int lb = idx[lb2[X]]; int rb = idx[rb2[X]] + 1; sum += tree.query(lb, rb); tree.clear(lb, rb); } tree.add(sum, idx[X]); cout << tree.query(idx[X], idx[X] + 1) << endl; } return 0; }