This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<long long,int>pii;
//typedef pair<pii,pii>pi2;
typedef array<int,6>pi2;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n,q,w;
vector<pii>adj[100005];
pii edge[100005];
int in[100005];
int out[100005];
int d[100005];
int ptr=0;
vector<int>v;
void dfs(int index,int par){
in[index]=v.size();
v.push_back(index);
for(auto it:adj[index]){
if(it.first==par) continue;
d[it.first]=d[index]+it.second;
dfs(it.first,index);
v.push_back(index);
}
out[index]=v.size()-1;
}
inline pi2 combine(pi2 a, pi2 b){
pi2 temp;
temp={0,0,0,0,0};
temp[0]=max(a[0],b[0]);
temp[1]=max(a[1],b[1]);
temp[2]=max(a[2],b[2]);
temp[3]=max({a[3],b[3],a[0]+b[1]});
temp[4]=max({a[4],b[4],a[1]+b[2]});
temp[5]=max({a[5],b[5],a[3]+b[2],a[0]+b[4]});
return temp;
}
struct node{
int s,e,m;
node *l,*r;
pi2 v;
int lazyUpd;
node(int ss, int ee):s(ss),e(ee),m((s+e)>>1),lazyUpd(0){
v={0,0,0,0,0,0};
if(s!=e){
l=new node(s,m);
r=new node(m+1,e);
}
}
void self_add(int x){
v[0]+=x;
v[1]-=2*x;
v[2]+=x;
v[3]-=x;
v[4]-=x;
lazyUpd+=x;
}
void forceProp(){
if(s==e) return;
if(lazyUpd){
l->self_add(lazyUpd);
r->self_add(lazyUpd);
lazyUpd=0;
}
}
void rangeUpd(int x, int y, int c){
if(x<=s&&y>=e){
self_add(c);
return;
}
forceProp();
if(x<=m) l->rangeUpd(x,y,c);
if(y>m) r->rangeUpd(x,y,c);
v=combine(l->v,r->v);
}
pi2 query(int x, int y){
if(x<=s&&y>=e){
return v;
}
forceProp();
if(y<=m) return l->query(x,y);
if(x>m) return r->query(x,y);
return combine(l->query(x,m),r->query(m+1,y));
}
};
int cost[200005];
void solve(){
cin >> n >> q >> w;
int temp,temp2,temp3;
for(int x=0;x<n-1;x++){
cin >> temp >> temp2 >> temp3;
adj[temp].push_back({temp2,temp3});
adj[temp2].push_back({temp,temp3});
edge[x]={temp,temp2};
cost[x]=temp3;
}
dfs(1,-1);
for(int x=0;x<n-1;x++){
if(d[edge[x].first]<d[edge[x].second]) swap(edge[x].first,edge[x].second);
}
node st(0,v.size()-1);
for(int x=0;x<n-1;x++){
st.rangeUpd(in[edge[x].first],out[edge[x].first],cost[x]);
}
int last=0;
for(int x=0;x<q;x++){
cin >> temp >> temp2;
temp=(temp+last)%(n-1);
temp2=(temp2+last)%w;
int a=edge[temp].first;
int diff=temp2-cost[temp];
cost[temp]=temp2;
st.rangeUpd(in[a],out[a],diff);
last=st.v[5];
cout << last << "\n";
}
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
//cin >> t;
while(t--){
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... |