結果

問題 No.900 aδδitivee
ユーザー risujirohrisujiroh
提出日時 2019-10-04 21:40:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 170 ms / 2,000 ms
コード長 4,481 bytes
コンパイル時間 1,949 ms
コンパイル使用メモリ 182,344 KB
実行使用メモリ 24,748 KB
最終ジャッジ日時 2023-07-26 20:59:10
合計ジャッジ時間 8,483 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 165 ms
22,444 KB
testcase_08 AC 168 ms
22,180 KB
testcase_09 AC 170 ms
22,252 KB
testcase_10 AC 165 ms
22,276 KB
testcase_11 AC 168 ms
22,320 KB
testcase_12 AC 168 ms
22,100 KB
testcase_13 AC 167 ms
22,124 KB
testcase_14 AC 170 ms
22,168 KB
testcase_15 AC 165 ms
22,352 KB
testcase_16 AC 164 ms
22,244 KB
testcase_17 AC 165 ms
22,248 KB
testcase_18 AC 164 ms
22,324 KB
testcase_19 AC 167 ms
22,448 KB
testcase_20 AC 164 ms
22,260 KB
testcase_21 AC 166 ms
22,308 KB
testcase_22 AC 128 ms
24,560 KB
testcase_23 AC 129 ms
24,748 KB
testcase_24 AC 129 ms
24,564 KB
testcase_25 AC 129 ms
24,368 KB
testcase_26 AC 130 ms
24,700 KB
testcase_27 AC 132 ms
24,372 KB
testcase_28 AC 130 ms
24,488 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:15:27: 警告: use of ‘auto’ in parameter declaration only available with ‘-std=c++20’ or ‘-fconcepts’
   15 |     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}; }

  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]);
  }
};

// 部分木vは[in[v],out[v])に対応
struct HeavyLightDecomposition {
  const bool edge;
  const int n;
  VV<> g;
  V<> sz, par, in, out, anc, rev;
  HeavyLightDecomposition(VV<>& g, bool edge, int r = 0) :
    edge(edge), n(g.size()), g(g), sz(n), par(n), in(n), out(n), anc(n), rev(n) {
    dfs_sz(r, -1);
    anc[r] = r;
    dfs_hl(r);
  }
  void dfs_sz(int v, int p) {
    par[v] = p;
    for (auto&& w : g[v]) if (w == p) {
      swap(w, g[v].back());
      g[v].pop_back();
      break;
    }
    sz[v] = 1;
    for (auto&& w : g[v]) {
      dfs_sz(w, v);
      sz[v] += sz[w];
      if (sz[w] > sz[g[v][0]]) swap(w, g[v][0]);
    }
  }
  void dfs_hl(int v) {
    static int t = 0;
    in[v] = t++;
    rev[in[v]] = v;
    for (int w : g[v]) {
      anc[w] = w == g[v][0] ? anc[v] : w;
      dfs_hl(w);
    }
    out[v] = t;
  }
  int lca(int u, int v) const {
    while (true) {
      if (in[u] > in[v]) swap(u, v);
      if (anc[u] == anc[v]) return u;
      v = par[anc[v]];
    }
  }
  auto acc(SegmentTree& st, int u, int v) const {
    auto res = st.e();
    while (true) {
      if (in[u] > in[v]) swap(u, v);
      if (anc[u] == anc[v]) break;
      res = st.op(res, st.acc(in[anc[v]], in[v] + 1));
      v = par[anc[v]];
    }
    res = st.op(res, st.acc(in[u] + edge, in[v] + 1));
    return res;
  }
  void act(SegmentTree& st, int u, int v, const SegmentTree::U& f) const {
    while (true) {
      if (in[u] > in[v]) swap(u, v);
      if (anc[u] == anc[v]) break;
      st.act(in[anc[v]], in[v] + 1, f);
      v = par[anc[v]];
    }
    st.act(in[u] + edge, in[v] + 1, f);
  }
};

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int n; cin >> n;
  VV<> g(n);
  V<> u(n - 1), v(n - 1), w(n - 1);
  for (int i = 0; i < n - 1; ++i) {
    cin >> u[i] >> v[i] >> w[i];
    g[u[i]].push_back(v[i]);
    g[v[i]].push_back(u[i]);
  }
  HeavyLightDecomposition hld(g, true);
  SegmentTree st(n);
  for (int i = 0; i < n; ++i) st[i] = {0, 1};
  st.build();
  for (int i = 0; i < n - 1; ++i) {
    hld.act(st, u[i], v[i], {1, w[i]});
  }
  int q; cin >> q;
  while (q--) {
    int tp; cin >> tp;
    if (tp == 1) {
      int v, x; cin >> v >> x;
      st.act(hld.in[v] + 1, hld.out[v], {1, x});
    } else {
      int v; cin >> v;
      cout << hld.acc(st, 0, v).sum << '\n';
    }
  }
}
0