結果

問題 No.899 γatheree
ユーザー niuezniuez
提出日時 2019-09-17 22:45:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 700 ms / 2,000 ms
コード長 3,879 bytes
コンパイル時間 2,833 ms
コンパイル使用メモリ 188,664 KB
実行使用メモリ 20,696 KB
最終ジャッジ日時 2023-09-21 20:21:08
合計ジャッジ時間 16,821 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 654 ms
20,412 KB
testcase_07 AC 687 ms
20,368 KB
testcase_08 AC 691 ms
20,540 KB
testcase_09 AC 700 ms
20,540 KB
testcase_10 AC 695 ms
20,488 KB
testcase_11 AC 696 ms
20,368 KB
testcase_12 AC 663 ms
20,396 KB
testcase_13 AC 660 ms
20,416 KB
testcase_14 AC 649 ms
20,336 KB
testcase_15 AC 644 ms
20,508 KB
testcase_16 AC 651 ms
20,508 KB
testcase_17 AC 653 ms
20,532 KB
testcase_18 AC 654 ms
20,360 KB
testcase_19 AC 657 ms
20,504 KB
testcase_20 AC 684 ms
20,652 KB
testcase_21 AC 641 ms
20,664 KB
testcase_22 AC 660 ms
20,600 KB
testcase_23 AC 662 ms
20,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
#define all(x) x.begin(),x.end()
#define let auto const

struct lazy_segment_tree {
  using T = i64;
  using L = i64;
  T t_ide() { return 0; }
  L l_ide() { return 0; }
  T ope(const T& a, const T& b) { return a + b; }
  L lazy_ope(const L& a, const L& b) { return b; }
  T effect(const T& t, const L& l, const i64 len) { return l * len; }

  i64 n;
  vector<T> node;
  vector<L> lazy;
  vector<bool> flag;

  /* create lazy_segment_tree for sequence [t_ide; sz]A */
  lazy_segment_tree(vector<T> init) {
    n = 1;
    while(n < init.size()) n *= 2;
    node.assign(n * 2, t_ide());
    lazy.assign(n * 2, l_ide());
    flag.assign(n * 2, false);
    for(int i = 0;i < init.size();i++) node[i + n - 1] = init[i];
    for(int i = n - 2;i >= 0;i--) node[i] = ope(node[i * 2 + 1], node[i * 2 + 2]);
  }
  

  void eval(i64 l, i64 r, i64 k) {
    if(flag[k]) {
      node[k] = effect(node[k], lazy[k], r - l);
      
      if(r - l > 1) {
        lazy[k * 2 + 1] = lazy_ope(lazy[k * 2 + 1], lazy[k]);
        flag[k * 2 + 1] = true;
        lazy[k * 2 + 2] = lazy_ope(lazy[k * 2 + 2], lazy[k]);
        flag[k * 2 + 2] = true;
      }

      flag[k] = false;
      lazy[k] = l_ide();
    }
  }
  
  /* for i in [a, b) effect(a[i], lx) */
  void update(i64 a, i64 b, L lx, i64 l = 0, i64 r = -1, i64 k = 0) {
    if(r < 0) r = n;
    eval(l, r, k);
    if(b <= l || r <= a) return;
    else if(a <= l && r <= b) {
      lazy[k] = lazy_ope(lazy[k], lx);
      flag[k] = true;
      eval(l, r, k);
    }
    else {
      update(a, b, lx, l, (l + r) / 2, k * 2 + 1);
      update(a, b, lx, (l + r) / 2, r, k * 2 + 2);
      node[k] =ope(node[k * 2 + 1], node[k * 2 + 2]);
    }
  }
  
  /* the sum of a[i] for i in [a, b) */
  T get(i64 a, i64 b, i64 l = 0, i64 r = -1, i64 k = 0) {
    if(r < 0) r = n;
    eval(l, r, k);
    if(b <= l || r <= a) return t_ide();
    else if(a <= l && r <= b) return node[k];
    else return ope(get(a, b, l, (l + r) / 2, k * 2 + 1), get(a, b, (l + r) / 2, r, k * 2 + 2));
  }
};

vector<i64> vec;
vector<i64> idx;
vector<i64> L1;
vector<i64> R1;
vector<i64> L2;
vector<i64> R2;
vector<i64> p;
vector<vector<i64>> G;


int main() {
  i64 N;
  cin >> N;
  idx.resize(N + 1, -1);
  L1.resize(N + 1, -1);
  R1.resize(N + 1, -1);
  L2.resize(N + 1, -1);
  R2.resize(N + 1, -1);
  p.resize(N + 1, -1);
  G.resize(N + 1);
  for(int i = 0;i < N - 1; i++) {
    i64 a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  G[N].push_back(0);

  queue<i64> que;
  que.push(N);
  idx[N] = vec.size();
  vec.push_back(N);
  while(!que.empty()) {
    i64 v = que.front();
    que.pop();
    for(auto x: G[v]) {
      if(idx[x] != -1) continue;
      que.push(x);
      idx[x] = vec.size();
      vec.push_back(x);
      p[x] = v;

      if(L1[v] == -1) L1[v] = idx[x];
      R1[v] = idx[x] + 1;

      i64 pp = p[v];
      if(pp != -1) {
        if(L2[pp] == -1) L2[pp] = idx[x];
        R2[pp] = idx[x] + 1;
      }
    }
  }
  vector<i64> A(N + 1, 0);
  for(int i = 0;i < N;i++) cin >> A[idx[i]];
  lazy_segment_tree seg(A);

  i64 Q;
  cin >> Q;
  for(int q = 0;q < Q;q++) {
    i64 x;
    cin >> x;
    i64 ad = 0;
    if(p[p[x]] != -1) {
      ad += seg.get(idx[p[p[x]]], idx[p[p[x]]] + 1);
    }
    ad += seg.get(idx[p[x]], idx[p[x]] + 1);
    ad += seg.get(L1[p[x]], R1[p[x]]);
    if(L1[x] != -1) ad += seg.get(L1[x], R1[x]);
    if(L2[x] != -1) ad += seg.get(L2[x], R2[x]);
    if(p[p[x]] != -1) {
      seg.update(idx[p[p[x]]], idx[p[p[x]]] + 1, 0);
    }
    seg.update(idx[p[x]], idx[p[x]] + 1, 0);
    seg.update(L1[p[x]], R1[p[x]], 0);
    if(L1[x] != -1) seg.update(L1[x], R1[x], 0);
    if(L2[x] != -1) seg.update(L2[x], R2[x], 0);
    seg.update(idx[x], idx[x] + 1, ad);

    cout << ad << endl;
  }
}
0