結果

問題 No.1038 TreeAddQuery
ユーザー 👑 emthrmemthrm
提出日時 2020-07-30 19:13:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,270 ms / 4,000 ms
コード長 5,326 bytes
コンパイル時間 4,161 ms
コンパイル使用メモリ 242,584 KB
実行使用メモリ 124,440 KB
最終ジャッジ日時 2023-09-17 21:06:41
合計ジャッジ時間 29,482 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 12 ms
4,564 KB
testcase_04 AC 13 ms
4,692 KB
testcase_05 AC 14 ms
4,936 KB
testcase_06 AC 12 ms
4,484 KB
testcase_07 AC 15 ms
4,924 KB
testcase_08 AC 997 ms
66,368 KB
testcase_09 AC 1,223 ms
73,424 KB
testcase_10 AC 1,246 ms
72,084 KB
testcase_11 AC 1,191 ms
70,472 KB
testcase_12 AC 1,213 ms
72,460 KB
testcase_13 AC 2,169 ms
124,400 KB
testcase_14 AC 1,797 ms
83,876 KB
testcase_15 AC 1,664 ms
80,004 KB
testcase_16 AC 1,667 ms
78,788 KB
testcase_17 AC 1,499 ms
75,716 KB
testcase_18 AC 291 ms
31,256 KB
testcase_19 AC 375 ms
36,164 KB
testcase_20 AC 370 ms
36,340 KB
testcase_21 AC 421 ms
36,496 KB
testcase_22 AC 545 ms
45,996 KB
testcase_23 AC 597 ms
45,716 KB
testcase_24 AC 888 ms
67,228 KB
testcase_25 AC 2,270 ms
124,440 KB
testcase_26 AC 1,191 ms
81,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

struct CentroidDecomposition {
  int root;
  vector<vector<int>> comp;
  vector<int> par;

  CentroidDecomposition(const vector<vector<int>> &graph) : graph(graph) {
    int n = graph.size();
    alive.assign(n, true);
    subtree.resize(n);
    comp.resize(n);
    par.assign(n, -1);
    root = build(0);
  }

private:
  const vector<vector<int>> graph;
  vector<bool> alive;
  vector<int> subtree;

  int build(int s) {
    int centroid = search_centroid(-1, s, calc_subtree(-1, s) / 2);
    alive[centroid] = false;
    for (int e : graph[centroid]) {
      if (alive[e]) {
        comp[centroid].emplace_back(build(e));
        par[e] = centroid;
      }
    }
    alive[centroid] = true;
    return centroid;
  }

  int calc_subtree(int par, int ver) {
    subtree[ver] = 1;
    for (int e : graph[ver]) {
      if (e != par && alive[e]) subtree[ver] += calc_subtree(ver, e);
    }
    return subtree[ver];
  }

  int search_centroid(int par, int ver, int half) {
    for (int e : graph[ver]) {
      if (e != par && alive[e]) {
        if (subtree[e] > half) return search_centroid(ver, e, half);
      }
    }
    return ver;
  }
};

template <typename Abelian>
struct BITRangeAdd {
  BITRangeAdd(int n_, const Abelian UNITY = 0) : n(n_), UNITY(UNITY) {
    ++n;
    dat_const.assign(n, UNITY);
    dat_linear.assign(n, UNITY);
  }

  void add(int left, int right, Abelian val) {
    if (right < ++left) return;
    for (int i = left; i < n; i += i & -i) {
      dat_const[i] -= val * (left - 1);
      dat_linear[i] += val;
    }
    for (int i = right + 1; i < n; i += i & -i) {
      dat_const[i] += val * right;
      dat_linear[i] -= val;
    }
  }

  Abelian sum(int idx) {
    Abelian res = UNITY;
    for (int i = idx; i > 0; i -= i & -i) res += dat_linear[i];
    res *= idx;
    for (int i = idx; i > 0; i -= i & -i) res += dat_const[i];
    return res;
  }

  Abelian sum(int left, int right) {
    if (right <= left) return UNITY;
    return sum(right) - sum(left);
  }

  Abelian operator[](const int idx) { return sum(idx, idx + 1); }

  int n;
private:
  const Abelian UNITY;
  vector<Abelian> dat_const, dat_linear;
};

int main() {
  int n, q; cin >> n >> q;
  vector<vector<int>> graph(n);
  REP(_, n - 1) {
    int a, b; cin >> a >> b; --a; --b;
    graph[a].emplace_back(b);
    graph[b].emplace_back(a);
  }
  CentroidDecomposition cd(graph);
  vector visited(n, false);
  vector<vector<tuple<int, int, int, int, int, int>>> resp(n);
  int idx = 0, sum_dep = 0;
  function<void(int)> rec = [&](int root) {
    visited[root] = true;
    map<int, int> mp;
    int mx = 0;
    vector<pair<int, int>> que{{-1, root}};
    for (int dep = 1; !que.empty(); ++dep) {
      vector<pair<int, int>> nx;
      for (auto [par, ver] : que) {
        for (int e : graph[ver]) {
          if (!visited[e] && e != par) {
            mp[e] = dep;
            chmax(mx, dep);
            nx.emplace_back(ver, e);
          }
        }
      }
      que.swap(nx);
    }
    resp[root].emplace_back(0, idx, idx + mx, -1, -1, -1);
    for (int dst : graph[root]) {
      if (visited[dst]) continue;
      int mx_child = 0;
      vector<pair<int, int>> vs;
      function<void(int, int, int)> dfs = [&](int par, int ver, int dep) {
        chmax(mx_child, dep);
        vs.emplace_back(ver, dep);
        for (int e : graph[ver]) {
          if (!visited[e] && e != par) dfs(ver, e, dep + 1);
        }
      };
      dfs(root, dst, 0);
      for (auto [ver, dep] : vs) resp[ver].emplace_back(mp[ver], idx, idx + mx, sum_dep + dep, sum_dep, sum_dep + mx_child);
      sum_dep += mx_child + 1;
    }
    idx += mx + 1;
    for (int e : cd.comp[root]) {
      if (!visited[e]) rec(e);
    }
  };
  rec(cd.root);
  // REP(i, n) {
  //   cout << i << ":\n";
  //   for (auto [dep, l_add, r_add, idx, l_sub, r_sub] : resp[i]) {
  //     cout << dep << ' ' << l_add << ' ' << r_add << ' ' << idx << ' ' << l_sub << ' ' << r_sub << '\n';
  //   }
  // }
  BITRangeAdd<ll> bit1(idx), bit2(sum_dep);
  while (q--) {
    int x, y, z; cin >> x >> y >> z; --x;
    ll ans = 0;
    for (auto tpl : resp[x]) {
      ans += bit1[get<1>(tpl) + get<0>(tpl)];
      if (get<3>(tpl) != -1) ans -= bit2[get<3>(tpl)];
    }
    cout << ans << '\n';
    for (auto [dep, l_add, r_add, idx, l_sub, r_sub] : resp[x]) {
      int dist = y - dep;
      bit1.add(l_add, min(l_add + dist, r_add) + 1, z);
      if (idx != -1) bit2.add(l_sub, min(l_sub + dist - 1, r_sub) + 1, z);
    }
  }
  return 0;
}
0