# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
579258 | juggernaut | Dynamic Diameter (CEOI19_diameter) | C++14 | 0 ms | 0 KiB |
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[20];
int tin[100005][20];
int tout[100005][20];
ll w[100005];
multiset<ll>st[100005];
pair<int,int>edge[100005];
vector<pair<int,int>>g[100005];
bool vis[100005];
int sz[100005];
int par[100005];
int depth[100005];
int prepar[100005][20];
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_tin(int v,int p,int &depth,int &kylych){
tin[v][depth]=++timer[depth];
prepar[v][depth]=kylych;
for(auto &to:g[v])if((to.fr^p)&&(!vis[to.fr]))build_tin(to.fr,v,depth,kylych);
tout[v][depth]=timer[depth];
}
void go(int v,int p,int &depth,ll kylych){
tree[depth].update(v,v,kylych,1,1,n);
for(auto &to:g[v])if((to.fr^p)&&(!vis[to.fr]))go(to.fr,v,depth,kylych+to.sc);
}
void build(int v,int p,int dep){
build_sz(v,0);
v=centroid(v,0,sz[v]>>1);
tin[v][dep]=++timer[dep];
for(auto &to:g[v])if((to.fr^p)&&(!vis[to.fr])){
build_tin(to.fr,v,dep,to.fr);
}
go(v,v,dep,0);
tout[v][dep]=timer[dep];
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(){
}
MultiSegmentTree(int n){
tree.assign(n<<2|3,0ll);
lazy.assign(n<<2|3,0ll);
}
void push(int v,bool bit){
if(bit){
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]);
}
};
MultiSegmentTree tree[20],alls;
int cand_x,cand_y;
void edit(int v,ll val){
int x=cand_x;
int y=cand_y;
if(tin[x][depth[v]]<tin[y][depth[v]])swap(x,y);
int child=prepar[x][depth[v]];
st[v].erase(st[v].find(tree[depth[v]].get(tin[child][depth[v]],tout[child][depth[v]],1,1,n)));
tree[depth[v]].update(tin[x][depth[v]],tout[x][depth[v]],val,1,1,n);
st[v].insert(tree[depth[v]].get(tin[child][depth[v]],tout[child][depth[v]],1,1,n));
ll ans=0;
auto it=st[v].rbegin();
ans+=*it;
if((int)st[v].size()!=1){
it++;
ans+=*it;
}
alls.update(v,v,ans-alls.get(v,v,1,1,n),1,1,n);
}
int main(){
scanf("%d%d%lld",&n,&q,&w[0]);
for(int i=0;i<20;i++)tree[i]=MultiSegmentTree(n);
alls=MultiSegmentTree(n);
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);
return 1;
while(q--){
ll x;
ll y;
scanf("%lld%lld",&x,&y);
x=((x+ans)%(n-1))+1;
y=(y+ans)%w[0];
ll pivot=y-w[x];
w[x]=y;
cand_x=edge[x].fr;
cand_y=edge[x].sc;
x=edge[x].fr;
while(x){
edit(x,pivot);
x=par[x];
}
ans=alls.get(1,n,1,1,n);
printl("______________________");
printf("%lld\n",ans);
}
}