This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
const int N = 3e5 + 5;
int n;
long long dis[N];
long long weight[N];
int cnt, in[N], out[N];
vector<pair<int, int>> g[N];
struct node {
long long mx = 0, mn = 0, mxmn = 0, mnmx = 0, ans = 0, lazy = 0;
}seg[N * 4];
node merge(node& lx, node& rx) {
node ret;
ret.mx = max(lx.mx, rx.mx);
ret.mn = min(lx.mn, rx.mn);
ret.mxmn = max({lx.mxmn, rx.mxmn, lx.mx - 2LL * rx.mn});
ret.mnmx = max({lx.mnmx, rx.mnmx, - 2LL * lx.mn + rx.mx});
ret.ans = max({lx.ans, rx.ans, lx.mx + rx.mnmx, lx.mxmn + rx.mx});
return ret;
}
void push(int nd, int l, int r) {
if (!seg[nd].lazy) {
return;
}
seg[nd].mx += seg[nd].lazy;
seg[nd].mn += seg[nd].lazy;
seg[nd].mxmn -= seg[nd].lazy;
seg[nd].mnmx -= seg[nd].lazy;
if (l != r) {
int mid = (l + r) / 2;
int rs = nd + ((mid - l + 1) * 2);
seg[nd + 1].lazy += seg[nd].lazy;
seg[rs].lazy += seg[nd].lazy;
}
seg[nd].lazy = 0;
}
void upd(int nd, int l, int r, int s, int e, long long v) {
push(nd, l, r);
if (l >= s && r <= e) {
seg[nd].lazy = v;
push(nd, l, r);
return;
}
int mid = (l + r) / 2;
int rs = nd + ((mid - l + 1) * 2);
if (mid >= e) {
upd(nd + 1, l, mid, s, e, v);
push(rs, mid + 1, r);
} else {
if (mid < s) {
push(nd + 1, l, mid);
upd(rs, mid + 1, r, s, e, v);
} else {
upd(nd + 1, l, mid, s, e, v);
upd(rs, mid + 1, r, s, e, v);
}
}
seg[nd] = merge(seg[nd + 1], seg[rs]);
}
void dfs(int v, int pe) {
cnt++;
for (auto [u, i] : g[v]) {
if (i == pe) {
continue;
}
dis[u] = dis[v] + weight[i];
in[i] = cnt;
dfs(u, i);
out[i] = cnt - 1;
upd(0, 0, 2 * n - 2, in[i], out[i], weight[i]);
cnt++;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int q;
long long lim;
cin >> n >> q >> lim;
for(int i = 0; i < n - 1; ++i) {
int u, v;
long long w;
cin >> u >> v >> w;
weight[i] = w;
g[u].emplace_back(v, i);
g[v].emplace_back(u, i);
}
dfs(1, n);
long long last = 0;
while (q--) {
long long d, c;
cin >> d >> c;
d = (d + last) % (n - 1);
c = (c + last) % lim;
upd(0, 0, 2 * n - 2, in[d], out[d], -weight[d]);
weight[d] = c;
upd(0, 0, 2 * n - 2, in[d], out[d], +weight[d]);
last = max(seg[0].mx, seg[0].ans);
cout << last << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |