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

#TimeUsernameProblemLanguageResultExecution timeMemory
779531ThegeekKnight16Dynamic Diameter (CEOI19_diameter)C++17
100 / 100
378 ms62648 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int INF = 0x3f3f3f3f; struct node { int Mi, Mj, Mk, Mij, Mjk, Mijk; int lz; node(int I = 0, int J = 0, int K = 0, int IJ = 0, int JK = 0, int IJK = 0, int LZ = 0) : Mi(I), Mj(J), Mk(K), Mij(IJ), Mjk(JK), Mijk(IJK), lz(LZ) {} node operator+(node outro) { node resp; resp.Mi = resp.Mk = max(Mi, outro.Mi); resp.Mj = min(Mj, outro.Mj); resp.Mij = max({Mij, Mi - 2*outro.Mj, outro.Mij}); resp.Mjk = max({Mjk, -2*Mj + outro.Mk, outro.Mjk}); resp.Mijk = max({Mijk, Mij + outro.Mk, Mi + outro.Mjk, outro.Mijk}); return resp; } }; const int MAXN = 2e5 + 10; array<node, 4*MAXN> seg; array<vector<pair<int, int>>, MAXN> grafo; array<int, MAXN> peso, idIn, idOut; int temp = 0; void refresh(int pos, int ini, int fim) { if (seg[pos].lz == 0) return; int x = seg[pos].lz; seg[pos].lz = 0; seg[pos].Mi += x; seg[pos].Mj += x; seg[pos].Mk += x; seg[pos].Mij -= x; seg[pos].Mjk -= x; seg[pos].Mijk += 0; if (ini == fim) return; int e = 2*pos; int d = 2*pos + 1; seg[e].lz += x; seg[d].lz += x; } void update(int pos, int ini, int fim, int p, int q, int val) { refresh(pos, ini, fim); if (q < ini || p > fim) return; if (p <= ini && fim <= q) { seg[pos].lz += val; refresh(pos, ini, fim); return; } int m = (ini + fim) >> 1; int e = 2*pos; int d = 2*pos + 1; update(e, ini, m, p, q, val); update(d, m+1, fim, p, q, val); seg[pos] = seg[e] + seg[d]; } node query(int pos, int ini, int fim, int p, int q) { refresh(pos, ini, fim); if (q < ini || p > fim) return node(-INF, INF, -INF, -INF, -INF, -INF); if (p <= ini && fim <= q) return seg[pos]; int m = (ini + fim) >> 1; int e = 2*pos; int d = 2*pos + 1; return (query(e, ini, m, p, q) + query(d, m+1, fim, p, q)); } void dfs(int v, int p, int paiEdge) { ++temp; if (paiEdge != -1) idIn[paiEdge] = temp; for (auto [viz, e] : grafo[v]) { if (viz == p) continue; dfs(viz, v, e); } ++temp; if (paiEdge != -1) idOut[paiEdge] = temp; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int N, Q, W; cin >> N >> Q >> W; for (int i = 1; i < N; i++) { int X, Y, P; cin >> X >> Y >> P; grafo[X].emplace_back(Y, i); grafo[Y].emplace_back(X, i); peso[i] = P; } dfs(1, 1, -1); --temp; for (int i = 1; i < N; i++) update(1, 1, temp, idIn[i], idOut[i]-1, peso[i]); // node aux = query(1, 1, temp, 1, temp); // cerr << aux.Mi << " " << aux.Mj << " " << aux.Mk << " " << aux.Mij << " " << aux.Mjk << " " << aux.Mijk << '\n'; int last = 0; while (Q--) { int D, E; cin >> D >> E; D = (D + last) % (N - 1); ++D; E = (E + last) % W; update(1, 1, temp, idIn[D], idOut[D]-1, E - peso[D]); peso[D] = E; last = query(1, 1, temp, 1, temp).Mijk; cout << last << '\n'; } }
#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...