結果
問題 | No.1038 TreeAddQuery |
ユーザー | 👑 emthrm |
提出日時 | 2020-07-30 17:41:58 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 5,331 bytes |
コンパイル時間 | 3,145 ms |
コンパイル使用メモリ | 236,732 KB |
実行使用メモリ | 263,516 KB |
最終ジャッジ日時 | 2024-07-04 10:29:45 |
合計ジャッジ時間 | 55,869 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 22 ms
6,944 KB |
testcase_04 | AC | 24 ms
6,940 KB |
testcase_05 | AC | 23 ms
6,940 KB |
testcase_06 | AC | 20 ms
6,940 KB |
testcase_07 | AC | 27 ms
6,944 KB |
testcase_08 | AC | 2,536 ms
171,292 KB |
testcase_09 | AC | 2,991 ms
186,400 KB |
testcase_10 | AC | 3,008 ms
189,348 KB |
testcase_11 | AC | 2,973 ms
186,996 KB |
testcase_12 | AC | 3,129 ms
193,664 KB |
testcase_13 | TLE | - |
testcase_14 | TLE | - |
testcase_15 | AC | 3,817 ms
222,376 KB |
testcase_16 | AC | 3,711 ms
218,644 KB |
testcase_17 | AC | 3,580 ms
215,332 KB |
testcase_18 | AC | 595 ms
92,868 KB |
testcase_19 | AC | 841 ms
100,512 KB |
testcase_20 | AC | 833 ms
102,692 KB |
testcase_21 | AC | 979 ms
109,816 KB |
testcase_22 | AC | 1,390 ms
130,208 KB |
testcase_23 | AC | 1,600 ms
137,376 KB |
testcase_24 | AC | 2,464 ms
176,112 KB |
testcase_25 | TLE | - |
testcase_26 | AC | 2,432 ms
181,148 KB |
ソースコード
#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<bool> visited(n, false); vector<unordered_map<int, int>> mp(n), minus(n); vector<vector<int>> resp(n), depth(n, vector<int>{0}); vector<int> ds{0}; int sum_dep = 0, idx = -1; vector<BITRangeAdd<ll>> bit; function<void(int)> rec = [&](int root) { ++idx; visited[root] = true; mp[idx][root] = 0; resp[root].emplace_back(idx); vector<int> que{root}; for (int dep = 1; !que.empty(); ++dep) { vector<int> nx; for (int ver : que) { for (int e : graph[ver]) { if (!visited[e] && mp[idx].count(e) == 0) { int sz = mp[idx].size(); mp[idx][e] = sz; resp[e].emplace_back(idx); depth[idx].emplace_back(dep); nx.emplace_back(e); } } } que.swap(nx); } bit.emplace_back(mp[idx].size()); minus[root][idx] = -1; for (int dst : graph[root]) { if (visited[dst]) continue; int max_depth = 0; function<void(int, int, int)> dfs = [&](int par, int ver, int dep) { chmax(max_depth, dep); minus[ver][idx] = sum_dep + dep; for (int e : graph[ver]) { if (!visited[e] && e != par) dfs(ver, e, dep + 1); } }; dfs(root, dst, 0); sum_dep += max_depth + 1; ds.emplace_back(sum_dep); } for (int e : cd.comp[root]) { if (!visited[e]) rec(e); } }; rec(cd.root); BITRangeAdd<ll> bit2(sum_dep + 1); while (q--) { int x, y, z; cin >> x >> y >> z; --x; ll ans = 0; for (int idx : resp[x]) { ans += bit[idx][mp[idx][x]]; if (minus[x][idx] != -1) ans -= bit2[minus[x][idx]]; } cout << ans << '\n'; for (int idx : resp[x]) { int ver = mp[idx][x], dist = y - depth[idx][ver]; if (dist >= 0) { bit[idx].add(0, upper_bound(ALL(depth[idx]), dist) - depth[idx].begin(), z); if (dist > 0 && minus[x][idx] != -1) { auto it = upper_bound(ALL(ds), minus[x][idx]); int l = *prev(it), r = *it - 1; bit2.add(l, min(l + dist - 1, r) + 1, z); } } } } return 0; }