#include <bits/stdc++.h>
// #pragma GCC optimize("O3")
#define int long long
#define ll long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<ll,ll> pii;
typedef pair<int,pii> ipii;
const int MAXN = 2e5+10;
const int MAXA = 1e9;
const ll INF = 1e18+100;
const int SQRT = 500;
const int LOG = 61;
const int MOD = 1e9+7;
void chmn(auto &a, auto b){ a = min(a, b); }
void chmx(auto &a, auto b){ a = max(a, b); }
int sum(int a, int b){ a %= MOD; b %= MOD; return (a+b)%MOD; }
void chsum(int &a, int b){ a %= MOD; b %= MOD; a = (a+b)%MOD; }
void chsub(int &a, int b){ a %= MOD; b %= MOD; a = (a+MOD-b)%MOD; }
int mul(int a, int b){ a %= MOD; b %= MOD; return a*b%MOD;}
void chmul(int &a, int b){ a = a*b%MOD; }
int expo(int a, int b){
if(b==0) return 1;
int te = expo(a, b/2); te = mul(te, te);
return (b%2 ? mul(te, a) : te);
}
struct node {
ll a,b,c,ab,bc,abc;
};
ll arr[MAXN], in[MAXN], out[MAXN], tim;
struct segtree {
node st[4*MAXN]; ll laz[4*MAXN];
void push(int id, ll lz){
st[id].a += lz; st[id].b -= 2*lz; st[id].c += lz;
st[id].ab -= lz; st[id].bc -= lz;
laz[id] += lz;
}
void bnc(int id, int l, int r){
if(laz[id]==0) return;
push(lf, laz[id]); push(rg, laz[id]);
laz[id] = 0;
}
node mrg(node X, node Y){
if(X.a==-INF) return Y;
if(Y.a==-INF) return X;
node ret;
ret.a = max(X.a, Y.a); ret.c = max(X.c, Y.c);
ret.b = max(X.b, Y.b);
ret.ab = max({X.ab, Y.ab, X.a+Y.b});
ret.bc = max({X.bc, Y.bc, X.b+Y.c});
ret.abc = max({X.abc,Y.abc, X.ab+Y.c, X.a+Y.bc});
return ret;
}
node upd(int id,int l,int r,int x,int y,ll p){
if(x<=l && r<=y){
push(id, p);
return st[id];
}
if(r<x || y<l) return st[id];
bnc(id,l,r);
return st[id] = mrg(upd(lf,l,md,x,y,p), upd(rg,md+1,r,x,y,p));
}
} A;
int n, q, W;
ll u[MAXN], v[MAXN], w[MAXN], par[MAXN];
vector<pii> tree[MAXN];
void trav(int nw, int pa, int w=0){
in[nw] = out[nw] = ++tim; par[nw] = pa;
for(auto [nx, wei] : tree[nw]){
if(nx==pa) continue;
trav(nx,nw,wei);
out[nw] = ++tim;
}
A.upd(1,1,2*n, in[nw],out[nw],w);
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>q>>W;
for(int i=1; i<=n-1; i++){
int x,y,we; cin>>x>>y>>we;
tree[x].pb({y,we}); tree[y].pb({x,we});
u[i] = x; v[i] = y; w[i] = we;
}
trav(1,0);
for(int i=1; i<=n-1; i++)
if(par[u[i]] == v[i]) swap(u[i], v[i]);
// for(int i=1; i<=n; i++){
// cout << in[i] << ' ' << out[i] << " in\n";
// }
ll P = 0;
while(q--){
int x,y; cin>>x>>y;
x = (x+P)%(n-1)+1; y = (y+P)%W;
ll baw = v[x];
A.upd(1,1,2*n, in[baw], out[baw], y-w[x]);
w[x] = y;
cout << (P = A.st[1].abc) << '\n';
}
}
# | 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... |