Submission #831957

# Submission time Handle Problem Language Result Execution time Memory
831957 2023-08-20T18:35:12 Z NK_ Grapevine (NOI22_grapevine) C++17
68 / 100
1842 ms 1048576 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
 
using namespace std;
 
#define nl '\n'
#define pb push_back
#define pf push_front
 
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
 
template<class T> using V = vector<T>;
using pi = pair<int, int>;
using vi = V<int>;
using vpi = V<pi>;
 
using ll = long long;
using pl = pair<ll, ll>;
using vpl = V<pl>;
using vl = V<ll>;
 
using db = long double;
 
template<class T> using pq = priority_queue<T, V<T>, greater<T>>;
 
const int MOD = 1e9 + 7;
const ll INFL = ll(1e17);
const int nax = 1e5 + 5;
const int LG = 17;
const int MEM = 7e7;

int amt = 5 * nax * LG;

// START OF SEGTREE
struct Seg {
	const ll ID = INFL, IDL = 0; ll comb(ll a, ll b) { return min(a, b); }
	int n; vl seg, lazy;
	void init(int N) {
		n = 1; while(n < N) n *= 2;
		amt += 4 * n;
		seg.assign(2*n, ID); lazy.assign(2*n, IDL);
	}
 
	void pull(int x) { seg[x] = comb(seg[2*x], seg[2*x+1]); } 
 
	void push(int x, int L, int R) {
		seg[x] += lazy[x];
		if (L != R) for(int i = 0; i < 2; i++) lazy[2*x+i] += lazy[x];
		lazy[x] = IDL;
	}
 
	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);
	}
 
	ll query(int l, int r, int x, int L, int R) {
		push(x, L, R); if (r < L || R < l) return 2 * ID;
		if (l <= L && R <= r) return seg[x];
		int M = (L + R) / 2;
		return comb(query(l, r, 2*x, L, M), query(l, r, 2*x+1, M+1, R));
	}
 
	void upd(int l, int r, ll v) { upd(l, r, v, 1, 0, n - 1); }
	ll query(int l, int r) { return query(l, r, 1, 0, n - 1); }
	ll query() { return seg[1]; }
};
// END OF SEGTREE
 
 
 
// MAIN
vpi adj[nax]; int W[nax];
 
// CENTROID
int sub[nax], proc[nax], siz[nax];
 
// CENTROID SUBTREES
int eid[nax][LG], st[nax][LG], en[nax][LG], par[nax][LG];
Seg T[nax];
// __[u][t] = a property of the subtree rooted at u's t-th parent in the centroid tree
 
// START OF CENTROID
void gen(int u, int p = -1) {
	sub[u] = 1;
	for(auto& e : adj[u]) {
		int v, i; tie(v, i) = e;
		if (v != p && !proc[v]) {
			gen(v, u); sub[u] += sub[v];
		}
	}
}
 
int t = 0, C = -1;
int LVL = 0;
void dfs(int u, int p = -1) {
	par[u][LVL] = C; st[u][LVL] = t++;
	for(auto& e : adj[u]) {
		int v, i; tie(v, i) = e;
		if (v != p && !proc[v]) {
			eid[v][LVL] = i;
			dfs(v, u); sub[u] += sub[v];
		}
	}
	en[u][LVL] = t - 1;
}
 
int find(int u, int n, int p = -1) {
	for(auto& e : adj[u]) {
		int v, i; tie(v, i) = e;
		if (v != p && !proc[v]) {
			if (2 * sub[v] > n) return find(v, n, u);
		}
	}
	return u;
}
 
int init(int u = 0) {
	gen(u); int c = find(u, sub[u]);
 
	t = 0, C = c, eid[c][LVL] = -1; dfs(c);
	proc[c] = 1;
 
	siz[c] = sub[u];
	assert(t <= siz[c]);
	T[c].init(siz[c]);
	assert(amt <= MEM);
 
	for(auto& e : adj[c]) {
		int v, i; tie(v, i) = e;
		if (!proc[v]) {
			++LVL; init(v); --LVL;
		}
	}
 
	return c;
}
// END OF CENTROID
 
