結果

問題 No.899 γatheree
ユーザー 0w1
提出日時 2019-10-10 18:56:16
言語 C++17(1z)
(gcc 8.3.0)
結果
WA   .
実行時間 -
コード長 3,705 Byte
コンパイル時間 2,605 ms
使用メモリ 13,248 KB
最終ジャッジ日時 2019-10-10 18:56:30

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
sample01 AC 6 ms
3,940 KB
test_1_01 WA -
test_1_02 WA -
test_1_03 WA -
test_1_04 WA -
test_1_05 WA -
test_2_01 WA -
test_2_02 WA -
test_2_03 WA -
test_2_04 WA -
test_2_05 WA -
test_2_06 WA -
test_2_07 WA -
test_2_08 WA -
test_2_09 WA -
test_2_10 WA -
test_2_11 WA -
test_2_12 WA -
test_2_13 WA -
test_2_14 WA -
test_2_15 WA -
test_3_01 WA -
test_3_02 WA -
test_3_03 WA -
テストケース一括ダウンロード

ソースコード

diff #
#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;
}
0