(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 #1099096

#TimeUsernameProblemLanguageResultExecution timeMemory
1099096anhthiDynamic Diameter (CEOI19_diameter)C++14
100 / 100
851 ms55716 KiB
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define ll long long #define mp(x, y) make_pair(x, y) #define sz(v) ((int) (v).size()) #define all(v) (v).begin(), (v).end() #define MASK(i) (1LL << (i)) #define BIT(x, y) (((x) >> (y)) & 1) #define pb push_back #define max_rng *max_element #define min_rng *min_element #define rep(i, n) for(int i = 1, _n = (n); i <= _n; ++i) #define forn(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i) #define ford(i, a, b) for(int i = (a), _b = (b); i >= _b; --i) template <class X, class Y> inline bool maximize(X &x, Y y) { return (x < y ? x = y, true : false); } template <class X, class Y> inline bool minimize(X &x, Y y) { return (x > y ? x = y, true : false); } template <class X> inline void compress(vector<X> &a) { sort(all(a)); a.resize(unique(all(a)) - a.begin()); } int ctz(ll x) { return __builtin_ctzll(x); } int lg(ll x) { return 63 - __builtin_clzll(x); } int popcount(ll x) { return __builtin_popcount(x); } mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ll rand(ll l, ll r) { return l + abs((ll) rng()) % (r - l + 1); } const ll oo = (ll) 1e17; const int inf = (ll) 1e9 + 7, mod = (ll) 1e9 + 7; const int N = 1e5 + 5, BASE = 307, LOG = 18; void add(int &x, int y) { x += y; if (x >= mod) x -= mod; } void sub(int &x, int y) { x -= y; if (x < 0) x += mod; } int n, Q; ll W; struct Edge { int u, v; ll w; void input() { cin >> u >> v >> w; } } e[N]; vector<pair<int, int>> g[N]; namespace LCA { int timer = 0; ll dist[N]; int d[N], in[N], out[N], tour[N << 1]; void dfs(int u = 1, int p = 0) { in[u] = ++timer; tour[timer] = u; for (pair<int, int> i : g[u]) { int v = i.first; if (v == p) continue; d[v] = d[u] + 1; dist[v] = dist[u] + e[i.second].w; dfs(v, u); tour[++timer] = u; } out[u] = timer; } int rmq[N << 1][LOG + 5]; int min_rmq(int u, int v) { return d[u] < d[v] ? u : v; } void init_rmq() { rep(i, timer) rmq[i][0] = tour[i]; rep(j, LOG) { rep(i, timer - MASK(j) + 1) { rmq[i][j] = min_rmq(rmq[i][j-1], rmq[i + MASK(j-1)][j-1]); } } return; } int lca(int u, int v) { u = in[u]; v = in[v]; if (u > v) swap(u, v); int h = lg(v - u + 1); return min_rmq(rmq[u][h], rmq[v - MASK(h) + 1][h]); } } using namespace LCA; namespace CTDL { ll bit[N << 1]; void upd(int p, ll d) { for (; p <= timer; p += p & -p) bit[p] += d; return; } void upd_range(int l, int r, ll d) { upd(l, +d); upd(r + 1, -d); } ll get(int p) { ll ans = 0; for (; p > 0; p -= p & -p) ans += bit[p]; return ans; } ll getDist(int u, int v) { return get(in[u]) + get(in[v]) - 2 * get(in[lca(u, v)]); } struct node { ll ans; int u, v; node(ll a = 0, int b = 0, int c = 0) { ans = a; u = b; v = c; } void trans(int _u, int _v) { if (maximize(ans, getDist(_u, _v))) { u = _u; v = _v; } } void debug() { cout << ans << ' ' << u << ' ' << v << '\n'; } }; node st[(N << 3) + 5]; node combine(node &x, node &y) { if (!x.u) return y; if (!y.u) return x; node res = (x.ans < y.ans ? y : x); res.trans(x.u, y.u); res.trans(x.u, y.v); res.trans(x.v, y.u); res.trans(x.v, y.v); return res; } void build(int i, int l, int r) { if (l == r) { st[i] = node(0, tour[l], tour[l]); } else { int m = (l + r) >> 1; build(2 * i, l, m); build(2 * i + 1, m + 1, r); st[i] = combine(st[2 * i], st[2 * i + 1]); } return; } void build() { return build(1, 1, timer); } void upd(int i, int l, int r, int u, int v) { if (l > v || r < u) return; if (l >= u && r <= v) return; int m = (l + r) >> 1; upd(2 * i, l, m, u, v); upd(2 * i + 1, m + 1, r, u, v); st[i] = combine(st[2 * i], st[2 * i + 1]); } void upd(int u, int v) { return upd(1, 1, timer, u, v); } ll solve() { return st[1].ans; } } using namespace CTDL; signed main(void) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("input.inp", "r", stdin); cin >> n >> Q >> W; rep(i, n - 1) { e[i].input(); g[e[i].u].pb(mp(e[i].v, i)); g[e[i].v].pb(mp(e[i].u, i)); } dfs(); init_rmq(); rep(i, n - 1) { if (d[e[i].u] > d[e[i].v]) swap(e[i].u, e[i].v); } rep(i, timer) { upd_range(i, i, dist[tour[i]]); } build(); ll lst = 0; rep(_, Q) { int d; ll g; cin >> d >> g; g = (g + lst) % W; d = (d + lst) % (n - 1) + 1; Edge &cur = e[d]; ll change = g - cur.w; cur.w = g; upd_range(in[cur.v], out[cur.v], change); upd(in[cur.v], out[cur.v]); cout << (lst = solve()) << '\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...