Submission #1303039

#TimeUsernameProblemLanguageResultExecution timeMemory
1303039Faisal_SaqibDynamic Diameter (CEOI19_diameter)C++20
42 / 100
5094 ms33096 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define int long long #define pb push_back const ll N=100100; vector<pair<ll,ll>> ma[N]; pair<ll,ll> edg[N]; ll dp[N][2],h[N],sz[N],dist[N],par[N],ans=0,wei[N]; multiset<ll> sub[N]; multiset<ll> pos; void dfs(int x,int p=-1) { dp[x][1]=dp[x][0]=0; sub[x].insert(0); for(auto it:ma[x]) { int y=it.first; int was=it.second; if(y==p)continue; edg[was]={x,y}; // par, child ll w=wei[was]; h[y]=h[x]+w; par[y]=x; dfs(y,x); sub[x].insert(dp[y][0]); if(dp[y][0]+w > dp[x][0]) { dp[x][1]=dp[x][0]; dp[x][0]=dp[y][0]+w; } else{ dp[x][1]=max(dp[x][1],dp[y][0]+w); } } pos.insert(dp[x][0]+dp[x][1]); // ans=max(ans,dp[x][0]+dp[x][1]); // cout<<x<<' '<<dp[x][0]<<' '<<dp[x][1]<<endl; } int32_t main() { ll n,q,w; cin>>n>>q>>w; for(int i=0;i<n-1;i++) { ll a,b,w; cin>>a>>b>>w; wei[i]=w; edg[i]={a,b}; ma[a].pb({b,i}); ma[b].pb({a,i}); } h[1]=1; dfs(1); // cout<<ans<<endl; ans=0; while(q--) { ll d,nw; cin>>d>>nw; d=(d+ans)%(n-1); nw=(nw+ans)%w; wei[d]=nw; ans=0; ll x=edg[d].second; while(x>=1) { ll p=par[x]; pos.erase(pos.find(dp[x][0]+dp[x][1])); dp[x][1]=dp[x][0]=0; for(auto it:ma[x]) { int y=it.first; int was=it.second; if(y==p)continue; ll w=wei[was]; h[y]=h[x]+w; par[y]=x; if(dp[y][0]+w > dp[x][0]) { dp[x][1]=dp[x][0]; dp[x][0]=dp[y][0]+w; } else{ dp[x][1]=max(dp[x][1],dp[y][0]+w); } } pos.insert(dp[x][0]+dp[x][1]); x=p; } ans=*rbegin(pos); cout<<ans<<endl; } } // 6164 // 7812 // 8385 // 6737 // 6738 // 7205 // 6641 // 7062 // 6581 // 515
#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...