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 FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }
typedef long long ll;
typedef long double ld;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
typedef pair<ll,ll>pi; typedef pair<ll,pi>spi; typedef pair<pi,pi>dpi;
#define LLINF ((long long)4e18+1000)
#define INF int(1e9+1e6)
#define MAXN (100006)
ll n,q,w;
int bck[MAXN],st[MAXN],en[MAXN],co,p[MAXN][17],depth[MAXN];
vector<pair<int,ll>> v[MAXN];
spi e[MAXN];
pair<ll,int> def=pi(-LLINF,-1);
ll X;
struct fen {
ll fw[MAXN];
void pupdate(int x,ll nval) {
for(;x<MAXN;x+=x&(-x)) fw[x]+=nval;
}
void update(int x,int y,ll nval){
pupdate(x,nval),pupdate(y+1,-nval);
}
ll sum(int x){
ll res=0;
for(;x;x-=x&(-x)) res+=fw[x];
return res;
}
} dist;
void dfs(int x,int par,ll d){
bck[co]=x, st[x]=co++;
dist.update(st[x],st[x],d);
p[x][0]=par;
for(auto i:v[x]) if(i.f^par) {
depth[i.f]=depth[x]+1, dfs(i.f,x,d+i.s);
}
en[x]=co-1;
}
bool isp(int a,int b) { return st[a]<=st[b]&&en[a]>=en[b]; }
int lca(int x,int y){
if(isp(x,y))return x;
if(isp(y,x))return y;
DEC(i,16,0)if(!isp(p[x][i],y))x=p[x][i];
return p[x][0];
}
ll disty(int a,int b) { return dist.sum(st[a]) + dist.sum(st[b]) - 2 * dist.sum(st[lca(a,b)]); }
int kpar(int x,int k){
FOR(i,0,16) if(1<<i&k) {
x=p[x][i];
}
return x;
}
int sz[MAXN], total, cp[MAXN];
bitset<MAXN> done;
vector<int> kids[MAXN];
struct node{
int s,e,m;
pair<ll,int> v;
ll add;
node*l,*r;
node(int S,int E){
s=S,e=E,m=(s+e)>>1;
add=0;
if(s==e) v=pi(disty(bck[kids[X][e]],X),bck[kids[X][e]]);
else l=new node(s,m),r=new node(m+1,e),v=max(l->v,r->v);
}
void update(int x,int y,ll nval){
if(s==x&&e==y) {
add+=nval;
return;
}
if(x>m)r->update(x,y,nval);
else if(y<=m)l->update(x,y,nval);
else l->update(x,m,nval),r->update(m+1,y,nval);
v=max(l->value(),r->value());
}
pair<ll,int> value() {
if(add==0) return v;
v.f+=add;
if(s^e){
l->add+=add;
r->add+=add;
}
add=0;
return v;
}
pair<ll,int> rmq(int x,int y){
value();
if(s==x&&e==y) return v;
if(x>m) return r->rmq(x,y);
else if(y<=m) return l->rmq(x,y);
else return max(l->rmq(x,m),r->rmq(m+1,y));
}
pair<ll,int> exc_rmq(int x,int y){
return max((s<=x-1?rmq(s,x-1):def),(y+1<=e?rmq(y+1,e):def));
}
void exc_upd(int x,int y,ll nval){
if(s<=x-1) update(s,x-1,nval);
if(y+1<=e) update(y+1,e,nval);
}
} *seg[MAXN];
void dfs1(int x,int p){
sz[x]=1;
++ total;
for(auto i:v[x]) if(!done[i.f]&&i.f!=p){
dfs1(i.f,x);
sz[x]+=sz[i.f];
}
}
int dfs2(int x,int p){
for(auto i:v[x]) if(!done[i.f]&&i.f!=p&&sz[i.f]>total/2) return dfs2(i.f,x);
return x;
}
void dfs3(int x,int p){
total=0;
dfs1(x,x);
x=dfs2(x,x);
cp[x]=p;
done[x]=1;
for(auto i:v[x]) if(!done[i.f]) dfs3(i.f,x);
v[x].clear();
}
void upd(int x,int o,ll diff){
if(x==-1) return;
if(isp(o,x)) { // in subtre of o, exc_upd
seg[x]->exc_upd(lbd(kids[x],st[o]),ubd(kids[x],en[o])-1,diff);
}else{
seg[x]->update(lbd(kids[x],st[o]),ubd(kids[x],en[o])-1,diff);
}
upd(cp[x],o,diff);
}
inline pair<ll,int> add(pair<ll,int> ans,ll x){
return {ans.f+x,ans.s};
}
pair<ll,int> query(ll x,ll o){
if(x==-1) return def;
if(!isp(x,o)){
return max(query(cp[x],o), add(seg[x]->rmq(lbd(kids[x],st[x]),ubd(kids[x],en[x])-1),disty(x,o)));
}else{
ll inter = kpar(o,depth[o]-depth[x]-1);
return max(query(cp[x],o), add(seg[x]->exc_rmq(lbd(kids[x],st[inter]),ubd(kids[x],en[inter])-1),disty(x,o)));
}
}
#define gc getchar_unlocked
void in(ll&x){
x=0;
char ch=gc();
while(ch<'0'||ch>'9') ch=gc();
while(ch>='0'&&ch<='9'){
x=(x<<3)+(x<<1)+ch-48;
ch=gc();
}
}
void in(int&x){
x=0;
char ch=gc();
while(ch<'0'||ch>'9') ch=gc();
while(ch>='0'&&ch<='9'){
x=(x<<3)+(x<<1)+ch-48;
ch=gc();
}
}
int main(){
FAST
in(n),in(q),in(w);
FOR(i,0,n-2){
in(e[i].s.f),in(e[i].s.s),in(e[i].f);
v[e[i].s.f].eb(e[i].s.s,e[i].f), v[e[i].s.s].eb(e[i].s.f,e[i].f);
}
co=1, dfs(1,1,0);
FOR(j,1,16)FOR(i,1,n)p[i][j]=p[p[i][j-1]][j-1];
dfs3(1,-1);
FOR(st,1,n){
ll i=bck[st];
while(~i){
kids[i].pb(st);
i=cp[i];
}
}
FOR(x,1,n){
X=x, seg[x]=new node(0,siz(kids[x])-1);
}
ll d1=1,d2=1,best=0,pans=0;
FOR(i,1,n) {
pi tryy=query(i,i);
if(tryy.f>best) best=tryy.f, d1=i, d2=tryy.s;
}
cerr<<"best: "<<best<<'\n';
FOR(i,1,q){
ll eg,ncost;
in(eg),in(ncost);
eg+=pans,eg%=n-1,ncost+=pans,ncost%=w;
ll a=e[eg].s.f, b=e[eg].s.s;
if(p[a][0]==b) swap(a,b);
upd(b,b,ncost-e[eg].f);
dist.update(st[b],en[b],ncost-e[eg].f);
e[eg].f=ncost;
best=disty(d1,d2);
ll od1=d1,od2=d2;
pi hmm=query(od1,od1);
if(hmm.f>best) best=hmm.f, d1=od1, d2=hmm.s;
hmm=query(od2,od2);
if(hmm.f>best) best=hmm.f, d1=hmm.s, d2=od2;
cout<<(pans=best)<<'\n';
}
}
/*
4 2 2000
1 2 100
2 3 1000
2 4 1000
2 1030
1 1020
*/
# | 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... |