Submission #917756

# Submission time Handle Problem Language Result Execution time Memory
917756 2024-01-28T17:31:07 Z NK_ Dynamic Diameter (CEOI19_diameter) C++17
100 / 100
291 ms 54100 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) int(x.size())

using ll = long long;
const ll INF = 7e18 + 10;

template<class T> using V = vector<T>;
using vi = V<int>;
using pi = pair<int, int>;
using vpi = V<pi>;
using vl = V<ll>;

struct T {
	ll a, b, c, ab, bc, abc;

	T() { a = b = c = ab = bc = abc = -INF; }
	T(ll x) {
		a = x, b = -2 * x, c = x, ab = -x, bc = -x, abc = 0;
	}

	void upd(ll d) {
		a += d, b -= 2 * d, c += d, ab -= d, bc -= d;
	}

};

struct Seg {
	const T ID = T(); T comb(T a, T b) {
		T c = T(); 
		c.a = max(a.a, b.a), c.b = max(a.b, b.b), c.c = max(a.c, b.c);
		c.ab = max({a.ab, b.ab, a.a + b.b}), c.bc = max({a.bc, b.bc, a.b + b.c});
		c.abc = max({a.abc, b.abc, a.ab + b.c, a.a + b.bc});
		return c;
	};

	V<T> seg; vl lazy; int N; void init(int n) {
		for(N = 1; N < n; ) N *= 2;
		seg.assign(2 * N, ID); lazy.assign(2 * N, 0);
	}

	void push(int x, int L, int R) {
		seg[x].upd(lazy[x]);
		if (L != R) for(int i = 0; i < 2; i++) lazy[2*x+i] += lazy[x];
		lazy[x] = 0;
	}

	void set(int p, T x) {
		seg[p += N] = x; for(p /= 2; p; p /= 2) pull(p);
	}

	void pull(int x) { seg[x] = comb(seg[2*x], seg[2*x+1]); }

	void upd(int l, int r, ll v, int x, int L, int R) {
		push(x, L, R); if (r < L || R < l) return;
		if (l <= L && R <= r) {
			lazy[x] += v; push(x, L, R); return;
		}

		int M = (L + R) / 2;
		upd(l, r, v, 2 * x, L, M);
		upd(l, r, v, 2 * x + 1, M + 1, R);
		pull(x);
	}

	void upd(int l, int r, ll v) { upd(l, r, v, 1, 0, N - 1); }
};


int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int N, M; ll MXW; cin >> N >> M >> MXW;

	V<vpi> adj(N); vi E(N - 1); vl W(N);

	for(int i = 0; i < N - 1; i++) {
		int u, v; cin >> u >> v >> W[i]; --u, --v;
		adj[u].pb(mp(v, i));
		adj[v].pb(mp(u, i));
	}
	
	Seg S; S.init(2*N); vi st(N), en(N);

	vi ord; int t = 0; ll dep = 0;
	function<void(int, int)> dfs = [&](int u, int p) {
		st[u] = en[u] = t; S.set(t, T(dep)); ord.pb(u); t++;

		for(auto& e : adj[u]) {
			int v, i; tie(v, i) = e;
			if (v == p) continue;

			dep += W[i];
			E[i] = v; dfs(v, u);
			dep -= W[i];

			S.set(t, T(dep)); en[u] = t; ord.pb(u); t++; 
		}

	};

	dfs(0, -1);

	// for(int i = 0; i < N; i++) {
	// 	cout << i << " => " << st[i] << " " << en[i] << endl;
	// }

	// for(auto& x : ord) {
	// 	cout << x << " ";
	// }
	// cout << endl;

	// cout << S.seg[1].abc << endl;


	ll last = 0;
	for(int i = 0; i < M; i++) {
		ll e, w; cin >> e >> w;

		e = ll(e + last) % ll(N - 1);
		w = ll(w + last) % MXW;

		// cout << e << " " << w << endl;

		int u = E[e];
		S.upd(st[u], en[u], w - W[e]);
		W[e] = w;

		last = S.seg[1].abc;
		cout << last << nl;
	}

	exit(0-0);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 3 ms 604 KB Output is correct
