답안 #831978

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
831978 2023-08-20T19:02:19 Z NK_ Grapevine (NOI22_grapevine) C++17
100 / 100
2053 ms 212332 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);
 
// 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;
		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
 
 
const int nax = 1e5 + 5;
 
// MAIN
vpi adj[nax]; int W[nax];
 
// CENTROID
vi chd[nax];
int sub[nax], proc[nax], siz[nax];
 
// CENTROID SUBTREES
vi eid[nax], st[nax], en[nax], par[nax]; 
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;
void dfs(int u, int p = -1) {
	par[u].pb(C); st[u].pb(t++);
	for(auto& e : adj[u]) {
		int v, i; tie(v, i) = e;
		if (v != p && !proc[v]) {
			eid[v].pb(i);
			dfs(v, u);
		}
	}
	en[u].pb(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].pb(-1); dfs(c);
	proc[c] = 1;
 
	siz[c] = sub[u];
	assert(t <= 2 * siz[c]);
	T[c].init(2 * siz[c]);
 
	for(auto& e : adj[c]) {
		int v, i; tie(v, i) = e;
		if (!proc[v]) {
			int cv = init(v);
			chd[c].pb(cv);
		}
	}
 
	return c;
}
// END OF CENTROID
 
int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	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++) {
		reverse(begin(par[u]), end(par[u]));
		reverse(begin(eid[u]), end(eid[u]));
		reverse(begin(st[u]), end(st[u]));
		reverse(begin(en[u]), end(en[u]));
	}
 
	for(int u = 0; u < N; u++) {
		int M = sz(par[u]);
		for(int p = 0; p < M; p++) {
			int e = eid[u][p], x = par[u][p];
			if (e != -1) {
				// cout << x << " " << u << endl;
				// cout << st[u][p] << " " << en[u][p] << " " << W[e] << endl;
				T[x].upd(st[u][p], en[u][p], W[e]);
			}
		}
	}
 
	// cout << endl;
	// cout << endl;
 
	for(int q = 0; q < Q; q++) {
		int type; cin >> type;
 
		if (type == 1) {
			int u; cin >> u; --u;
			
			ll ans = 2 * INFL;
 
			int M = sz(par[u]);
			for(int p = 0; p < M; p++) {
				int x = par[u][p], pos = st[u][p];
 
				ll dst = T[x].query(pos, pos);
				if (dst >= INFL) dst -= INFL;
 
				// cout << x << " TO " << u << ": " << dst << endl;
				// cout << x << " TO CLOSEST: " << T[x].query() << endl;
 
				ans = min(ans, dst + T[x].query());
			}
			cout << (ans >= INFL ? -1 : ans) << nl;
		}
 
		if (type == 2) {
			int u; cin >> u; --u;
			int M = sz(par[u]);
			for(int p = 0; p < M; p++) {
				int x = par[u][p];
				int pos = st[u][p];
 
				if (T[x].query(pos, pos) >= INFL) T[x].upd(pos, pos, -INFL);
				else T[x].upd(pos, pos, +INFL);
				// cout << u << " TO " << x << ": " << T[x].query(pos, pos) << endl;
			}
		}
 
		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++) {
				int M = sz(par[u]);
				for(int p = 0; p < M; p++) {
					int x = par[u][p], e = eid[u][p];
					int l = st[u][p], r = en[u][p];
 
					if (e == i) T[x].upd(l, r, w - W[i]);
				}
 
				swap(u, v);
			}
 
			W[i] = w;
		}
 
	}
 
 
 
    return 0;
} 

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:157:6: warning: unused variable 'R' [-Wunused-variable]
  157 |  int R = init();
      |      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 23536 KB Output is correct
