This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
in the name of god
*/
//#pragma GCC optimize("Ofast,O3,unroll-loops")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,sse4a,avx,avx2,popcnt,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> pii;
typedef pair<long long ,long long> pll;
typedef long long ll ;
#define File freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define md(x) (((x%MD)+MD)%MD)
#define all(V) V.begin(),V.end()
#define setprec(x) fixed << setprecision(x)
#define Mp(a,b) make_pair(a,b)
#define len(V) (int)(V.size())
#define sep ' '
#define endl '\n'
#define pb(x) push_back(x)
#define fi first
#define sec second
#define popcount(x) __builtin_popcount(x)
#define lid u<<1
#define rid (lid)|1
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll N = 1e5 + 10,SQ=320,LOG=31;
const ll inf = 2e18 , MD = 1e9 + 7;
int n ,q ,W;
int dis[N];
int st[N<<1],tim=1,L[N],R[N];
vector<pii> g[N];
int par[N];
void tour(int u,int p){
st[tim] = u;
L[u] = min(L[u],tim);
R[u] = max(R[u],tim);
tim++;
for(auto h : g[u]){
if(h.fi!=p){
tour(h.fi,u);
st[tim] = u;
L[u] = min(L[u],tim);
R[u] = max(R[u],tim);
tim++;
}
}
}
void dfs(int u,int p,int w){
par[u] = p;
dis[u] = dis[p] + w;
for(auto h : g[u]){
if(h.fi != p){
dfs(h.fi,u,h.sec);
}
}
}
/*
a,b,c
0 -> a
1 -> ab
2 -> bc
3 -> abc
*/
struct node{
int mn,lazy ;
int ans[4];
};
node seg[N<<3];
node merge(node a,node b){
node ans;
ans.lazy = 0;
ans.mn = min(a.mn,b.mn);
ans.ans[0] = max(a.ans[0],b.ans[0]);
ans.ans[1] = max(a.ans[1],b.ans[1]);
ans.ans[1] = max(ans.ans[1],a.ans[0] - 2*b.mn);
ans.ans[2] = max({a.ans[2],b.ans[2],b.ans[0] - 2*a.mn});
ans.ans[3] = max({a.ans[3],b.ans[3],a.ans[0] + b.ans[2],a.ans[1] + b.ans[0]});
return ans;
}
void build(int u=1,int l=1,int r=tim){
if(r-l==1){
seg[u].mn = dis[st[l]];
seg[u].ans[0] = dis[st[l]];
seg[u].ans[1] = seg[u].ans[2] = -dis[st[l]];
seg[u].ans[3] = 0;
return ;
}
int mid = (l+r)>>1;
build(lid,l,mid);
build(rid,mid,r);
seg[u] = merge(seg[lid],seg[rid]);
}
void shift(int u,int v){
seg[u].mn += v;
seg[u].ans[0] += v;
seg[u].ans[1] -= v;
seg[u].ans[2] -= v;
seg[u].lazy += v;
}
void relax(int u){
shift(lid,seg[u].lazy);
shift(rid,seg[u].lazy);
seg[u].lazy = 0;
}
void update(int s,int e,int v,int u=1,int l=1,int r=tim){
if(e <= s || e <= l || r <= s) return ;
if(s <= l and r <= e){
shift(u,v);
return ;
}
relax(u);
int mid = (l+r)>>1;
update(s,e,v,lid,l,mid);
update(s,e,v,rid,mid,r);
seg[u] = merge(seg[lid],seg[rid]);
}
node get(int s,int e,int u=1,int l=1,int r=tim){
if(s <= l and r <= e) return seg[u];
int mid = (l+r)>>1;
relax(u);
if(e <= mid) return get(s,e,lid,l,mid);
if(s >= mid) return get(s,e,rid,mid,r);
return merge(get(s,e,lid,l,mid),get(s,e,rid,mid,r));
}
pair<pii,int> E[N];
int32_t main() {
ios_base::sync_with_stdio(false);cout.tie(0);cin.tie(0);
cin >> n >> q >> W;
for(int i = 0;i<n-1;i++){
int u,v,w;
cin >> u >> v >> w;
g[u].pb(Mp(v,w));
g[v].pb(Mp(u,w));
E[i] = Mp(Mp(u,v),w);
}
fill(L,L+n+1,inf);
dfs(1,1,0);
tour(1,1);
build();
int lst = 0;
for(int i = 0;i<q;i++){
int d,w;
cin >> d >> w;
d = (d+lst)%(n-1);
w = (w+lst)%W;
auto [u,v] = E[d].fi;
if(par[u] != v) swap(u,v);
update(L[u],R[u]+1,-E[d].sec + w);
E[d].sec = w;
lst = seg[1].ans[3];
cout << lst << endl;
}
return 0;
}
# | 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... |