(UPD: 2024-12-04 14:48 UTC) Judge is not working due to Cloudflare incident. (URL) We can do nothing about it, sorry. After the incident is resolved, we will grade all submissions.

Submission #834639

#TimeUsernameProblemLanguageResultExecution timeMemory
834639EliasDynamic Diameter (CEOI19_diameter)C++17
100 / 100
4425 ms345964 KiB
#ifndef _DEBUG #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx") // codeforces #endif #include <bits/stdc++.h> using namespace std; #define int int64_t template <class T> istream &operator>>(istream &in, vector<T> &a) { for (T &x : a) in >> x; return in; } template <class T> ostream &operator<<(ostream &out, const vector<T> &a) { for (T x : a) out << x << " "; out << "\n"; return out; } struct SegTree { pair<int, int> get(int l, int r) { return get(0, n, 0, l, r); } void inc(int l, int r, int x) { inc(0, n, 0, l, r, x); } SegTree(const vector<int> &a) { n = a.size(); updates = vector<int>(4 * n); b = vector<pair<int, int>>(4 * n); build(0, n, 0, a); } SegTree() {} private: int n = 0; vector<pair<int, int>> b = {}; vector<int> updates = {}; void push(int l, int r, int i) { if (l + 1 == r) return; updates[i * 2 + 1] += updates[i]; updates[i * 2 + 2] += updates[i]; b[i * 2 + 1].first += updates[i]; b[i * 2 + 2].first += updates[i]; updates[i] = 0; } pair<int, int> build(int l, int r, int i, const vector<int> &a) { if (l + 1 == r) return b[i] = pair<int, int>{a[l], l}; int m = (l + r) / 2; return b[i] = max(build(l, m, i * 2 + 1, a), build(m, r, i * 2 + 2, a)); } pair<int, int> get(int l, int r, int i, int ql, int qr) { if (l >= qr || r <= ql) return {LLONG_MIN / 2, -1}; if (l >= ql && r <= qr) return b[i]; push(l, r, i); int m = (l + r) / 2; return max(get(l, m, i * 2 + 1, ql, qr), get(m, r, i * 2 + 2, ql, qr)); } pair<int, int> inc(int l, int r, int i, int ul, int ur, int x) { if (l >= ur || r <= ul) return b[i]; if (l >= ul && r <= ur) { updates[i] += x; b[i].first += x; return b[i]; } push(l, r, i); int m = (l + r) / 2; return b[i] = max(inc(l, m, i * 2 + 1, ul, ur, x), inc(m, r, i * 2 + 2, ul, ur, x)); } }; vector<vector<pair<int, int>>> adj; vector<bool> blocked; vector<vector<int>> pre; struct Tree { int n = 0; int level = 0; vector<int> subtree; vector<int> initial_parent; vector<int> pre_order; vector<int> pre_dist; int timer = 0; int centroid = -1; unordered_set<int> nodes; SegTree pre_dist_seg; void spread(int i, int p = -1) { n++; nodes.insert(i); for (auto [c, D] : adj[i]) { if (c != p && !blocked[c]) { spread(c, i); } } } int find_centroid(int i, int p = -1) { int subtree_size = 1; int largest_subtree = 0; for (auto [c, D] : adj[i]) { if (c != p && !blocked[c]) { int sub = find_centroid(c, i); subtree_size += sub; largest_subtree = max(largest_subtree, sub); } } if (largest_subtree <= n / 2 && n - subtree_size <= n / 2) centroid = i; return subtree_size; } int dfs(int i, int d = 0, int p = -1) { pre[level][i] = timer++; pre_order.push_back(i); pre_dist[pre[level][i]] = d; int subtree_size = 1; if (p == -1) initial_parent[pre[level][i]] = -1; else if (initial_parent[pre[level][p]] == -1) initial_parent[pre[level][i]] = i; else initial_parent[pre[level][i]] = initial_parent[pre[level][p]]; for (auto [c, D] : adj[i]) { if (c != p && !blocked[c]) { subtree_size += dfs(c, d + D, i); } } subtree[pre[level][i]] = subtree_size; return subtree_size; } vector<int> split() { blocked[centroid] = true; vector<int> out; for (auto [c, d] : adj[centroid]) { if (!blocked[c]) out.push_back(c); } return out; } Tree(int start, int level) : level(level) { spread(start); pre_dist = initial_parent = subtree = vector<int>(n); find_centroid(start); assert(centroid != -1); dfs(centroid); pre_dist_seg = SegTree(pre_dist); } int update(int a, int b, int c) { if (nodes.count(a) != 0 && nodes.count(b) != 0) { if (pre[level][a] > pre[level][b]) swap(a, b); int dist_lower = pre_dist_seg.get(pre[level][b], pre[level][b] + 1).first; int dist_upper = pre_dist_seg.get(pre[level][a], pre[level][a] + 1).first; int old_weight = dist_lower - dist_upper; int delta = c - old_weight; pre_dist_seg.inc(pre[level][b], pre[level][b] + subtree[pre[level][b]], delta); } auto [dist, i] = pre_dist_seg.get(0, n); int bad_child = initial_parent[i]; auto dist2 = pre_dist_seg.get(0, pre[level][bad_child]).first; auto dist3 = pre_dist_seg.get(pre[level][bad_child] + subtree[pre[level][bad_child]], n).first; return dist + max({dist2, dist3, 0l}); } }; signed main() { cin.tie(0); ios_base::sync_with_stdio(false); int n, q, w; cin >> n >> q >> w; adj.resize(n); blocked.resize(n); pre = vector<vector<int>>(30, vector<int>(n)); vector<pair<int, int>> all_edges; for (int i = 0; i < n - 1; i++) { int a, b, c; cin >> a >> b >> c; a--, b--; adj[a].push_back({b, c}); adj[b].push_back({a, c}); all_edges.push_back({a, b}); } vector<Tree> subtrees; subtrees.push_back(Tree(0, 0)); queue<int> todo; todo.push(0); vector<vector<int>> trees_using(n, {0}); while (todo.size()) { int index = todo.front(); todo.pop(); auto out = subtrees[index].split(); for (auto x : out) { Tree new_tree(x, subtrees[index].level + 1); if (new_tree.n > 1) { int new_index = subtrees.size(); todo.push(new_index); for (auto i : new_tree.pre_order) { if (trees_using[i].back() != new_index) trees_using[i].push_back(new_index); } subtrees.push_back(move(new_tree)); } } } vector<int> subtree_result(subtrees.size()); set<pair<int, int>> results; for (int i = 0; i < subtrees.size(); i++) { subtree_result[i] = subtrees[i].update(-1, -1, -1); results.insert({subtree_result[i], i}); } int last = 0; while (q--) { int d, e; cin >> d >> e; d = (d + last) % int(n - 1); e = (e + last) % w; auto [a, b] = all_edges[d]; int out = 0; for (int i : trees_using[a]) { results.erase({subtree_result[i], i}); subtree_result[i] = subtrees[i].update(a, b, e); results.insert({subtree_result[i], i}); } out = prev(results.end())->first; cout << out << "\n"; last = out; } }

Compilation message (stderr)

diameter.cpp: In function 'int main()':
diameter.cpp:300:20: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<Tree>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  300 |  for (int i = 0; i < subtrees.size(); i++)
      |                  ~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...