#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<int,int> pii;
typedef pair<int,pii> ipii;
const int MAXN = 1e6+10;
const int MAXA = 1e9;
const int INF = 1e9+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);
}
int leaf[MAXN];
struct segtree {
int st[4*MAXN], laz[4*MAXN];
void bnc(int id, int l, int r){
if(laz[id]==0) return;
st[lf] += laz[id]; laz[lf] += laz[id];
st[rg] += laz[id]; laz[rg] += laz[id];
laz[id] = 0;
}
void bd(int id,int l,int r){
if(l==r){ st[id] = leaf[l]; return; }
bd(lf,l,md); bd(rg,md+1,r);
st[id] = max(st[lf], st[rg]);
}
int que(int id,int l,int r,int x,int y){
if(x<=l && r<=y) return st[id];
if(r<x || y<l) return -INF;
bnc(id,l,r);
return max(que(lf,l,md,x,y), que(rg,md+1,r,x,y));
}
int upd(int id,int l,int r,int x,int y,int p){
if(x<=l && r<=y){
laz[id] += p;
return st[id] += p;
}
if(r<x || y<l) return st[id];
bnc(id,l,r);
return st[id] = max(upd(lf,l,md,x,y,p), upd(rg,md+1,r,x,y,p));
}
} A;
int n, q, W;
int u[MAXN], v[MAXN], w[MAXN], par[MAXN];
vector<pii> tree[MAXN], adj[MAXN];
int val[MAXN], tim, in[MAXN], out[MAXN];
void dfs(int nw, int pa){
in[nw] = ++tim;
par[nw] = pa;
for(auto [nx,wei] : tree[nw]){
if(nx==pa) continue;
adj[nw].pb({nx, wei});
val[nx] = val[nw]+wei;
dfs(nx,nw);
}
out[nw] = tim; leaf[in[nw]] = val[nw];
}
int opt[MAXN];
multiset<int>m;
void trav(int nw){
int mx = 0, mx2 = 0;
for(auto [nx, wei] : adj[nw]){
int te = A.que(1,1,n,in[nx],out[nx]);
if(te >= mx) mx2 = mx, mx = te;
else if(te >= mx2) mx2 = te;
}
opt[nw] = mx+mx2 - 2*A.que(1,1,n,in[nw],in[nw]);
m.insert(opt[nw]);
// cout << nw << ' ' << opt[nw] << " opt\n";
for(auto [nx, wei] : adj[nw])
trav(nx);
}
void UPD(int nw){
if(nw==0) return;
m.erase(m.find(opt[nw]));
int mx = 0, mx2 = 0;
for(auto [nx, wei] : adj[nw]){
int te = A.que(1,1,n,in[nx],out[nx]);
if(te >= mx) mx2 = mx, mx = te;
else if(te >= mx2) mx2 = te;
}
opt[nw] = mx+mx2 - 2*A.que(1,1,n,in[nw],in[nw]);
m.insert(opt[nw]);
UPD(par[nw]);
}
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;
}
dfs(1,0);
for(int i=1; i<=n-1; i++)
if(par[u[i]] == v[i]) swap(u[i], v[i]);
A.bd(1,1,tim);
trav(1);
int P = 0;
while(q--){
int x,y; cin>>x>>y;
x = (x+P)%(n-1)+1; y = (y+P)%W;
// cout << x << ' ' << y << " xy\n";
int baw = v[x];
A.upd(1,1,tim,in[baw],out[baw], y-w[x]);
UPD(u[x]);
// for(auto p : m) cout << p << " p\n";
w[x] = y;
cout << *(--m.end()) << '\n';
P = *(--m.end());
}
}
# | 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... |