Submission #796273

# Submission time Handle Problem Language Result Execution time Memory
796273 2023-07-28T08:43:23 Z GEN 이지후(#10112) Security Guard (JOI23_guard) C++17
100 / 100
2039 ms 79096 KB
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pi = array<lint, 2>;
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
const int MAXN = 200005;

// case tree:
// root with maximum
// anchor in parent
// using the guard from root, can do everything

struct disj {
	int pa[MAXN], mx[MAXN];
	void init(int n, lint *a) {
		iota(pa, pa + n + 1, 0);
		for (int i = 0; i < n; i++)
			mx[i] = a[i];
	}
	int find(int x) { return pa[x] = (pa[x] == x ? x : find(pa[x])); }
	int query(int x) { return mx[find(x)]; }
	bool uni(int p, int q) {
		p = find(p);
		q = find(q);
		if (p == q)
			return 0;
		pa[q] = p;
		mx[p] = min(mx[p], mx[q]);
		return 1;
	}
} disj;

using node = array<lint, 3>;
priority_queue<pi, vector<pi>, greater<pi>> pq[MAXN];
priority_queue<node, vector<node>, greater<node>> global;

vector<pi> gph[MAXN];
lint a[MAXN];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n, m, q;
	cin >> n >> m >> q;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	vector<array<int, 3>> edges(m);
	for (auto &[x, s, e] : edges) {
		cin >> s >> e;
		s--;
		e--;
		x = a[s] + a[e];
	}
	sort(all(edges));
	disj.init(n, a);
	for (auto &[x, s, e] : edges) {
		if (disj.uni(s, e)) {
			pq[s].push({x, e});
			pq[e].push({x, s});
		}
	}
	for (int i = 0; i < n; i++) {
		global.push({pq[i].top()[0] - a[i], i, a[i]});
	}
	disj.init(n, a);
	int rt = min_element(a, a + n) - a;
	lint tot = 1ll * a[rt] * (n - 2) + *max_element(a, a + n);
	vector<lint> ans(q + 1, tot);
	for (int i = n - 2; i >= 0; i--) {
		int v = -1;
		while (sz(global)) {
			auto x = global.top();
			global.pop();
			if (disj.query(x[1]) != x[2])
				continue;
			v = disj.find(x[1]);
			while (sz(pq[v]) && disj.find(v) == disj.find(pq[v].top()[1])) {
				pq[v].pop();
			}
			assert(sz(pq[v]));
			if (pq[v].top()[0] - disj.query(v) != x[0]) {
				global.push({pq[v].top()[0] - disj.query(v), v, disj.query(v)});
				continue;
			}
			break;
		}
		assert(v != -1);
		auto tp = pq[v].top();
		assert(disj.find(tp[1]) != disj.find(v));
		pq[v].pop();
		tot += tp[0] - disj.query(v) - a[rt];
		if (i < sz(ans))
			ans[i] = tot;

		int w = tp[1];
		v = disj.find(v);
		w = disj.find(w);
		disj.uni(v, w); // pa[w] = v;
		if (sz(pq[w]) > sz(pq[v])) {
			swap(pq[w], pq[v]);
		}
		while (sz(pq[w])) {
			auto tp = pq[w].top();
			pq[w].pop();
			if (disj.find(tp[1]) == disj.find(v))
				continue;
			pq[v].push(tp);
		}
		if (sz(pq[v]))
			global.push({pq[v].top()[0] - disj.query(v), v, disj.query(v)});
	}
	for (auto &x : ans)
		cout << x << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 11220 KB Output is correct
2 Correct 101 ms 32328 KB Output is correct
3 Correct 106 ms 32332 KB Output is correct
4 Correct 173 ms 34748 KB Output is correct
5 Correct 175 ms 34704 KB Output is correct
6 Correct 172 ms 34736 KB Output is correct
7 Correct 177 ms 34796 KB Output is correct
8 Correct 5 ms 11168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 11220 KB Output is correct
2 Correct 101 ms 32328 KB Output is correct
3 Correct 106 ms 32332 KB Output is correct
4 Correct 173 ms 34748 KB Output is correct
5 Correct 175 ms 34704 KB Output is correct
6 Correct 172 ms 34736 KB Output is correct
7 Correct 177 ms 34796 KB Output is correct
8 Correct 5 ms 11168 KB Output is correct
9 Correct 5 ms 11220 KB Output is correct
10 Correct 384 ms 35844 KB Output is correct
11 Correct 108 ms 32432 KB Output is correct
12 Correct 144 ms 32348 KB Output is correct
13 Correct 135 ms 32444 KB Output is correct
14 Correct 395 ms 35764 KB Output is correct
15 Correct 394 ms 35772 KB Output is correct
16 Correct 403 ms 35840 KB Output is correct
17 Correct 416 ms 35860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 11220 KB Output is correct
2 Correct 101 ms 32328 KB Output is correct
3 Correct 106 ms 32332 KB Output is correct
4 Correct 173 ms 34748 KB Output is correct
5 Correct 175 ms 34704 KB Output is correct
6 Correct 172 ms 34736 KB Output is correct
7 Correct 177 ms 34796 KB Output is correct
8 Correct 5 ms 11168 KB Output is correct
9 Correct 5 ms 11220 KB Output is correct
10 Correct 384 ms 35844 KB Output is correct
11 Correct 108 ms 32432 KB Output is correct
12 Correct 144 ms 32348 KB Output is correct
13 Correct 135 ms 32444 KB Output is correct
14 Correct 395 ms 35764 KB Output is correct
15 Correct 394 ms 35772 KB Output is correct
16 Correct 403 ms 35840 KB Output is correct
17 Correct 416 ms 35860 KB Output is correct
18 Correct 5 ms 11220 KB Output is correct
19 Correct 418 ms 35088 KB Output is correct
20 Correct 394 ms 34876 KB Output is correct
21 Correct 403 ms 34352 KB Output is correct
22 Correct 424 ms 33728 KB Output is correct
23 Correct 313 ms 32844 KB Output is correct
24 Correct 295 ms 32868 KB Output is correct
25 Correct 221 ms 33068 KB Output is correct
26 Correct 192 ms 33860 KB Output is correct
27 Correct 143 ms 35396 KB Output is correct
28 Correct 434 ms 34972 KB Output is correct
29 Correct 427 ms 34344 KB Output is correct
30 Correct 407 ms 33884 KB Output is correct
31 Correct 146 ms 35400 KB Output is correct
32 Correct 1327 ms 33908 KB Output is correct
33 Correct 773 ms 35020 KB Output is correct
34 Correct 370 ms 58232 KB Output is correct
35 Correct 336 ms 73540 KB Output is correct
36 Correct 244 ms 60300 KB Output is correct
37 Correct 233 ms 55776 KB Output is correct
38 Correct 251 ms 34632 KB Output is correct
39 Correct 211 ms 34608 KB Output is correct
40 Correct 253 ms 34224 KB Output is correct
41 Correct 220 ms 32332 KB Output is correct
42 Correct 273 ms 32344 KB Output is correct
43 Correct 278 ms 33476 KB Output is correct
44 Correct 735 ms 33728 KB Output is correct
45 Correct 332 ms 33568 KB Output is correct
46 Correct 462 ms 33752 KB Output is correct
47 Correct 569 ms 33760 KB Output is correct
48 Correct 732 ms 33756 KB Output is correct
49 Correct 280 ms 35152 KB Output is correct
50 Correct 343 ms 33312 KB Output is correct
51 Correct 200 ms 33224 KB Output is correct
52 Correct 212 ms 33456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 11220 KB Output is correct
2 Correct 101 ms 32328 KB Output is correct
3 Correct 106 ms 32332 KB Output is correct
4 Correct 173 ms 34748 KB Output is correct
5 Correct 175 ms 34704 KB Output is correct
6 Correct 172 ms 34736 KB Output is correct
7 Correct 177 ms 34796 KB Output is correct
8 Correct 5 ms 11168 KB Output is correct
9 Correct 5 ms 11220 KB Output is correct
10 Correct 384 ms 35844 KB Output is correct
11 Correct 108 ms 32432 KB Output is correct
12 Correct 144 ms 32348 KB Output is correct
13 Correct 135 ms 32444 KB Output is correct
14 Correct 395 ms 35764 KB Output is correct
15 Correct 394 ms 35772 KB Output is correct
16 Correct 403 ms 35840 KB Output is correct
17 Correct 416 ms 35860 KB Output is correct
18 Correct 5 ms 11220 KB Output is correct
19 Correct 418 ms 35088 KB Output is correct
20 Correct 394 ms 34876 KB Output is correct
21 Correct 403 ms 34352 KB Output is correct
22 Correct 424 ms 33728 KB Output is correct
23 Correct 313 ms 32844 KB Output is correct
24 Correct 295 ms 32868 KB Output is correct
25 Correct 221 ms 33068 KB Output is correct
26 Correct 192 ms 33860 KB Output is correct
27 Correct 143 ms 35396 KB Output is correct
28 Correct 434 ms 34972 KB Output is correct
29 Correct 427 ms 34344 KB Output is correct
30 Correct 407 ms 33884 KB Output is correct
31 Correct 146 ms 35400 KB Output is correct
32 Correct 1327 ms 33908 KB Output is correct
33 Correct 773 ms 35020 KB Output is correct
34 Correct 370 ms 58232 KB Output is correct
35 Correct 336 ms 73540 KB Output is correct
36 Correct 244 ms 60300 KB Output is correct
37 Correct 233 ms 55776 KB Output is correct
38 Correct 251 ms 34632 KB Output is correct
39 Correct 211 ms 34608 KB Output is correct
40 Correct 253 ms 34224 KB Output is correct
41 Correct 220 ms 32332 KB Output is correct
42 Correct 273 ms 32344 KB Output is correct
43 Correct 278 ms 33476 KB Output is correct
44 Correct 735 ms 33728 KB Output is correct
45 Correct 332 ms 33568 KB Output is correct
46 Correct 462 ms 33752 KB Output is correct
47 Correct 569 ms 33760 KB Output is correct
48 Correct 732 ms 33756 KB Output is correct
49 Correct 280 ms 35152 KB Output is correct
50 Correct 343 ms 33312 KB Output is correct
51 Correct 200 ms 33224 KB Output is correct
52 Correct 212 ms 33456 KB Output is correct
53 Correct 5 ms 11220 KB Output is correct
54 Correct 450 ms 35152 KB Output is correct
55 Correct 442 ms 35028 KB Output is correct
56 Correct 425 ms 35100 KB Output is correct
57 Correct 477 ms 36088 KB Output is correct
58 Correct 384 ms 35188 KB Output is correct
59 Correct 318 ms 35172 KB Output is correct
60 Correct 253 ms 34224 KB Output is correct
61 Correct 251 ms 36212 KB Output is correct
62 Correct 199 ms 37624 KB Output is correct
63 Correct 449 ms 35148 KB Output is correct
64 Correct 466 ms 35584 KB Output is correct
65 Correct 491 ms 36468 KB Output is correct
66 Correct 194 ms 37932 KB Output is correct
67 Correct 770 ms 36208 KB Output is correct
68 Correct 784 ms 35016 KB Output is correct
69 Correct 387 ms 58752 KB Output is correct
70 Correct 338 ms 75956 KB Output is correct
71 Correct 238 ms 59388 KB Output is correct
72 Correct 233 ms 57680 KB Output is correct
73 Correct 247 ms 34624 KB Output is correct
74 Correct 226 ms 34616 KB Output is correct
75 Correct 260 ms 34348 KB Output is correct
76 Correct 235 ms 32328 KB Output is correct
77 Correct 262 ms 32328 KB Output is correct
78 Correct 284 ms 33520 KB Output is correct
79 Correct 843 ms 36120 KB Output is correct
80 Correct 365 ms 34188 KB Output is correct
81 Correct 535 ms 35516 KB Output is correct
82 Correct 647 ms 36068 KB Output is correct
83 Correct 742 ms 36060 KB Output is correct
84 Correct 326 ms 36088 KB Output is correct
85 Correct 341 ms 33208 KB Output is correct
86 Correct 206 ms 33224 KB Output is correct
87 Correct 186 ms 33220 KB Output is correct
88 Correct 600 ms 38356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 11220 KB Output is correct
2 Correct 5 ms 11204 KB Output is correct
3 Correct 5 ms 11220 KB Output is correct
4 Correct 5 ms 11220 KB Output is correct
5 Correct 5 ms 11220 KB Output is correct
6 Correct 6 ms 11196 KB Output is correct
7 Correct 5 ms 11220 KB Output is correct
8 Correct 18 ms 15060 KB Output is correct
9 Correct 19 ms 15060 KB Output is correct
10 Correct 19 ms 14804 KB Output is correct
11 Correct 18 ms 14972 KB Output is correct
12 Correct 18 ms 15080 KB Output is correct
13 Correct 23 ms 15052 KB Output is correct
14 Correct 19 ms 15072 KB Output is correct
15 Correct 19 ms 15060 KB Output is correct
16 Correct 18 ms 15060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 11220 KB Output is correct
2 Correct 5 ms 11204 KB Output is correct
3 Correct 5 ms 11220 KB Output is correct
4 Correct 5 ms 11220 KB Output is correct
5 Correct 5 ms 11220 KB Output is correct
6 Correct 6 ms 11196 KB Output is correct
7 Correct 5 ms 11220 KB Output is correct
8 Correct 18 ms 15060 KB Output is correct
9 Correct 19 ms 15060 KB Output is correct
10 Correct 19 ms 14804 KB Output is correct
11 Correct 18 ms 14972 KB Output is correct
12 Correct 18 ms 15080 KB Output is correct
13 Correct 23 ms 15052 KB Output is correct
14 Correct 19 ms 15072 KB Output is correct
15 Correct 19 ms 15060 KB Output is correct
16 Correct 18 ms 15060 KB Output is correct
17 Correct 29 ms 15716 KB Output is correct
18 Correct 27 ms 15720 KB Output is correct
19 Correct 46 ms 16368 KB Output is correct
20 Correct 58 ms 17480 KB Output is correct
21 Correct 67 ms 18008 KB Output is correct
22 Correct 58 ms 17468 KB Output is correct
23 Correct 106 ms 20100 KB Output is correct
24 Correct 71 ms 18232 KB Output is correct
25 Correct 24 ms 15444 KB Output is correct
26 Correct 54 ms 17112 KB Output is correct
27 Correct 27 ms 16136 KB Output is correct
28 Correct 21 ms 15444 KB Output is correct
29 Correct 22 ms 15428 KB Output is correct
30 Correct 21 ms 15520 KB Output is correct
31 Correct 19 ms 14164 KB Output is correct
32 Correct 20 ms 14260 KB Output is correct
33 Correct 24 ms 15424 KB Output is correct
34 Correct 23 ms 15444 KB Output is correct
35 Correct 42 ms 16580 KB Output is correct
36 Correct 106 ms 20100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 11220 KB Output is correct
2 Correct 101 ms 32328 KB Output is correct
3 Correct 106 ms 32332 KB Output is correct
4 Correct 173 ms 34748 KB Output is correct
5 Correct 175 ms 34704 KB Output is correct
6 Correct 172 ms 34736 KB Output is correct
7 Correct 177 ms 34796 KB Output is correct
8 Correct 5 ms 11168 KB Output is correct
9 Correct 5 ms 11220 KB Output is correct
10 Correct 384 ms 35844 KB Output is correct
11 Correct 108 ms 32432 KB Output is correct
12 Correct 144 ms 32348 KB Output is correct
13 Correct 135 ms 32444 KB Output is correct
14 Correct 395 ms 35764 KB Output is correct
15 Correct 394 ms 35772 KB Output is correct
16 Correct 403 ms 35840 KB Output is correct
17 Correct 416 ms 35860 KB Output is correct
18 Correct 5 ms 11220 KB Output is correct
19 Correct 418 ms 35088 KB Output is correct
20 Correct 394 ms 34876 KB Output is correct
21 Correct 403 ms 34352 KB Output is correct
22 Correct 424 ms 33728 KB Output is correct
23 Correct 313 ms 32844 KB Output is correct
24 Correct 295 ms 32868 KB Output is correct
25 Correct 221 ms 33068 KB Output is correct
26 Correct 192 ms 33860 KB Output is correct
27 Correct 143 ms 35396 KB Output is correct
28 Correct 434 ms 34972 KB Output is correct
29 Correct 427 ms 34344 KB Output is correct
30 Correct 407 ms 33884 KB Output is correct
31 Correct 146 ms 35400 KB Output is correct
32 Correct 1327 ms 33908 KB Output is correct
33 Correct 773 ms 35020 KB Output is correct
34 Correct 370 ms 58232 KB Output is correct
35 Correct 336 ms 73540 KB Output is correct
36 Correct 244 ms 60300 KB Output is correct
37 Correct 233 ms 55776 KB Output is correct
38 Correct 251 ms 34632 KB Output is correct
39 Correct 211 ms 34608 KB Output is correct
40 Correct 253 ms 34224 KB Output is correct
41 Correct 220 ms 32332 KB Output is correct
42 Correct 273 ms 32344 KB Output is correct
43 Correct 278 ms 33476 KB Output is correct
44 Correct 735 ms 33728 KB Output is correct
45 Correct 332 ms 33568 KB Output is correct
46 Correct 462 ms 33752 KB Output is correct
47 Correct 569 ms 33760 KB Output is correct
48 Correct 732 ms 33756 KB Output is correct
49 Correct 280 ms 35152 KB Output is correct
50 Correct 343 ms 33312 KB Output is correct
51 Correct 200 ms 33224 KB Output is correct
52 Correct 212 ms 33456 KB Output is correct
53 Correct 5 ms 11220 KB Output is correct
54 Correct 450 ms 35152 KB Output is correct
55 Correct 442 ms 35028 KB Output is correct
56 Correct 425 ms 35100 KB Output is correct
57 Correct 477 ms 36088 KB Output is correct
58 Correct 384 ms 35188 KB Output is correct
59 Correct 318 ms 35172 KB Output is correct
60 Correct 253 ms 34224 KB Output is correct
61 Correct 251 ms 36212 KB Output is correct
62 Correct 199 ms 37624 KB Output is correct
63 Correct 449 ms 35148 KB Output is correct
64 Correct 466 ms 35584 KB Output is correct
65 Correct 491 ms 36468 KB Output is correct
66 Correct 194 ms 37932 KB Output is correct
67 Correct 770 ms 36208 KB Output is correct
68 Correct 784 ms 35016 KB Output is correct
69 Correct 387 ms 58752 KB Output is correct
70 Correct 338 ms 75956 KB Output is correct
71 Correct 238 ms 59388 KB Output is correct
72 Correct 233 ms 57680 KB Output is correct
73 Correct 247 ms 34624 KB Output is correct
74 Correct 226 ms 34616 KB Output is correct
75 Correct 260 ms 34348 KB Output is correct
76 Correct 235 ms 32328 KB Output is correct
77 Correct 262 ms 32328 KB Output is correct
78 Correct 284 ms 33520 KB Output is correct
79 Correct 843 ms 36120 KB Output is correct
80 Correct 365 ms 34188 KB Output is correct
81 Correct 535 ms 35516 KB Output is correct
82 Correct 647 ms 36068 KB Output is correct
83 Correct 742 ms 36060 KB Output is correct
84 Correct 326 ms 36088 KB Output is correct
85 Correct 341 ms 33208 KB Output is correct
86 Correct 206 ms 33224 KB Output is correct
87 Correct 186 ms 33220 KB Output is correct
88 Correct 600 ms 38356 KB Output is correct
89 Correct 5 ms 11220 KB Output is correct
90 Correct 5 ms 11204 KB Output is correct
91 Correct 5 ms 11220 KB Output is correct
92 Correct 5 ms 11220 KB Output is correct
93 Correct 5 ms 11220 KB Output is correct
94 Correct 6 ms 11196 KB Output is correct
95 Correct 5 ms 11220 KB Output is correct
96 Correct 18 ms 15060 KB Output is correct
97 Correct 19 ms 15060 KB Output is correct
98 Correct 19 ms 14804 KB Output is correct
99 Correct 18 ms 14972 KB Output is correct
100 Correct 18 ms 15080 KB Output is correct
101 Correct 23 ms 15052 KB Output is correct
102 Correct 19 ms 15072 KB Output is correct
103 Correct 19 ms 15060 KB Output is correct
104 Correct 18 ms 15060 KB Output is correct
105 Correct 29 ms 15716 KB Output is correct
106 Correct 27 ms 15720 KB Output is correct
107 Correct 46 ms 16368 KB Output is correct
108 Correct 58 ms 17480 KB Output is correct
109 Correct 67 ms 18008 KB Output is correct
110 Correct 58 ms 17468 KB Output is correct
111 Correct 106 ms 20100 KB Output is correct
112 Correct 71 ms 18232 KB Output is correct
113 Correct 24 ms 15444 KB Output is correct
114 Correct 54 ms 17112 KB Output is correct
115 Correct 27 ms 16136 KB Output is correct
116 Correct 21 ms 15444 KB Output is correct
117 Correct 22 ms 15428 KB Output is correct
118 Correct 21 ms 15520 KB Output is correct
119 Correct 19 ms 14164 KB Output is correct
120 Correct 20 ms 14260 KB Output is correct
121 Correct 24 ms 15424 KB Output is correct
122 Correct 23 ms 15444 KB Output is correct
123 Correct 42 ms 16580 KB Output is correct
124 Correct 106 ms 20100 KB Output is correct
125 Correct 435 ms 39696 KB Output is correct
126 Correct 475 ms 39772 KB Output is correct
127 Correct 474 ms 39928 KB Output is correct
128 Correct 574 ms 41040 KB Output is correct
129 Correct 373 ms 37784 KB Output is correct
130 Correct 375 ms 38984 KB Output is correct
131 Correct 325 ms 38684 KB Output is correct
132 Correct 276 ms 39856 KB Output is correct
133 Correct 247 ms 39552 KB Output is correct
134 Correct 503 ms 40048 KB Output is correct
135 Correct 482 ms 39548 KB Output is correct
136 Correct 574 ms 40392 KB Output is correct
137 Correct 246 ms 42368 KB Output is correct
138 Correct 2039 ms 39004 KB Output is correct
139 Correct 795 ms 37476 KB Output is correct
140 Correct 429 ms 63984 KB Output is correct
141 Correct 378 ms 79096 KB Output is correct
142 Correct 278 ms 64352 KB Output is correct
143 Correct 257 ms 61960 KB Output is correct
144 Correct 305 ms 36964 KB Output is correct
145 Correct 259 ms 36468 KB Output is correct
146 Correct 307 ms 36860 KB Output is correct
147 Correct 279 ms 34816 KB Output is correct
148 Correct 277 ms 34884 KB Output is correct
149 Correct 325 ms 38368 KB Output is correct
150 Correct 898 ms 41056 KB Output is correct
151 Correct 402 ms 39516 KB Output is correct
152 Correct 559 ms 41264 KB Output is correct
153 Correct 679 ms 41296 KB Output is correct
154 Correct 735 ms 44016 KB Output is correct
155 Correct 358 ms 48572 KB Output is correct
156 Correct 378 ms 39340 KB Output is correct
157 Correct 216 ms 41524 KB Output is correct
158 Correct 244 ms 37444 KB Output is correct
159 Correct 718 ms 49240 KB Output is correct