20 Correct 3 ms 604 KB Output is correct
21 Correct 3 ms 604 KB Output is correct
22 Correct 4 ms 860 KB Output is correct
23 Correct 7 ms 2652 KB Output is correct
24 Correct 7 ms 2652 KB Output is correct
25 Correct 7 ms 2652 KB Output is correct
26 Correct 8 ms 3164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 6 ms 348 KB Output is correct
5 Correct 35 ms 860 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 8 ms 604 KB Output is correct
11 Correct 40 ms 1036 KB Output is correct
12 Correct 3 ms 2652 KB Output is correct
13 Correct 4 ms 2652 KB Output is correct
14 Correct 4 ms 2652 KB Output is correct
15 Correct 13 ms 2856 KB Output is correct
16 Correct 53 ms 3240 KB Output is correct
17 Correct 63 ms 38856 KB Output is correct
18 Correct 64 ms 38772 KB Output is correct
19 Correct 65 ms 38856 KB Output is correct
20 Correct 86 ms 38852 KB Output is correct
21 Correct 157 ms 38852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 604 KB Output is correct
2 Correct 6 ms 824 KB Output is correct
3 Correct 26 ms 856 KB Output is correct
4 Correct 49 ms 1256 KB Output is correct
5 Correct 8 ms 4956 KB Output is correct
6 Correct 13 ms 5136 KB Output is correct
7 Correct 40 ms 5272 KB Output is correct
8 Correct 93 ms 5460 KB Output is correct
9 Correct 39 ms 19412 KB Output is correct
10 Correct 43 ms 19424 KB Output is correct
11 Correct 74 ms 19668 KB Output is correct
12 Correct 118 ms 19848 KB Output is correct
13 Correct 70 ms 38344 KB Output is correct
14 Correct 81 ms 38344 KB Output is correct
15 Correct 139 ms 38740 KB Output is correct
16 Correct 167 ms 38916 KB Output is correct
17 Correct 160 ms 38852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 242 ms 39736 KB Output is correct
2 Correct 198 ms 43768 KB Output is correct
3 Correct 217 ms 43720 KB Output is correct
4 Correct 201 ms 43900 KB Output is correct
5 Correct 200 ms 43724 KB Output is correct
6 Correct 180 ms 43896 KB Output is correct
7 Correct 232 ms 47556 KB Output is correct
8 Correct 221 ms 47816 KB Output is correct
9 Correct 265 ms 47548 KB Output is correct
10 Correct 232 ms 47564 KB Output is correct
11 Correct 254 ms 47864 KB Output is correct
12 Correct 225 ms 46712 KB Output is correct
13 Correct 291 ms 53936 KB Output is correct
14 Correct 288 ms 53960 KB Output is correct
15 Correct 274 ms 53960 KB Output is correct
16 Correct 279 ms 53956 KB Output is correct
17 Correct 272 ms 53060 KB Output is correct
18 Correct 227 ms 49068 KB Output is correct
19 Correct 268 ms 53956 KB Output is correct
20 Correct 275 ms 53956 KB Output is correct
21 Correct 280 ms 54100 KB Output is correct
22 Correct 280 ms 53892 KB Output is correct
23 Correct 284 ms 53192 KB Output is correct
24 Correct 224 ms 48956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 3 ms 604 KB Output is correct
20 Correct 3 ms 604 KB Output is correct
21 Correct 3 ms 604 KB Output is correct
22 Correct 4 ms 860 KB Output is correct
23 Correct 7 ms 2652 KB Output is correct
24 Correct 7 ms 2652 KB Output is correct
25 Correct 7 ms 2652 KB Output is correct
26 Correct 8 ms 3164 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 6 ms 348 KB Output is correct
31 Correct 35 ms 860 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 8 ms 604 KB Output is correct
37 Correct 40 ms 1036 KB Output is correct
38 Correct 3 ms 2652 KB Output is correct
39 Correct 4 ms 2652 KB Output is correct
40 Correct 4 ms 2652 KB Output is correct
41 Correct 13 ms 2856 KB Output is correct
42 Correct 53 ms 3240 KB Output is correct
43 Correct 63 ms 38856 KB Output is correct
44 Correct 64 ms 38772 KB Output is correct
45 Correct 65 ms 38856 KB Output is correct
46 Correct 86 ms 38852 KB Output is correct
47 Correct 157 ms 38852 KB Output is correct
48 Correct 2 ms 604 KB Output is correct
49 Correct 6 ms 824 KB Output is correct
50 Correct 26 ms 856 KB Output is correct
51 Correct 49 ms 1256 KB Output is correct
52 Correct 8 ms 4956 KB Output is correct
53 Correct 13 ms 5136 KB Output is correct
54 Correct 40 ms 5272 KB Output is correct
55 Correct 93 ms 5460 KB Output is correct
56 Correct 39 ms 19412 KB Output is correct
57 Correct 43 ms 19424 KB Output is correct
58 Correct 74 ms 19668 KB Output is correct
59 Correct 118 ms 19848 KB Output is correct
60 Correct 70 ms 38344 KB Output is correct
61 Correct 81 ms 38344 KB Output is correct
62 Correct 139 ms 38740 KB Output is correct
63 Correct 167 ms 38916 KB Output is correct
64 Correct 160 ms 38852 KB Output is correct
65 Correct 242 ms 39736 KB Output is correct
66 Correct 198 ms 43768 KB Output is correct
67 Correct 217 ms 43720 KB Output is correct
68 Correct 201 ms 43900 KB Output is correct
69 Correct 200 ms 43724 KB Output is correct
70 Correct 180 ms 43896 KB Output is correct
71 Correct 232 ms 47556 KB Output is correct
72 Correct 221 ms 47816 KB Output is correct
73 Correct 265 ms 47548 KB Output is correct
74 Correct 232 ms 47564 KB Output is correct
75 Correct 254 ms 47864 KB Output is correct
76 Correct 225 ms 46712 KB Output is correct
77 Correct 291 ms 53936 KB Output is correct
78 Correct 288 ms 53960 KB Output is correct
79 Correct 274 ms 53960 KB Output is correct
80 Correct 279 ms 53956 KB Output is correct
81 Correct 272 ms 53060 KB Output is correct
82 Correct 227 ms 49068 KB Output is correct
83 Correct 268 ms 53956 KB Output is correct
84 Correct 275 ms 53956 KB Output is correct
85 Correct 280 ms 54100 KB Output is correct
86 Correct 280 ms 53892 KB Output is correct
87 Correct 284 ms 53192 KB Output is correct
88 Correct 224 ms 48956 KB Output is correct
89 Correct 216 ms 42692 KB Output is correct
90 Correct 230 ms 43460 KB Output is correct
91 Correct 256 ms 45132 KB Output is correct
92 Correct 235 ms 45260 KB Output is correct
93 Correct 268 ms 49096 KB Output is correct
94 Correct 244 ms 47476 KB Output is correct
95 Correct 271 ms 49864 KB Output is correct
96 Correct 250 ms 48296 KB Output is correct
97 Correct 281 ms 49908 KB Output is correct
98 Correct 282 ms 52956 KB Output is correct
99 Correct 261 ms 49460 KB Output is correct
100 Correct 274 ms 49372 KB Output is correct