Submission #876694

#TimeUsernameProblemLanguageResultExecution timeMemory
876694mgl_diamondDynamic Diameter (CEOI19_diameter)C++17
49 / 100
5041 ms20252 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<ll, ll>; #define foru(i, l, r) for(int i=(l); i<=(r); ++i) #define ford(i, l, r) for(int i=(l); i>=(r); --i) #define fore(x, v) for(auto &x : v) #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() #define fi first #define se second #define file "input" template<class T> bool minimize(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template<class T> bool maximize(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } void setIO() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); if (fopen(file".inp", "r")) { freopen(file".inp", "r", stdin); freopen(file".out", "w", stdout); } } const int MOD = 998244353; const int N = 1e5+5; struct edge { int u, v; ll w; int oth(int x) { return u^v^x; } }; vector<edge> e; bool allstar = 1, bintree = 1; int n, q, up[N], par[N]; ll W; vector<int> adj[N]; ll f[N]; ii nax[N]; set<ii> al; void dfs_first(int u, int p, int h) { if (h > 20) bintree = 0; nax[u] = {0, 0}; fore(i, adj[u]) { int v = e[i].oth(u); if (v == p) continue; up[i] = u; par[v] = u; dfs_first(v, u, h+1); maximize(f[u], f[v] + e[i].w); ll tmp = f[v] + e[i].w; if (tmp > nax[u].fi) swap(nax[u].fi, nax[u].se), nax[u].fi = tmp; else if (tmp > nax[u].se) nax[u].se = tmp; } al.insert({-nax[u].fi-nax[u].se, u}); } void recalc(int u) { al.erase({-nax[u].fi-nax[u].se, u}); nax[u] = {0, 0}; f[u] = 0; fore(i, adj[u]) { int v = e[i].oth(u); if (v == par[u]) continue; maximize(f[u], f[v] + e[i].w); ll tmp = f[v] + e[i].w; if (tmp > nax[u].fi) swap(nax[u].fi, nax[u].se), nax[u].fi = tmp; else if (tmp > nax[u].se) nax[u].se = tmp; } al.insert({-nax[u].fi-nax[u].se, u}); } int main() { setIO(); cin >> n >> q >> W; e = vector<edge>(n-1); foru(i, 0, n-2) { int u, v; ll w; cin >> u >> v >> w; e[i] = {u, v, w}; adj[u].push_back(i); adj[v].push_back(i); } allstar = (sz(adj[1]) == n-1); foru(i, 1, n) bintree &= (sz(adj[i]) > 2); if (allstar) foru(i, 0, n-2) al.insert({-e[i].w, i}); else dfs_first(1, 0, 0); ll lst = 0; foru(i, 1, q) { ll ds, es; cin >> ds >> es; ds = (ds + lst) % (n - 1); es = (es + lst) % W; if (allstar) { al.erase({-e[ds].w, ds}); e[ds].w = es; al.insert({-e[ds].w, ds}); if (sz(al) == 1) lst = -(al.begin()->first); else lst = -(al.begin()->first + next(al.begin())->first); } else { int u = up[ds]; e[ds].w = es; while (true) { recalc(u); if (u == 1) break; u = par[u]; } lst = -(al.begin()->first); } cout << lst << "\n"; } }

Compilation message (stderr)

diameter.cpp: In function 'void setIO()':
diameter.cpp:22:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |     freopen(file".inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
diameter.cpp:23:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |     freopen(file".out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...