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>
#define int long long
using namespace std;
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 = 2e9 , MD = 1e9 + 7;
int n ,q,W;
int dis[N];
int st[N],ft[N],ind[N],tim=1;
int par[N];
vector<pii> g[N];
struct node{
int sum=1,lazy=1,ind=-1;
node(int s,int l,int i){sum = s,lazy = l,ind=i;}
node(){}
};
node seg[N<<2];
node merge(node a,node b){
node ans ;
if(a.sum > b.sum) ans = node(a.sum,0,a.ind);
else ans = node(b.sum,0,b.ind);
return ans;
}
void shift(int u,int l,int r,int x){
seg[u].sum += x;
seg[u].lazy += x;
}
void relax(int u,int l,int r){
int mid = (l+r)>>1;
shift(lid,l,mid,seg[u].lazy);
shift(rid,mid,r,seg[u].lazy);
seg[u].lazy = 0;
}
void build(int u=1,int l=1,int r=n+1){
if(r-l==1){
seg[u].sum = seg[u].lazy = 0;
seg[u].sum = dis[ind[l]];
seg[u].ind = l;
return ;
}
int mid = (l+r)>>1;
build(lid,l,mid);
build(rid,mid,r);
seg[u] = merge(seg[lid],seg[rid]);
}
void update(int s,int e,int v,int u=1,int l=1,int r=n+1){
if(e <= l || e <= l || r <= s) return ;
if(s <= l and r <= e){
shift(u,l,r,v);
return ;
}
int mid = (l+r)>>1;
relax(u,l,r);
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=n+1){
if(e <= s) return node(-inf,0,0);
if(s <= l and r <= e) return seg[u];
int mid = (l+r)>>1;
relax(u,l,r);
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];
void dfs(int u,int p,int w){
st[u] = tim++;
par[u] = p;
dis[u] = (u==p ? 0 : dis[p] + w);
ind[st[u]] = u;
for(auto h : g[u]){
if(h.fi != p){
dfs(h.fi,u,h.sec);
}
}
ft[u] = tim;
}
int mx[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;
E[i] = Mp(Mp(u,v),w);
g[u].pb(Mp(v,w));
g[v].pb(Mp(u,w));
}
int lst = 0;
dfs(1,1,0);
build();
for(int i = 0;i<q;i++){
int e,s;
cin >> e >> s;
e = (e+lst)%(n-1);
s = (s+lst)%W;
pair<pii,int> pp = E[e];
auto [u,v] = pp.fi;
if(par[v] == u) swap(u,v);
update(st[u],ft[u],s-E[e].sec);
E[e].sec = s;
node p = seg[1];
int who = ind[p.ind];
int now = who;
int ans = 0;
int ls = -1;
while(1){
int tmp = get(st[who],st[who]+1).sum - 2*get(st[now],st[now]+1).sum;
if(ls == -1){
tmp += get(st[now],ft[now]).sum;
}
else{
int oo = max(get(st[now],st[ls]).sum,get(ft[ls],ft[now]).sum);
tmp += oo;
}
ans = max(ans,tmp);
if(now == 1) break;
ls = now;
now = par[now];
}
cout << ans << endl;
lst = ans;
}
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... |