This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout (T t, U...u) {cout << t << (sizeof... (u) ? ", " : ""), dout (u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
#define lpos pos*2
#define rpos pos*2+1
const int SIZE = 2e5 + 5;
int n, q;
ll ans, w, dep[SIZE];
vector<pair<int, ll>> adj[SIZE];
tuple<int, int, ll> e[SIZE];
int dfsid, in[SIZE], out[SIZE], id[SIZE];
void dfs(int pos, int fa) {
    in[pos] = out[pos] = ++dfsid;
    id[dfsid] = pos;
    for (auto [np, w] : adj[pos]) if (np != fa) {
        dep[np] = dep[pos] + w;
        dfs(np, pos);
        out[pos] = ++dfsid;
        id[dfsid] = pos;
    }
}
struct Node {
    ll lazy = 0, mn, mx, ab, bc, abc;
    Node operator + (const Node& rhs) const {
        Node re;
        re.mn = min(mn, rhs.mn);
        re.mx = max(mx, rhs.mx);
        re.ab = max({ab, rhs.ab, mx - 2 * rhs.mn});
        re.bc = max({bc, rhs.bc, rhs.mx - 2 * mn});
        re.abc = max({abc, rhs.abc, ab + rhs.mx, mx + rhs.bc});
        return re;
    }
} node[4 * SIZE];
void build(int pos, int l, int r) {
    if (l == r) {
        node[pos].mn = node[pos].mx = dep[id[l]];
        node[pos].ab = node[pos].bc = -dep[id[l]];
        node[pos].abc = 0;
        return;
    }
    int mid = (l + r) / 2;
    build(lpos, l, mid);
    build(rpos, mid + 1, r);
    node[pos] = node[lpos] + node[rpos];
}
void push(int pos, int l, int r) {
    if (!node[pos].lazy) return;
    node[pos].mx += node[pos].lazy;
    node[pos].mn += node[pos].lazy;
    node[pos].ab -= node[pos].lazy;
    node[pos].bc -= node[pos].lazy;
    if (l < r) {
        node[lpos].lazy += node[pos].lazy;
        node[rpos].lazy += node[pos].lazy;
    }
    node[pos].lazy = 0;
}
void pull(int pos, int l, int r) {
    int mid = (l + r) / 2;
    push(lpos, l, mid);
    push(rpos, mid + 1, r);
    node[pos] = node[lpos] + node[rpos];
}
void upd(int pos, int l, int r, int L, int R, ll x) {
    if (l == L && r == R) {
        node[pos].lazy += x;
        push(pos, L, R);
        return;
    }
    push(pos, L, R);
    int mid = (L + R) / 2;
    if (r <= mid) upd(lpos, l, r, L, mid, x);
    else if (l > mid) upd(rpos, l, r, mid + 1, R, x);
    else {
        upd(lpos, l, mid, L, mid, x);
        upd(rpos, mid + 1, r, mid + 1, R, x);
    }
    pull(pos, L, R);
}
void solve() {
    cin >> n >> q >> w;
    FOR (i, 1, n - 1) {
        auto &[a, b, c] = e[i];
        cin >> a >> b >> c;
        adj[a].pb(b, c);
        adj[b].pb(a, c);
    }
    dfs(1, 1);
    FOR (i, 1, n - 1) {
        auto &[a, b, c] = e[i];
        if (in[a] < in[b]) swap(a, b);
    }
    build(1, 1, dfsid);
    while (q--) {
        int p;
        ll k;
        cin >> p >> k;
        p = (p + ans) % (n - 1) + 1;
        k = (k + ans) % w;
        auto &[a, b, c] = e[p];
        upd(1, in[a], out[a], 1, dfsid, k - c);
        c = k;
        cout << (ans = node[1].abc) << '\n';
    }
}
int main() {
    Waimai;
    solve();
}
| # | 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... |