Submission #619272

#TimeUsernameProblemLanguageResultExecution timeMemory
619272ZaniteDynamic Diameter (CEOI19_diameter)C++17
49 / 100
5037 ms42444 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<ll, ll>; using Edge = pair<ll, pll>; #define fi first #define se second #define debug(x) cout << #x << " = " << x << '\n' const int maxN = 1e5 + 5; ll n, q, w; set<pll> adj[maxN]; vector<Edge> edges; ll id[maxN], dist[maxN]; multiset<ll> dpq; namespace Subtask12 { vector<ll> dist; void dfs(ll cur, ll par, ll cdist) { dist[cur] = cdist; for (auto &[nxt, w] : adj[cur]) { if (nxt == par) continue; dfs(nxt, cur, cdist+w); } } ll solve() { dist.assign(n+1, 0); dfs(1, 0, 0); ll e1 = -1, mx = -1; for (ll i = 1; i <= n; i++) { if (dist[i] > mx) { mx = dist[i]; e1 = i; } } dist.assign(n+1, 0); dfs(e1, 0, 0); ll ret = 0; for (ll i = 1; i <= n; i++) { ret = max(ret, dist[i]); } return ret; } void caller() { ll last = 0; //cout << solve() << '\n'; for (ll d, e, i = 1; i <= q; i++) { scanf("%lld %lld", &d, &e); d = (d + last) % (n - 1); e = (e + last) % w; //debug(d); debug(e); auto &[w, p] = edges[d]; adj[p.fi].erase({p.se, w}); adj[p.se].erase({p.fi, w}); edges[d].fi = e; adj[p.fi].insert({p.se, e}); adj[p.se].insert({p.fi, e}); /* for (ll i = 1; i <= n; i++) { cout << i << ": "; for (auto &[j, k] : adj[i]) { cout << "{" << j << ", " << k << "} "; } cout << '\n'; } */ last = solve(); printf("%lld\n", last); } } } namespace Subtask4 { ll initDia[maxN], parent[maxN]; ll timer, tin[maxN], tout[maxN], byTin[maxN]; struct Segtree { ll sz; vector<ll> seg, lazy; Segtree(ll sz) : sz(sz) { seg = lazy = vector<ll>((sz << 2) + 1, 0); } void updateNode(ll pos, ll val) { seg[pos] += val; lazy[pos] += val; } void checkLazy(ll pos) { if (lazy[pos] != 0) { ll lc = pos << 1, rc = lc | 1; updateNode(lc, lazy[pos]); updateNode(rc, lazy[pos]); lazy[pos] = 0; } } void update(ll pos, ll l, ll r, ll ul, ll ur, ll val) { if (l > r || ul > ur || l > ur || ul > r) return; if (ul <= l && r <= ur) {updateNode(pos, val); return;} checkLazy(pos); ll m = (l+r) >> 1, lc = pos << 1, rc = lc | 1; update(lc, l, m, ul, ur, val); update(rc, m+1, r, ul, ur, val); seg[pos] = max(seg[lc], seg[rc]); } void update(ll ul, ll ur, ll val) {update(1, 1, sz, ul, ur, val);} ll query(ll pos, ll l, ll r, ll ul, ll ur) { if (l > r || ul > ur || l > ur || ul > r) return 0; if (ul <= l && r <= ur) return seg[pos]; checkLazy(pos); ll m = (l+r) >> 1, lc = pos << 1, rc = lc | 1; return max(query(lc, l, m, ul, ur), query(rc, m+1, r, ul, ur)); } ll query(ll ul, ll ur) {return query(1, 1, sz, ul, ur);} void print() { for (ll i = 1; i <= sz; i++) { cout << query(i, i) << ' '; } cout << '\n'; } } S(1), T(1); void compute(ll cur) { ll cdist = S.query(tin[cur], tin[cur]); vector<ll> children(2, cdist); for (auto &[nxt, w] : adj[cur]) { children.push_back(S.query(tin[nxt], tout[nxt])); } sort(children.begin(), children.end()); reverse(children.begin(), children.end()); //for (auto i : children) {cout << i << ' ';} cout <<'\n'; //cout << cur << ' ' << -T.query(cur, cur) + children[0] + children[1] - 2ll*cdist << '\n'; T.update(cur, cur, -T.query(cur, cur) + children[0] + children[1] - 2ll*cdist); } #define debug(x) cout << #x << " = " << x << '\n' void dfs(ll cur, ll par, ll cdist, ll pw) { //debug(cur); tin[cur] = ++timer; byTin[timer] = cur; parent[cur] = par; dist[cur] = cdist; S.update(tin[cur], tin[cur], cdist); adj[cur].erase({par, pw}); for (auto &[nxt, w] : adj[cur]) { if (nxt == par) continue; dfs(nxt, cur, cdist+w, w); } compute(cur); tout[cur] = timer; } void caller() { S = T = Segtree(n); dfs(1, 0, 0, 0); for (auto &[w, p] : edges) { if (tin[p.fi] > tin[p.se]) {swap(p.fi, p.se);} } //S.print(); T.print(); /* for (ll i = 1; i <= n; i++) { cout << i << ": "; for (auto &[j, k] : adj[i]) { cout << "{" << j << ", " << k << "} "; } cout << '\n'; } */ ll last = 0; for (ll d, e, i = 1; i <= q; i++) { scanf("%lld %lld", &d, &e); d = (d + last) % (n - 1); e = (e + last) % w; //debug(d); debug(e); auto &[w, p] = edges[d]; //debug(e-w); S.update(tin[p.se], tout[p.se], e-w); adj[p.fi].erase({p.se, w}); edges[d].fi = e; adj[p.fi].insert({p.se, e}); for (ll cur = p.fi; cur > 0; cur = parent[cur]) { //debug(cur); compute(cur); } //S.print(); T.print(); last = T.query(1, n); printf("%lld\n", last); } } } int main() { scanf("%lld %lld %lld", &n, &q, &w); bool Star = 1; bool BinTree = 1; for (ll a, b, c, i = 1; i < n; i++) { scanf("%lld %lld %lld", &a, &b, &c); adj[a].insert({b, c}); adj[b].insert({a, c}); edges.push_back({c, {a, b}}); if (a != 1 && b != 1) {Star = 0;} int x = min(a, b), y = max(a, b); if (y != 2*x && y != (2*x + 1)) {BinTree = 0;} if (b == 1) swap(a, b); id[i-1] = b; dist[b] = c; dpq.insert(c); } if (Star) { dpq.insert(0); ll last = 0; for (ll d, e, i = 1; i <= q; i++) { scanf("%lld %lld", &d, &e); d = (d + last) % (n - 1); e = (e + last) % w; ll change = id[d]; dpq.erase(dpq.lower_bound(dist[change])); dist[change] = e; dpq.insert(e); auto it = dpq.rbegin(); last = *it; it++; last += *it; printf("%lld\n", last); } } else if (BinTree) { Subtask4::caller(); } else { Subtask12::caller(); } }

Compilation message (stderr)

diameter.cpp: In function 'void Subtask12::caller()':
diameter.cpp:59:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |   scanf("%lld %lld", &d, &e);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~
diameter.cpp: In function 'void Subtask4::caller()':
diameter.cpp:209:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  209 |   scanf("%lld %lld", &d, &e);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~
diameter.cpp: In function 'int main()':
diameter.cpp:238:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  238 |  scanf("%lld %lld %lld", &n, &q, &w);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diameter.cpp:243:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  243 |   scanf("%lld %lld %lld", &a, &b, &c);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diameter.cpp:263:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  263 |    scanf("%lld %lld", &d, &e);
      |    ~~~~~^~~~~~~~~~~~~~~~~~~~~
#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...