結果
問題 | No.1038 TreeAddQuery |
ユーザー | 👑 emthrm |
提出日時 | 2020-06-17 09:06:11 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 7,271 bytes |
コンパイル時間 | 3,499 ms |
コンパイル使用メモリ | 254,964 KB |
実行使用メモリ | 301,516 KB |
最終ジャッジ日時 | 2024-07-03 12:07:14 |
合計ジャッジ時間 | 59,014 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 20 ms
6,944 KB |
testcase_04 | AC | 23 ms
7,168 KB |
testcase_05 | AC | 22 ms
7,648 KB |
testcase_06 | AC | 19 ms
6,940 KB |
testcase_07 | AC | 25 ms
7,388 KB |
testcase_08 | AC | 2,650 ms
203,856 KB |
testcase_09 | AC | 3,035 ms
222,480 KB |
testcase_10 | AC | 3,166 ms
224,688 KB |
testcase_11 | AC | 3,301 ms
222,128 KB |
testcase_12 | AC | 3,289 ms
228,336 KB |
testcase_13 | TLE | - |
testcase_14 | TLE | - |
testcase_15 | TLE | - |
testcase_16 | TLE | - |
testcase_17 | AC | 3,907 ms
249,140 KB |
testcase_18 | AC | 541 ms
94,500 KB |
testcase_19 | AC | 737 ms
105,424 KB |
testcase_20 | AC | 737 ms
105,008 KB |
testcase_21 | AC | 900 ms
118,500 KB |
testcase_22 | AC | 1,295 ms
147,568 KB |
testcase_23 | AC | 1,584 ms
156,440 KB |
testcase_24 | AC | 2,601 ms
212,620 KB |
testcase_25 | TLE | - |
testcase_26 | AC | 2,480 ms
200,824 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; using CostType = bool; struct Edge { int src, dst; CostType cost; Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {} inline bool operator<(const Edge &x) const { return cost != x.cost ? cost < x.cost : dst != x.dst ? dst < x.dst : src < x.src; } inline bool operator<=(const Edge &x) const { return !(x < *this); } inline bool operator>(const Edge &x) const { return x < *this; } inline bool operator>=(const Edge &x) const { return !(*this < x); } }; struct CentroidDecomposition { int root; vector<vector<int>> comp; vector<int> par; CentroidDecomposition(const vector<vector<Edge>> &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<Edge>> 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 (const Edge &e : graph[centroid]) { if (alive[e.dst]) { comp[centroid].emplace_back(build(e.dst)); par[e.dst] = centroid; } } alive[centroid] = true; return centroid; } int calc_subtree(int par, int ver) { subtree[ver] = 1; for (const Edge &e : graph[ver]) { if (e.dst != par && alive[e.dst]) subtree[ver] += calc_subtree(ver, e.dst); } return subtree[ver]; } int search_centroid(int par, int ver, int half) { for (const Edge &e : graph[ver]) { if (e.dst != par && alive[e.dst]) { if (subtree[e.dst] > half) return search_centroid(ver, e.dst, 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<Edge>> graph(n); REP(_, n - 1) { int a, b; cin >> a >> b; --a; --b; graph[a].emplace_back(a, b); graph[b].emplace_back(b, a); } CentroidDecomposition cd(graph); vector<bool> visited(n, false); vector<map<int, int>> mp; vector<vector<int>> resp(n), depth; vector<BITRangeAdd<ll>> bit; vector<map<int, pair<int, int>>> minus(n); vector<vector<BITRangeAdd<ll>>> bit2; function<void(int)> rec = [&](int root) { // cout << root << '\n'; visited[root] = true; int idx = mp.size(); mp.emplace_back(); mp[idx][root] = 0; resp[root].emplace_back(idx); depth.emplace_back(vector<int>{0}); vector<int> que{root}; for (int dep = 1; !que.empty(); ++dep) { vector<int> nx; for (int ver : que) { for (const Edge &e : graph[ver]) { if (!visited[e.dst] && mp[idx].count(e.dst) == 0) { // cout << root << ' ' << e.dst << '\n'; int sz = mp[idx].size(); mp[idx][e.dst] = sz; resp[e.dst].emplace_back(idx); depth[idx].emplace_back(dep); nx.emplace_back(e.dst); } } } que.swap(nx); } bit.emplace_back(mp[idx].size()); // cout << root << endl; minus[root][idx] = {-1, 0}; bit2.emplace_back(); int bit2_idx = 0; for (const Edge &dst : graph[root]) { if (visited[dst.dst]) continue; // cout << dst.dst << endl; int max_depth = 0; function<void(int, int, int)> dfs = [&](int par, int ver, int dep) { // cout << par << ' ' << ver << ' ' << dep << endl; chmax(max_depth, dep); minus[ver][idx] = {bit2_idx, dep}; for (const Edge &e : graph[ver]) { if (!visited[e.dst] && e.dst != par) dfs(ver, e.dst, dep + 1); } }; dfs(root, dst.dst, 1); bit2[idx].emplace_back(max_depth + 1); ++bit2_idx; } for (int e : cd.comp[root]) { if (!visited[e]) rec(e); } }; rec(cd.root); // REP(i, n) { // cout << i << ": "; // for (auto [key, val] : mp[i]) cout << '(' << key << ',' << val << ") "; // cout << '\n'; // } // REP(i, n) { // cout << i << ": "; // for (int e : resp[i]) cout << e << ' '; // cout << '\n'; // } // REP(i, n) { // cout << i << ": "; // for (int e : depth[i]) cout << e << ' '; // cout << '\n'; // } // REP(i, n) { // cout << i << ": "; // for (auto [key, val] : minus[i]) cout << '(' << key << ',' << '(' << val.first << ',' << val.second << ")) "; // cout << '\n'; // } 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]]; auto [bit2_idx, dep] = minus[x][idx]; // cout << idx << ": " << bit2_idx << ' ' << dep << endl; if (bit2_idx != -1) ans -= bit2[idx][bit2_idx][dep]; } cout << ans << '\n'; for (int idx : resp[x]) { int ver = mp[idx][x], dist = y - depth[idx][ver]; // cout << idx << ' ' << ver << ' ' << dist << ": "; if (dist >= 0) { int l = 0, r = depth[idx].size(); while (r - l > 1) { int mid = (l + r) >> 1; (depth[idx][mid] <= dist ? l : r) = mid; } bit[idx].add(0, l + 1, z); auto [bit2_idx, ignore] = minus[x][idx]; // cout << l << ' ' << bit2_idx << ' ' << dep; if (bit2_idx != -1) bit2[idx][bit2_idx].add(0, min(dist, bit2[idx][bit2_idx].n - 2) + 1, z); } // cout << endl; } // REP(i, n) { // REP(j, bit[i].n - 1) cout << bit[i][j] << " \n"[j + 2 == bit[i].n]; // } // cout << "-----" << endl; } return 0; }