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>
#define fr first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
#define USING_ORDERED_SET 0
#if USING_ORDERED_SET
#include<bits/extc++.h>
using namespace __gnu_pbds;
template<class T>using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#endif
template<class T>void umax(T &a,T b){if(a<b)a=b;}
template<class T>void umin(T &a,T b){if(b<a)a=b;}
#ifdef juggernaut
#define printl(args...) printf(args)
#else
#define printl(args...) 0
#endif
int n,q;
int timer;
ll w[100005];
pair<int,int>edge[100005];
vector<pair<int,int>>g[100005];
bool vis[100005];
int sz[100005];
int par[100005];
int depth[100005];
void build_sz(int v,int p){
sz[v]=1;
for(auto &to:g[v])if((to.fr^p)&&(!vis[to.fr])){
build_sz(to.fr,v);
sz[v]+=sz[to.fr];
}
}
int centroid(int v,int p,int sz){
bool can;
while(true){
can=true;
for(auto &to:g[v])if((to.fr^p)&&(!vis[to.fr])){
if(::sz[to.fr]>sz){
can=false;
p=v;
v=to.fr;
break;
}
}
if(can)return v;
}
}
void build(int v,int p,int dep){
build_sz(v,0);
v=centroid(v,0,sz[v]>>1);
par[v]=p;
depth[v]=dep;
vis[v]=true;
for(auto &to:g[v])if(!vis[to.fr])build(to.fr,v,dep+1);
}
ll ans;
struct MultiSegmentTree{
vector<ll>tree;
vector<ll>lazy;
MultiSegmentTree(int n){
tree.assign(n<<2|3,0ll);
lazy.assign(n<<2|3,0ll);
}
void push(int v,int l,int r){
if(l^r){
lazy[v<<1]+=lazy[v];
lazy[v<<1|1]+=lazy[v];
}
tree[v]+=lazy[v];
lazy[v]=0;
}
ll get(int ql,int qr,int v,int l,int r){
if(qr<l||r<ql)return 0ll;
push(v,l,r);
if(ql<=l&&r<=qr)return tree[v];
int mid=(l+r)>>1;
return max(get(ql,qr,v<<1,l,mid),get(ql,qr,v<<1|1,mid+1,r));
}
void update(int ql,int qr,ll val,int v,int l,int r){
push(v,l,r);
if(qr<l||r<ql)return;
if(ql<=l&&r<=qr){
lazy[v]+=val;
push(v,l,r);
return;
}
int mid=(l+r)>>1;
update(ql,qr,val,v<<1,l,mid);
update(ql,qr,val,v<<1|1,mid+1,r);
tree[v]=max(tree[v<<1],tree[v<<1|1]);
}
};
ll go(int v,int p,int &pivot,ll depth){
ll mx=depth;
if(!p){
ll m1=0,m2=0;
for(auto &to:g[v])if((to.fr^p)&&::depth[to.fr]>pivot){
ll cur=go(to.fr,v,pivot,depth+w[to.sc]);
umax(mx,cur);
if(cur>m1){
m2=m1;
m1=cur;
}else if(cur>m2)m2=cur;
}
mx=m1+m2;
}else for(auto &to:g[v])if((to.fr^p)&&::depth[to.fr]>pivot)umax(mx,go(to.fr,v,pivot,depth+w[to.sc]));
return mx;
}
int main(){
scanf("%d%d%lld",&n,&q,&w[0]);
for(int i=1;i^n;i++){
scanf("%d%d%lld",&edge[i].fr,&edge[i].sc,&w[i]);
g[edge[i].fr].emplace_back(edge[i].sc,i);
g[edge[i].sc].emplace_back(edge[i].fr,i);
}
build(1,0,0);
for(int i=1;i^n;i++)if(depth[edge[i].fr]>depth[edge[i].sc])swap(edge[i].fr,edge[i].sc);
MultiSegmentTree alls(n);
for(int i=1;i<=n;i++)alls.update(i,i,go(i,0,depth[i],0),1,1,n);
while(q--){
ll x;
ll y;
scanf("%lld%lld",&x,&y);
x=(x+ans)%(n-1)+1;
y=(y+ans)%w[0];
//Episode 1-> Think hard
w[x]=y;
x=edge[x].fr;
while(x){
alls.update(x,x,go(x,0,depth[x],0)-alls.get(x,x,1,1,n),1,1,n);
x=par[x];
}
ans=alls.get(1,n,1,1,n);
printf("%lld\n",ans);
}
}
Compilation message (stderr)
diameter.cpp: In function 'int main()':
diameter.cpp:113:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
113 | scanf("%d%d%lld",&n,&q,&w[0]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
diameter.cpp:115:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | scanf("%d%d%lld",&edge[i].fr,&edge[i].sc,&w[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diameter.cpp:126:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
126 | scanf("%lld%lld",&x,&y);
| ~~~~~^~~~~~~~~~~~~~~~~~
# | 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... |