結果

問題 No.899 γatheree
ユーザー risujirohrisujiroh
提出日時 2019-10-04 22:50:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 339 ms / 2,000 ms
コード長 4,185 bytes
コンパイル時間 2,009 ms
コンパイル使用メモリ 182,000 KB
実行使用メモリ 19,372 KB
最終ジャッジ日時 2023-07-26 21:57:43
合計ジャッジ時間 8,245 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 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 234 ms
18,784 KB
testcase_07 AC 236 ms
18,836 KB
testcase_08 AC 230 ms
18,840 KB
testcase_09 AC 225 ms
18,760 KB
testcase_10 AC 227 ms
18,780 KB
testcase_11 AC 234 ms
18,968 KB
testcase_12 AC 228 ms
18,828 KB
testcase_13 AC 230 ms
18,760 KB
testcase_14 AC 238 ms
18,832 KB
testcase_15 AC 225 ms
18,828 KB
testcase_16 AC 235 ms
18,792 KB
testcase_17 AC 241 ms
18,844 KB
testcase_18 AC 254 ms
18,744 KB
testcase_19 AC 252 ms
18,820 KB
testcase_20 AC 339 ms
18,744 KB
testcase_21 AC 229 ms
19,296 KB
testcase_22 AC 211 ms
19,284 KB
testcase_23 AC 209 ms
19,372 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:14:27: 警告: use of ‘auto’ in parameter declaration only available with ‘-std=c++20’ or ‘-fconcepts’
   14 |     bool operator==(const auto& r) const { return p == r.p and q == r.q; }
      |                           ^~~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

struct SegmentTree {
  using D = lint;
  using T = struct { D sum, sz; };
  static T op(const T& a, const T& b) { return {a.sum + b.sum, a.sz + b.sz}; }
  static constexpr T e() { return {0, 0}; }
  using U = struct {
    D p, q;
    bool operator==(const auto& r) const { return p == r.p and q == r.q; }
  };
  static void ap(const U& f, T& a) { a.sum = f.p * a.sum + f.q * a.sz; }
  static void cp(const U& g, U& f) { f.p *= g.p, f.q = g.p * f.q + g.q; }
  static constexpr U id() { return {1, 0}; }
  D sum(int l, int r) { return acc(l, r).sum; }
  void add(int l, int r, D x) { act(l, r, {1, x}); }
  void mul(int l, int r, D x) { act(l, r, {x, 0}); }
  void update(int l, int r, D x) { act(l, r, {0, x}); }

  const int n;
  V<T> t;
  V<U> u;
  SegmentTree(int n) : n(n), t(2 * n, e()), u(n, id()) {}
  T& operator[](int i) { return t[i + n]; }
  void build() { for (int i = n - 1; i; --i) t[i] = op(t[2 * i], t[2 * i + 1]); }
  void push() { for (int i = 1; i < n; ++i) push(i); }
  void apply(const U& f, int i) {
    ap(f, t[i]);
    if (i < n) cp(f, u[i]);
  }
  void push(int i) {
    if (u[i] == id()) return;
    apply(u[i], 2 * i);
    apply(u[i], 2 * i + 1);
    u[i] = id();
  }
  void push(int l, int r) {
    for (int hl = __lg(l + n), hr = __lg(r - 1 + n); hr > 0; --hl, --hr) {
      int al = l + n >> hl, ar = r - 1 + n >> hr;
      if (al < n) push(al);
      if (ar != al) push(ar);
    }
  }
  T acc(int l, int r) {
    push(l, r);
    T resl = e(), resr = e();
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) resl = op(resl, t[l++]);
      if (r & 1) resr = op(t[--r], resr);
    }
    return op(resl, resr);
  }
  T get(int i) { return acc(i, i + 1); }
  void act(int l, int r, const U& f) {
    push(l, r);
    for (int i = l + n, j = r + n; i < j; i >>= 1, j >>= 1) {
      if (i & 1) apply(f, i++);
      if (j & 1) apply(f, --j);
    }
    l = l + n >> __builtin_ctz(l + n);
    while (l >>= 1) t[l] = op(t[2 * l], t[2 * l + 1]);
    r = r + n >> __builtin_ctz(r + n);
    while (r >>= 1) t[r] = op(t[2 * r], t[2 * r + 1]);
  }
  void set(int i, const T& a) {
    push(i, i + 1);
    t[i += n] = a;
    while (i >>= 1) t[i] = op(t[2 * i], t[2 * i + 1]);
  }
};

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int n; cin >> n;
  VV<> g(n);
  for (int _ = 0; _ < n - 1; ++_) {
    int u, v; cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  VV<> ch(n);
  V<> par(n);
  auto dfs = [&](const auto& dfs, int v, int p) -> void {
    par[v] = p;
    for (int u : g[v]) if (u != p) {
      ch[v].push_back(u);
      dfs(dfs, u, v);
    }
  };
  dfs(dfs, 0, -1);

  queue<int> que;
  que.push(0);
  int t = 0;
  V<> ord(n);
  while (!que.empty()) {
    int v = que.front(); que.pop();
    ord[v] = t++;
    for (int u : ch[v]) {
      que.push(u);
    }
  }

  SegmentTree st(n);
  for (int v = 0; v < n; ++v) {
    int a; cin >> a;
    st[ord[v]] = {a, 1};
  }
  st.build();

  V<> l(n, n), r(n, -1);
  for (int v = 0; v < n; ++v) {
    int p = v;
    bool ok = true;
    for (int _ = 0; _ < 2; ++_) if (par[p] != -1) {
      p = par[p];
    } else {
      ok = false;
      break;
    }
    if (!ok) continue;
    l[p] = min(l[p], ord[v]);
    r[p] = max(r[p], ord[v]);
  }

  int q; cin >> q;
  while (q--) {
    int v; cin >> v;
    int p = v;
    lint res = 0;
    for (int _ = 0; _ < 2; ++_) if (par[p] != -1) {
      p = par[p];
      res += st.get(ord[p]).sum;
      st.set(ord[p], {0, 1});
    }
    if (!ch[v].empty()) {
      res += st.acc(ord[ch[v][0]], ord[ch[v].back()] + 1).sum;
      st.act(ord[ch[v][0]], ord[ch[v].back()] + 1, {0, 0});
    }
    if (l[v] <= r[v]) {
      res += st.acc(l[v], r[v] + 1).sum;
      st.act(l[v], r[v] + 1, {0, 0});
    }
    if (par[v] != -1) {
      res += st.acc(ord[ch[par[v]][0]], ord[ch[par[v]].back()] + 1).sum;
      st.act(ord[ch[par[v]][0]], ord[ch[par[v]].back()] + 1, {0, 0});
    } else {
      res += st.get(ord[v]).sum;
    }
    st.set(ord[v], {res, 1});
    cout << res << '\n';
  }
}
0