// #pragma GCC optimize("O3")
// #pragma GCC optimization("Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define FOR(i, l, r) for (int i = (l); i <= (r); i++)
#define FOD(i, r, l) for (int i = (r); i >= (l); i--)
#define fi first
#define se second
#define pii pair<int, int>
const ll mod = 1e9 + 7;
const int MAXN = 1e6 + 5;
const ll oo = 1e18 + 7;
const int base = 10;
int n, q, W;
struct canh{
int u, v, w;
}cc[MAXN];
vector<pii> adj[MAXN];
int m;
vector<int> euler;
int L[MAXN], R[MAXN];
int d[MAXN], h[MAXN];
void dfs(int u, int p){
euler.push_back(u);
for(auto [v, w]:adj[u]){
if(v==p) continue;
d[v]=d[u]+w;
h[v]=h[u]+1;
dfs(v, u);
euler.push_back(u);
}
euler.push_back(u);
}
struct node{
int mi, mx, res1, res2, pos;
}st[8*MAXN];
int lz[8*MAXN];
node merge(node a, node b){
node c;
if(a.mx>b.mx){
c=a;
}
else if(a.mx<b.mx){
c=b;
}
else{
if(a.pos<b.pos){
c=a;
}
else{
c=b;
}
}
c.mi=min(a.mi, b.mi);
c.res1=max({a.res1, b.res1, a.mx-2*b.mi});
c.res2=max({a.res2, b.res2, b.mx-2*a.mi});
return c;
}
void build(int id, int l, int r){
if(l>=r){
int u=euler[l];
st[id]={d[u], d[u], -oo, -oo, l};
return;
}
int mid=(l+r)/2;
build(id*2, l, mid);
build(id*2+1, mid+1, r);
st[id]=merge(st[id*2], st[id*2+1]);
}
void push(int id){
if(lz[id]!=0){
int val=lz[id];
for(auto nid:{id*2, id*2+1}){
st[nid].mi+=val;
st[nid].mx+=val;
st[nid].res1-=val;
st[nid].res2-=val;
lz[nid]+=val;
}
lz[id]=0;
}
}
void update(int id, int l, int r, int i, int j, int val){
if(l>j || r<i){
return;
}
if(l>=i && r<=j){
st[id].mi+=val;
st[id].mx+=val;
st[id].res1-=val;
st[id].res2-=val;
lz[id]+=val;
return;
}
int mid=(l+r)/2;
push(id);
update(id*2, l, mid, i, j, val);
update(id*2+1, mid+1, r, i, j, val);
st[id]=merge(st[id*2], st[id*2+1]);
}
node get(int id, int l, int r, int i, int j){
if(l>j || r<i){
return {oo, -oo, -oo, -oo, 0};
}
if(l>=i && r<=j){
return st[id];
}
int mid=(l+r)/2;
push(id);
return merge(get(id*2, l, mid, i, j), get(id*2+1, mid+1, r, i, j));
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("test.txt", "r", stdin);
// freopen("o2.out", "w", stdout);
if(fopen(".inp", "r")){
freopen(".inp", "r", stdin);
freopen(".out", "w", stdout);
}
cin >> n >> q >> W;
FOR(i, 1, n-1){
int u, v, w;
cin >> u >> v >> w;
cc[i]={u, v, w};
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
euler.push_back(0);
dfs(1, 0);
m=(int)euler.size()-1;
FOR(i, 1, m){
int u=euler[i];
// cout << u << ' ';
if(L[u]==0){
L[u]=i;
}
R[u]=i;
}
build(1, 1, m);
int last=0;
while(q--){
int d, e;
cin >> d >> e;
d=(d+last)%(n-1);
e=(e+last)%W;
d++;
auto [u, v, w]=cc[d];
cc[d].w=e;
if(h[u]>h[v]) swap(u, v);
update(1, 1, m, L[v], R[v], e-w);
int pos=st[1].pos;
int res1=get(1, 1, m, 1, pos).res1+st[1].mx;
int res2=get(1, 1, m, pos, m).res2+st[1].mx;
last=max(res1, res2);
cout << last << '\n';
}
return 0;
}
Compilation message (stderr)
diameter.cpp: In function 'int main()':
diameter.cpp:132:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
132 | freopen(".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
diameter.cpp:133:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | freopen(".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~| # | 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... |