Submission #489192

#TimeUsernameProblemLanguageResultExecution timeMemory
489192cfalasDynamic Diameter (CEOI19_diameter)C++17
42 / 100
5059 ms46484 KiB
#include<bits/stdc++.h> using namespace std; #define mp make_pair #define INF 10000000 #define MOD 1000000007 #define MID ((l+r)/2) #define HASHMOD 2305843009213693951 #define ll long long #define ull unsigned long long #define F first #define S second typedef pair<ll, ll> ii; typedef pair<ii, ll> iii; typedef vector<ll> vi; typedef vector<ii> vii; typedef map<ll, ll> mii; #define EPS 1e-6 #define FOR(i,n) for(int i=0;i<((int)(n));i++) #define FORi(i,a,b) for(int i=((int)(a));i<((int)(b));i++) #define FOA(v, a) for(auto v : a) ll t, n; vi a, b; vector<vii> adj; ii f; ll cnt=0; vector<int> dfso; void dfs(int s, int p=-1, ll d=0){ dfso[s] = cnt++; if(d>f.F) f = max(f, {d, s}); FOA(v, adj[s]){ if(v.F!=p) dfs(v.F, s, d+v.S); } } vector<int> pr; struct node{ //ii l1={0,0}, l2={0,0}; set<ii> pa; int w=0; int id=1; ll ans=0; vector<node*> c; vector<node*> all_c; void build(int p=-1){ pr.push_back(id); FOA(v,adj[id]){ if(v.F!=p){ c.push_back(new node()); c[c.size()-1]->w = v.S; c[c.size()-1]->id = v.F; c[c.size()-1]->build(id); } } if(pa.size()==0) pa.insert({0,0}); FOA(v, c){ pa.insert({v->pa.rbegin()->F + v->w, v->id}); ans = max(ans, v->ans); //else if(v->w + v->l2 > l2) l2 = v->l2 + v->w; } if(pa.size()>1) ans = max(ans, (*pa.rbegin()).F + (*(++pa.rbegin())).F); //cout<<id<<" ("<<l1.F<<" "<<l1.S<<") ("<<l2.F<<" "<<l2.S<<")"<<" "<<ans<<endl; } void update(int s, ll t){ int pos = c.size()-1; FOR(i,c.size()-1){ if(dfso[c[i]->id] <= dfso[s] && dfso[s] < dfso[c[i+1]->id]) pos = i; } if(s==id){ //cout<<"CHAINGE "<<s<<endl; w = t; } else{ pa.erase({ c[pos]->pa.rbegin()->F + c[pos]->w, c[pos]->id}); c[pos]->update(s, t); pa.insert({c[pos]->pa.rbegin()->F + c[pos]->w, c[pos]->id}); //node*v = c[pos]; ans = 0; FOA(v, c){ ans = max(ans, v->ans); } if(pa.size()>1) ans = max(ans, (*pa.rbegin()).F + (*(++pa.rbegin())).F); } } }; int main(){ int q; ll w; cin>>n>>q>>w; adj.assign(n+1, vii()); dfso.assign(n+1, 0); vector<pair<ii,ii> > edg(n-1); set<ii> ws; vi wu(n); FOR(i,n-1){ ll a, b, c; cin>>a>>b>>c; ws.insert({-c, i}); wu[i] = c; edg[i] = {{a, adj[a].size()}, {b, adj[b].size()}}; adj[a].push_back({b,c}); adj[b].push_back({a,c}); } node root; root.build(); ll last=0; dfs(1); //FOR(i,n) cout<<dfso[i+1]<<" "; cout<<endl; while(q--){ ll a, b; cin>>a>>b; //cout<<"UPDATE\n"; a= (a + last) % (n-1); b = (b+last) % w; adj[edg[a].F.F][edg[a].F.S].S = b; adj[edg[a].S.F][edg[a].S.S].S = b; f = {0,1}; //dfs(1); //dfs(f.S); //cout<<a<<" "<<b<<endl; //cout<<"edg "<<edg[a].F.F<<" "<<edg[a].S.F<<endl; int upn = max(ii(dfso[edg[a].F.F], edg[a].F.F),ii(dfso[edg[a].S.F], edg[a].S.F)).S; //cout<<"pos "<<upn<<endl; root.update(upn, b); //last = f.F; last = root.ans; cout<<last<<endl; } }
#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...