結果

問題 No.899 γatheree
ユーザー pekempeypekempey
提出日時 2019-10-04 22:03:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 410 ms / 2,000 ms
コード長 2,689 bytes
コンパイル時間 2,263 ms
コンパイル使用メモリ 177,412 KB
実行使用メモリ 18,064 KB
最終ジャッジ日時 2023-07-26 21:17:29
合計ジャッジ時間 11,076 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,460 KB
testcase_01 AC 3 ms
8,444 KB
testcase_02 AC 4 ms
8,496 KB
testcase_03 AC 4 ms
8,556 KB
testcase_04 AC 4 ms
8,616 KB
testcase_05 AC 4 ms
8,440 KB
testcase_06 AC 383 ms
17,700 KB
testcase_07 AC 398 ms
17,788 KB
testcase_08 AC 400 ms
17,704 KB
testcase_09 AC 394 ms
17,648 KB
testcase_10 AC 399 ms
17,704 KB
testcase_11 AC 392 ms
17,704 KB
testcase_12 AC 403 ms
17,636 KB
testcase_13 AC 388 ms
17,704 KB
testcase_14 AC 387 ms
17,600 KB
testcase_15 AC 377 ms
17,780 KB
testcase_16 AC 386 ms
17,588 KB
testcase_17 AC 410 ms
17,704 KB
testcase_18 AC 389 ms
17,656 KB
testcase_19 AC 397 ms
17,608 KB
testcase_20 AC 391 ms
17,784 KB
testcase_21 AC 354 ms
17,848 KB
testcase_22 AC 344 ms
18,064 KB
testcase_23 AC 354 ms
17,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rep2(i, l, r) for (int i = (l); i < (r); i++)
#define rep2r(i, l, r) for (int i = (r) - 1; i >= (l); i--)
#define range(a) a.begin(), a.end()

using namespace std;
using ll = long long;

constexpr int U = 1 << 17;
ll dat[U * 2];
ll lazy[U * 2];
ll cnt[U * 2];

void apply(int k, ll v) {
  if (v == -1) return;
  dat[k] = v * cnt[k];
  lazy[k] = v;
}

void push(int k) {
  apply(k * 2 + 0, lazy[k]);
  apply(k * 2 + 1, lazy[k]);
  lazy[k] = -1;
}

void update(int a, int b, ll v, int k = 1, int l = 0, int r = U) {
  if (r <= a || b <= l) return;
  if (a <= l && r <= b) {
    apply(k, v);
    return;
  }
  push(k);
  update(a, b, v, k * 2 + 0, l, (l + r) / 2);
  update(a, b, v, k * 2 + 1, (l + r) / 2, r);
  dat[k] = dat[k * 2] + dat[k * 2 + 1];
}

ll query(int a, int b, int k = 1, int l = 0, int r = U) {
  if (r <= a || b <= l) return 0;
  if (a <= l && r <= b) return dat[k];
  push(k);
  ll vl = query(a, b, k * 2 + 0, l, (l + r) / 2);
  ll vr = query(a, b, k * 2 + 1, (l + r) / 2, r);
  return vl + vr;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  int N; cin >> N;
  vector<vector<int>> G(N);
  rep(i, N - 1) {
    int a, b; cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  int k = 0;
  vector<int> par(N, -1);
  vector<int> L(N);
  vector<int> R(N);
  vector<int> L2(N, 1e9);
  vector<int> R2(N, -1e9);
  vector<int> label(N);
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int u = q.front(); q.pop();
    L[u] = k + 1;
    for (int v : G[u]) if (v != par[u]) {
      label[v] = ++k;
      par[v] = u;
      q.push(v);
    }
    R[u] = k + 1;
  }
  rep(u, N) for (int v : G[u]) if (v != par[u]) {
    L2[u] = min(L2[u], L[v]);
    R2[u] = max(R2[u], R[v]);
  };
  vector<ll> A(N); rep(i, N) cin >> A[i];
  rep(i, N) {
    dat[label[i] + U] = A[i];
    cnt[i + U] = 1;
  }
  rep(i, U * 2) lazy[i] = -1;
  for (int i = U - 1; i >= 1; i--) {
    dat[i] = dat[i * 2] + dat[i * 2 + 1];
    cnt[i] = cnt[i * 2] + cnt[i * 2 + 1];
  }
  int Q; cin >> Q;
  while (Q--) {
    int x; cin >> x;
    ll s = 0;
    auto f = [&](int l, int r) {
      if (l <= r) {
        s += query(l, r);
        update(l, r, 0);
      }
    };
    f(L[x], R[x]);
    f(L2[x], R2[x]);
    if (par[x] != -1) {
      int p = par[x];
      f(label[p], label[p] + 1);
      f(L[p], R[p]);
      if (par[p] != -1) {
        int pp = par[p];
        f(label[pp], label[pp] + 1);
      }
    }
    f(label[x], label[x] + 1);
    update(label[x], label[x] + 1, s);
    cout << s << '\n';
  }
}
0