int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	memset(par, -1, sizeof(par));
 
	int N, Q; cin >> N >> Q;
 
	unordered_map<ll, int> E;
	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));
		E[u * 1LL * N + v] = E[v * 1LL * N + u] = i;
	}
 
	int R = init();
 
	for(int u = 0; u < N; u++) {
		for(int p = 0; p < LG; p++) {
			int e = eid[u][p], x = par[u][p]; if (x == -1) continue;
			if (e != -1) T[x].upd(st[u][p], en[u][p], W[e]);
		}
	}
 
	for(int q = 0; q < Q; q++) {
		int type; cin >> type;
 
		if (type == 1) {
			int u; cin >> u; --u;
			
			ll ans = 2 * INFL;
 
			for(int p = 0; p < LG; p++) {
				int x = par[u][p], pos = st[u][p]; 
 				if (x == -1) continue;
 
				ll dst = T[x].query(pos, pos);
				if (dst >= INFL) dst -= INFL;
 
				ans = min(ans, dst + T[x].query());
			}
			cout << (ans >= INFL ? -1 : ans) << nl;
		}
 
		if (type == 2) {
			int u; cin >> u; --u;
			for(int p = 0; p < LG; p++) {
				int x = par[u][p], pos = st[u][p]; 
 				if (x == -1) continue;
 
				if (T[x].query(pos, pos) >= INFL) T[x].upd(pos, pos, -INFL);
				else T[x].upd(pos, pos, +INFL);
			}
		}
 
		if (type == 3) {
			int u, v, w; cin >> u >> v >> w; --u, --v;
 
			int i = E[u * 1LL * N + v];
 
			for(int rep = 0; rep < 2; rep++) {
				for(int p = 0; p < LG; p++) {
					int x = par[u][p], e = eid[u][p]; if (x == -1) continue;
 
					if (e == i) T[x].upd(st[u][p], en[u][p], w - W[i]);
				}
 
				swap(u, v);
			}
 
			W[i] = w;
		}
 
	}
 
 
 
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:164:6: warning: unused variable 'R' [-Wunused-variable]
  164 |  int R = init();
      |      ^
