This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <stack>
#include <queue>
#include <assert.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
void debug() {cout << endl;}
template<class T, class ... U> void debug(T a, U ... b) {cout << a << " ", debug(b...);};
template<class T> void pary(T l, T r) {
	while (l != r) cout << *l << " ", l++;
	cout << endl;
}
#define ll long long
#define maxn 200005
#define mod 1000000007
#define pii pair<ll, ll>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
vector<pii> adj[maxn];
pii ed[maxn];
int ord[maxn], lef[maxn], rig[maxn], pa[maxn], sub[maxn];
ll dep[maxn], wei[maxn];
int ti = 0;
void dfs(int n, int par) {
	pa[n] = par;
	ord[ti] = n;
	lef[n] = ti++;
	for (auto v:adj[n]) {
		if (v.ff != par) {
			dep[v.ff] = dep[n] + v.ss;
			dfs(v.ff, n);
			ord[ti++] = n;
		}
	}
	rig[n] = ti;
}
const ll ninf = 1LL<<61;
struct segtree{
	struct node{
		ll mi, ma, la, ra, val;
		node() {mi = 0, ma = 0, la = 0, ra = 0, val = 0;}
	};
	node seg[4 * maxn];
	ll tag[4 * maxn];
	void pull(int cur) {
		ll tl = tag[cur * 2], tr = tag[cur * 2 + 1];
		node le = seg[cur*2], ri = seg[cur*2+1];
		seg[cur].mi = min(le.mi + tl, ri.mi + tr);
		seg[cur].ma = max(le.ma + tl, ri.ma + tr);
		seg[cur].la = max(max(le.la - tl, ri.la - tr), le.ma + tl - 2*(ri.mi + tr));
		seg[cur].ra = max(max(le.ra - tl, ri.ra - tr), ri.ma + tr - 2*(le.mi + tl));
		seg[cur].val = max(max(le.val, ri.val), max(le.la - tl + ri.ma + tr, le.ma + tl + ri.ra - tr));
	}
	void init(int cur, int l, int r) {
		if (r <= l) return;
		if (r - l == 1) {
			seg[cur].mi = seg[cur].ma = dep[ord[l]];
			seg[cur].la = seg[cur].ra = -dep[ord[l]];
			return;
		}	
		int mid = (l + r) / 2;
		init(cur * 2, l, mid), init(cur * 2 + 1, mid, r);
		pull(cur);	
		//debug(l, r, seg[cur].val);
	}
	void modify(int cur, int l, int r, int ql, int qr, ll val) {
		if (r <= l || ql >= r || qr <= l) return;
		if (ql <= l && qr >= r) {
			tag[cur] += val;
			return;
		}
		int mid = (l + r) / 2;
		modify(cur * 2, l, mid, ql, qr, val);
		modify(cur * 2 + 1, mid, r, ql, qr, val);
		pull(cur);
		//debug(l, r, seg[cur].val);
	}
	ll getval() {
		return seg[1].val + tag[1];
	}
} tree;
int main() {
	io
	ll n, q, W;
	cin >> n >> q >> W;
	for (int i = 0;i < n - 1;i++) {
		int u, v;
		ll w;
		cin >> u >> v >> w;
		ed[i] = {u, v};
		wei[i] = w;
		adj[u].push_back({v, w});
		adj[v].push_back({u, w});
	}
	dfs(1, 0);
	for (int i = 0;i < n - 1;i++) {
		if (pa[ed[i].ff] == ed[i].ss) sub[i] = ed[i].ff;
		else sub[i] = ed[i].ss;
	}
	//pary(ord, ord + ti);
	//pary(dep + 1, dep + 1 + n);
	tree.init(1, 0, ti);
	ll prv = 0;
	while (q--) {
		ll d, e;
		cin >> d >> e;
		d = (d + prv) % (n - 1);
		e = (e + prv) % W;
		ll dif = e - wei[d];
		tree.modify(1, 0, ti, lef[sub[d]], rig[sub[d]], dif);
		wei[d] = e;
		prv = tree.getval();
		cout << prv << "\n";
	}
}
/*
4 3 2000
1 2 100
2 3 1000
2 4 1000
2 1030
1 1020
1 890
*/
| # | 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... |