2 Correct 18 ms 23484 KB Output is correct
3 Correct 18 ms 23564 KB Output is correct
4 Correct 19 ms 23936 KB Output is correct
5 Correct 19 ms 23892 KB Output is correct
6 Correct 18 ms 23860 KB Output is correct
7 Correct 13 ms 22356 KB Output is correct
8 Correct 13 ms 22356 KB Output is correct
9 Correct 15 ms 22356 KB Output is correct
10 Correct 18 ms 24020 KB Output is correct
11 Correct 19 ms 23784 KB Output is correct
12 Correct 18 ms 24056 KB Output is correct
13 Correct 12 ms 22376 KB Output is correct
14 Correct 13 ms 22256 KB Output is correct
15 Correct 12 ms 22376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1585 ms 174500 KB Output is correct
2 Correct 1617 ms 174732 KB Output is correct
3 Correct 1554 ms 168984 KB Output is correct
4 Correct 1265 ms 211568 KB Output is correct
5 Correct 1177 ms 196888 KB Output is correct
6 Correct 1228 ms 200736 KB Output is correct
7 Correct 205 ms 67592 KB Output is correct
8 Correct 204 ms 66784 KB Output is correct
9 Correct 217 ms 66912 KB Output is correct
10 Correct 1218 ms 205396 KB Output is correct
11 Correct 1196 ms 201548 KB Output is correct
12 Correct 1283 ms 211588 KB Output is correct
13 Correct 201 ms 66916 KB Output is correct
14 Correct 211 ms 67024 KB Output is correct
15 Correct 218 ms 67436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1302 ms 172356 KB Output is correct
2 Correct 1313 ms 174108 KB Output is correct
3 Correct 1471 ms 174268 KB Output is correct
4 Correct 1327 ms 167392 KB Output is correct
5 Correct 1427 ms 168292 KB Output is correct
6 Correct 1299 ms 166852 KB Output is correct
7 Correct 1235 ms 166620 KB Output is correct
8 Correct 1310 ms 173012 KB Output is correct
9 Correct 1249 ms 167264 KB Output is correct
10 Correct 1248 ms 174252 KB Output is correct
11 Correct 1372 ms 167712 KB Output is correct
12 Correct 1253 ms 165912 KB Output is correct
13 Correct 1240 ms 172896 KB Output is correct
14 Correct 1290 ms 165936 KB Output is correct
15 Correct 1377 ms 167696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1837 ms 172212 KB Output is correct
2 Correct 1878 ms 173172 KB Output is correct
3 Correct 1827 ms 169816 KB Output is correct
4 Correct 1528 ms 211592 KB Output is correct
5 Correct 1446 ms 205660 KB Output is correct
6 Correct 1505 ms 207180 KB Output is correct
7 Correct 282 ms 67072 KB Output is correct
8 Correct 233 ms 67976 KB Output is correct
9 Correct 269 ms 67192 KB Output is correct
10 Correct 1444 ms 207000 KB Output is correct
11 Correct 1432 ms 200176 KB Output is correct
12 Correct 1464 ms 207052 KB Output is correct
13 Correct 266 ms 66872 KB Output is correct
14 Correct 245 ms 68100 KB Output is correct
15 Correct 311 ms 67332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2009 ms 173412 KB Output is correct
2 Correct 1853 ms 176900 KB Output is correct
3 Correct 1947 ms 172216 KB Output is correct
4 Correct 1535 ms 197960 KB Output is correct
5 Correct 1451 ms 212332 KB Output is correct
6 Correct 1445 ms 203628 KB Output is correct
7 Correct 305 ms 67136 KB Output is correct
8 Correct 267 ms 67640 KB Output is correct
9 Correct 227 ms 67768 KB Output is correct
10 Correct 1417 ms 195428 KB Output is correct
11 Correct 1337 ms 200672 KB Output is correct
12 Correct 1739 ms 193504 KB Output is correct
13 Correct 264 ms 67752 KB Output is correct
14 Correct 278 ms 67324 KB Output is correct
15 Correct 289 ms 67272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 23536 KB Output is correct
2 Correct 18 ms 23484 KB Output is correct
3 Correct 18 ms 23564 KB Output is correct
4 Correct 19 ms 23936 KB Output is correct
5 Correct 19 ms 23892 KB Output is correct
6 Correct 18 ms 23860 KB Output is correct
7 Correct 13 ms 22356 KB Output is correct
8 Correct 13 ms 22356 KB Output is correct
9 Correct 15 ms 22356 KB Output is correct
10 Correct 18 ms 24020 KB Output is correct
11 Correct 19 ms 23784 KB Output is correct
12 Correct 18 ms 24056 KB Output is correct
13 Correct 12 ms 22376 KB Output is correct
14 Correct 13 ms 22256 KB Output is correct
15 Correct 12 ms 22376 KB Output is correct
16 Correct 1585 ms 174500 KB Output is correct
17 Correct 1617 ms 174732 KB Output is correct
18 Correct 1554 ms 168984 KB Output is correct
19 Correct 1265 ms 211568 KB Output is correct
20 Correct 1177 ms 196888 KB Output is correct
21 Correct 1228 ms 200736 KB Output is correct
22 Correct 205 ms 67592 KB Output is correct
23 Correct 204 ms 66784 KB Output is correct
24 Correct 217 ms 66912 KB Output is correct
25 Correct 1218 ms 205396 KB Output is correct
26 Correct 1196 ms 201548 KB Output is correct
27 Correct 1283 ms 211588 KB Output is correct
28 Correct 201 ms 66916 KB Output is correct
29 Correct 211 ms 67024 KB Output is correct
30 Correct 218 ms 67436 KB Output is correct
31 Correct 1302 ms 172356 KB Output is correct
32 Correct 1313 ms 174108 KB Output is correct
33 Correct 1471 ms 174268 KB Output is correct
34 Correct 1327 ms 167392 KB Output is correct
35 Correct 1427 ms 168292 KB Output is correct
36 Correct 1299 ms 166852 KB Output is correct
37 Correct 1235 ms 166620 KB Output is correct
38 Correct 1310 ms 173012 KB Output is correct
39 Correct 1249 ms 167264 KB Output is correct
40 Correct 1248 ms 174252 KB Output is correct
41 Correct 1372 ms 167712 KB Output is correct
42 Correct 1253 ms 165912 KB Output is correct
43 Correct 1240 ms 172896 KB Output is correct
44 Correct 1290 ms 165936 KB Output is correct
45 Correct 1377 ms 167696 KB Output is correct
46 Correct 1837 ms 172212 KB Output is correct
47 Correct 1878 ms 173172 KB Output is correct
48 Correct 1827 ms 169816 KB Output is correct
49 Correct 1528 ms 211592 KB Output is correct
50 Correct 1446 ms 205660 KB Output is correct
51 Correct 1505 ms 207180 KB Output is correct
52 Correct 282 ms 67072 KB Output is correct
53 Correct 233 ms 67976 KB Output is correct
54 Correct 269 ms 67192 KB Output is correct
55 Correct 1444 ms 207000 KB Output is correct
56 Correct 1432 ms 200176 KB Output is correct
57 Correct 1464 ms 207052 KB Output is correct
58 Correct 266 ms 66872 KB Output is correct
59 Correct 245 ms 68100 KB Output is correct
60 Correct 311 ms 67332 KB Output is correct
61 Correct 2009 ms 173412 KB Output is correct
62 Correct 1853 ms 176900 KB Output is correct
63 Correct 1947 ms 172216 KB Output is correct
64 Correct 1535 ms 197960 KB Output is correct
65 Correct 1451 ms 212332 KB Output is correct
66 Correct 1445 ms 203628 KB Output is correct
67 Correct 305 ms 67136 KB Output is correct
68 Correct 267 ms 67640 KB Output is correct
69 Correct 227 ms 67768 KB Output is correct
70 Correct 1417 ms 195428 KB Output is correct
71 Correct 1337 ms 200672 KB Output is correct
72 Correct 1739 ms 193504 KB Output is correct
73 Correct 264 ms 67752 KB Output is correct
74 Correct 278 ms 67324 KB Output is correct
75 Correct 289 ms 67272 KB Output is correct
76 Correct 10 ms 21460 KB Output is correct
77 Correct 9 ms 21468 KB Output is correct
78 Correct 10 ms 21444 KB Output is correct
79 Correct 1980 ms 170928 KB Output is correct
80 Correct 2053 ms 173996 KB Output is correct
81 Correct 1913 ms 173124 KB Output is correct
82 Correct 1447 ms 201124 KB Output is correct
83 Correct 1486 ms 201088 KB Output is correct
84 Correct 1621 ms 207724 KB Output is correct
85 Correct 304 ms 68196 KB Output is correct
86 Correct 242 ms 67940 KB Output is correct
87 Correct 260 ms 67120 KB Output is correct
88 Correct 1657 ms 200632 KB Output is correct
89 Correct 1335 ms 201688 KB Output is correct
90 Correct 1815 ms 199348 KB Output is correct
91 Correct 297 ms 69592 KB Output is correct
92 Correct 243 ms 68860 KB Output is correct
93 Correct 348 ms 68452 KB Output is correct