(UPD: 2024-12-04 14:48 UTC) Judge is not working due to Cloudflare incident. (URL) We can do nothing about it, sorry. After the incident is resolved, we will grade all submissions.

Submission #740655

#TimeUsernameProblemLanguageResultExecution timeMemory
740655anhduc2701Dynamic Diameter (CEOI19_diameter)C++17
100 / 100
4521 ms382856 KiB
/* #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimize("unroll-loops") */ #include<bits/stdc++.h> #define int long long using namespace std; #define all(x) x.begin(), x.end() #define len(x) ll(x.size()) #define eb emplace_back #define PI acos(-1.0) #define fi first #define se second #define mp make_pair #define pb push_back #define MIN(v) *min_element(all(v)) #define MAX(v) *max_element(all(v)) #define BIT(x,i) (1&((x)>>(i))) #define MASK(x) (1LL<<(x)) #define task "tnc" typedef long long ll; typedef long double ld; const ll INF=1e18; const int maxn=1e6+5; const int mod=1e9+7; const int mo=998244353; using pi=pair<ll,ll>; using vi=vector<ll>; using pii=pair<pair<ll,ll>,ll>; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n,q,w; vector<pair<int,int>>G[maxn]; pair<int,int>ed[maxn]; int we[maxn]; int ok[maxn]; int tin[maxn][18]; int tout[maxn][18]; int eu[maxn][18]; int par[maxn][18]; int dis[maxn][18]; int belong[maxn][18]; int ti=0; struct ST{ vector<pair<int,int>>st; vector<int>lazy; vector<int>eu; int n; int l=0; void init(int _n){ n=_n; eu.resize(n+1,0); st.resize(4*(n+1),{0,0}); lazy.resize(4*(n+1),0); } void push(int id){ st[id*2].fi+=lazy[id]; st[id*2+1].fi+=lazy[id]; lazy[id*2]+=lazy[id]; lazy[id*2+1]+=lazy[id]; lazy[id]=0; } void update(int id,int l,int r,int u,int v,int val,int val1){ if(r<u || v<l){ return; } if(u<=l && r<=v){ if(val1!=0){ st[id].se=val1; } st[id].fi+=val; lazy[id]+=val; return; } int mid=(l+r)/2; push(id); update(id*2,l,mid,u,v,val,val1); update(id*2+1,mid+1,r,u,v,val,val1); st[id]=max(st[id*2],st[id*2+1]); } pair<int,int>get(int id,int l,int r,int u,int v){ if(v<=0 || u>n)return {0,0}; if(r<u || v<l)return {0,0}; if(u<= l && r<=v){ return st[id]; } push(id); int mid=(l+r)/2; return max(get(id*2,l,mid,u,v),get(id*2+1,mid+1,r,u,v)); } void update(int id,int val,int ti){ if(par[ed[id].se][ti]==ed[id].fi){ update(1,1,n,tin[ed[id].se][ti] ,tout[ed[id].se][ti],val,0); } else{ update(1,1,n,tin[ed[id].fi][ti],tout[ed[id].fi][ti],val,0); } } int calc(int ti){ int p=belong[st[1].se][ti]; pair<int,int>ma=max(get(1,1,n,1,tin[p][ti]-1),get(1,1,n,tout[p][ti]+1,n)); return st[1].fi+ma.fi; } }A[maxn]; void dfs(int u,int pa,int h,int thuoc,int siz,int be){ tin[u][h]=++ti; A[thuoc].update(1,1,siz,tin[u][h],tin[u][h],dis[u][h],u); belong[u][h]=be; for(auto v:G[u]){ if(v.fi==pa || ok[v.fi]==1)continue; dis[v.fi][h]=dis[u][h]+we[v.se]; par[v.fi][h]=u; dfs(v.fi,u,h,thuoc,siz,be); } tout[u][h]=ti; } int id; int sz[maxn]; void dfsz(int u,int pa){ sz[u]=1; for(auto v:G[u]){ if(v.fi==pa || ok[v.fi]==1)continue; dfsz(v.fi,u); sz[u]+=sz[v.fi]; } } int fin(int u,int pa,int siz){ for(auto v:G[u]){ if(v.fi==pa || ok[v.fi]==1)continue; if(sz[v.fi]>siz/2 ){ return fin(v.fi,u,siz); } } return u; } int l=0; int dep[maxn]; int thuoc[maxn]; int pa[maxn]; multiset<int>sus; void build(int u,int cha){ dfsz(u,-1); int cen=fin(u,-1,sz[u]); pa[cen]=cha; dep[cen]=dep[cha]+1; ++l; thuoc[cen]=l; ok[cen]=1; A[l].init(sz[u]); ti=0; for(auto v:G[cen]){ if(ok[v.fi]==1)continue; dis[v.fi][dep[cen]]=we[v.se]; par[v.fi][dep[cen]]=cen; dfs(v.fi,-1,dep[cen],l,sz[u],v.fi); } sus.insert(A[l].calc(dep[cen])); for(auto v:G[cen]){ if(ok[v.fi]==1){ continue; } build(v.fi,cen); } } void update(int x,int ed,int val){ while(x!=0){ sus.erase(sus.find(A[thuoc[x]].calc(dep[x]))); A[thuoc[x]].update(ed,-we[ed],dep[x]); A[thuoc[x]].update(ed,val,dep[x]); sus.insert(A[thuoc[x]].calc(dep[x])); //cout<<A[thuoc[x]].calc(dep[x]); x=pa[x]; } } signed main() { cin.tie(0),cout.tie(0)->sync_with_stdio(0); //freopen(task".inp" , "r" , stdin); //freopen(task".out" , "w" , stdout); cin>>n>>q>>w; for(int i=0;i<n-1;i++){ int u,v,w; cin>>u>>v>>w; ed[i]={u,v}; we[i]=w; G[u].pb({v,i}); G[v].pb({u,i}); } build(1,0); int last=0; for(int i=1;i<=q;i++){ int d,e; cin>>d>>e; d=(last+d)%(n-1); e=(e+last)%w; if(dep[ed[d].fi]>dep[ed[d].se])swap(ed[d].fi,ed[d].se); update(ed[d].fi,d,e); we[d]=e; last=(*sus.rbegin()); cout<<(*sus.rbegin())<<"\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...