# Verdict Execution time Memory Grader output
1 Correct 13 ms 17876 KB Output is correct
2 Correct 14 ms 17920 KB Output is correct
3 Correct 14 ms 18004 KB Output is correct
4 Correct 16 ms 17928 KB Output is correct
5 Correct 14 ms 17904 KB Output is correct
6 Correct 16 ms 18008 KB Output is correct
7 Correct 10 ms 17288 KB Output is correct
8 Correct 10 ms 17292 KB Output is correct
9 Correct 10 ms 17308 KB Output is correct
10 Correct 14 ms 18004 KB Output is correct
11 Correct 15 ms 17952 KB Output is correct
12 Correct 17 ms 18004 KB Output is correct
13 Correct 10 ms 17364 KB Output is correct
14 Correct 11 ms 17364 KB Output is correct
15 Correct 9 ms 17292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1242 ms 115064 KB Output is correct
2 Correct 1219 ms 117604 KB Output is correct
3 Correct 1232 ms 116536 KB Output is correct
4 Correct 1008 ms 125076 KB Output is correct
5 Correct 949 ms 117708 KB Output is correct
6 Correct 990 ms 120116 KB Output is correct
7 Correct 175 ms 66120 KB Output is correct
8 Correct 173 ms 65224 KB Output is correct
9 Correct 213 ms 65320 KB Output is correct
10 Correct 988 ms 122040 KB Output is correct
11 Correct 989 ms 120652 KB Output is correct
12 Correct 1010 ms 123580 KB Output is correct
13 Correct 177 ms 65404 KB Output is correct
14 Correct 195 ms 65464 KB Output is correct
15 Correct 206 ms 65856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1442 ms 525732 KB Output is correct
2 Correct 1497 ms 528896 KB Output is correct
3 Correct 1544 ms 530180 KB Output is correct
4 Correct 1429 ms 506548 KB Output is correct
5 Correct 1449 ms 508216 KB Output is correct
6 Correct 1436 ms 505252 KB Output is correct
7 Correct 1391 ms 504044 KB Output is correct
8 Correct 1492 ms 525916 KB Output is correct
9 Correct 1648 ms 505500 KB Output is correct
10 Correct 1588 ms 529144 KB Output is correct
11 Correct 1842 ms 507032 KB Output is correct
12 Correct 1618 ms 502124 KB Output is correct
13 Correct 1694 ms 526184 KB Output is correct
14 Correct 1663 ms 502144 KB Output is correct
15 Correct 1510 ms 507260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1362 ms 125028 KB Output is correct
2 Correct 1440 ms 130940 KB Output is correct
3 Correct 1295 ms 114448 KB Output is correct
4 Correct 1249 ms 125360 KB Output is correct
5 Correct 1183 ms 121608 KB Output is correct
6 Correct 1258 ms 122968 KB Output is correct
7 Correct 199 ms 65536 KB Output is correct
8 Correct 222 ms 66684 KB Output is correct
9 Correct 226 ms 65668 KB Output is correct
10 Correct 1149 ms 138644 KB Output is correct
11 Correct 1047 ms 119684 KB Output is correct
12 Correct 1089 ms 123588 KB Output is correct
13 Correct 199 ms 65352 KB Output is correct
14 Correct 195 ms 66616 KB Output is correct
15 Correct 215 ms 65944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1369 ms 123412 KB Output is correct
2 Correct 1359 ms 126760 KB Output is correct
3 Correct 1294 ms 120644 KB Output is correct
4 Correct 1106 ms 126916 KB Output is correct
5 Correct 1195 ms 126240 KB Output is correct
6 Correct 1079 ms 123236 KB Output is correct
7 Correct 199 ms 65548 KB Output is correct
8 Correct 193 ms 66112 KB Output is correct
9 Correct 211 ms 66232 KB Output is correct
10 Correct 1107 ms 117084 KB Output is correct
11 Correct 1081 ms 120800 KB Output is correct
12 Correct 1075 ms 132084 KB Output is correct
13 Correct 192 ms 66288 KB Output is correct
14 Correct 207 ms 65652 KB Output is correct
15 Correct 187 ms 65716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 17876 KB Output is correct
2 Correct 14 ms 17920 KB Output is correct
3 Correct 14 ms 18004 KB Output is correct
4 Correct 16 ms 17928 KB Output is correct
5 Correct 14 ms 17904 KB Output is correct
6 Correct 16 ms 18008 KB Output is correct
7 Correct 10 ms 17288 KB Output is correct
8 Correct 10 ms 17292 KB Output is correct
9 Correct 10 ms 17308 KB Output is correct
10 Correct 14 ms 18004 KB Output is correct
11 Correct 15 ms 17952 KB Output is correct
12 Correct 17 ms 18004 KB Output is correct
13 Correct 10 ms 17364 KB Output is correct
14 Correct 11 ms 17364 KB Output is correct
15 Correct 9 ms 17292 KB Output is correct
16 Correct 1242 ms 115064 KB Output is correct
17 Correct 1219 ms 117604 KB Output is correct
18 Correct 1232 ms 116536 KB Output is correct
19 Correct 1008 ms 125076 KB Output is correct
20 Correct 949 ms 117708 KB Output is correct
21 Correct 990 ms 120116 KB Output is correct
22 Correct 175 ms 66120 KB Output is correct
23 Correct 173 ms 65224 KB Output is correct
24 Correct 213 ms 65320 KB Output is correct
25 Correct 988 ms 122040 KB Output is correct
26 Correct 989 ms 120652 KB Output is correct
27 Correct 1010 ms 123580 KB Output is correct
28 Correct 177 ms 65404 KB Output is correct
29 Correct 195 ms 65464 KB Output is correct
30 Correct 206 ms 65856 KB Output is correct
31 Correct 1442 ms 525732 KB Output is correct
32 Correct 1497 ms 528896 KB Output is correct
33 Correct 1544 ms 530180 KB Output is correct
34 Correct 1429 ms 506548 KB Output is correct
35 Correct 1449 ms 508216 KB Output is correct
36 Correct 1436 ms 505252 KB Output is correct
37 Correct 1391 ms 504044 KB Output is correct
38 Correct 1492 ms 525916 KB Output is correct
39 Correct 1648 ms 505500 KB Output is correct
40 Correct 1588 ms 529144 KB Output is correct
41 Correct 1842 ms 507032 KB Output is correct
42 Correct 1618 ms 502124 KB Output is correct
43 Correct 1694 ms 526184 KB Output is correct
44 Correct 1663 ms 502144 KB Output is correct
45 Correct 1510 ms 507260 KB Output is correct
46 Correct 1362 ms 125028 KB Output is correct
47 Correct 1440 ms 130940 KB Output is correct
48 Correct 1295 ms 114448 KB Output is correct
49 Correct 1249 ms 125360 KB Output is correct
50 Correct 1183 ms 121608 KB Output is correct
51 Correct 1258 ms 122968 KB Output is correct
52 Correct 199 ms 65536 KB Output is correct
53 Correct 222 ms 66684 KB Output is correct
54 Correct 226 ms 65668 KB Output is correct
55 Correct 1149 ms 138644 KB Output is correct
56 Correct 1047 ms 119684 KB Output is correct
57 Correct 1089 ms 123588 KB Output is correct
58 Correct 199 ms 65352 KB Output is correct
59 Correct 195 ms 66616 KB Output is correct
60 Correct 215 ms 65944 KB Output is correct
61 Correct 1369 ms 123412 KB Output is correct
62 Correct 1359 ms 126760 KB Output is correct
63 Correct 1294 ms 120644 KB Output is correct
64 Correct 1106 ms 126916 KB Output is correct
65 Correct 1195 ms 126240 KB Output is correct
66 Correct 1079 ms 123236 KB Output is correct
67 Correct 199 ms 65548 KB Output is correct
68 Correct 193 ms 66112 KB Output is correct
69 Correct 211 ms 66232 KB Output is correct
70 Correct 1107 ms 117084 KB Output is correct
71 Correct 1081 ms 120800 KB Output is correct
72 Correct 1075 ms 132084 KB Output is correct
73 Correct 192 ms 66288 KB Output is correct
74 Correct 207 ms 65652 KB Output is correct
75 Correct 187 ms 65716 KB Output is correct
76 Correct 8 ms 16308 KB Output is correct
77 Correct 7 ms 16364 KB Output is correct
78 Correct 7 ms 16340 KB Output is correct
79 Correct 1351 ms 115728 KB Output is correct
80 Correct 1395 ms 130444 KB Output is correct
81 Correct 1412 ms 119604 KB Output is correct
82 Correct 1153 ms 120432 KB Output is correct
83 Correct 1141 ms 122224 KB Output is correct
84 Correct 1172 ms 130336 KB Output is correct
85 Correct 196 ms 66628 KB Output is correct
86 Correct 210 ms 66272 KB Output is correct
87 Correct 206 ms 65384 KB Output is correct
88 Correct 1059 ms 135404 KB Output is correct
89 Runtime error 514 ms 1048576 KB Execution killed with signal 9
90 Halted 0 ms 0 KB -