Submission #645671

# Submission time Handle Problem Language Result Execution time Memory
645671 2022-09-27T15:51:33 Z rainboy Reconstruction Project (JOI22_reconstruction) C
100 / 100
1480 ms 84476 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	500
#define M	100000
#define Q	1000000
#define INF	0x3f3f3f3f

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

int ii[M], jj[M], ww[M], xx[Q + 1]; long long ans[Q];

void sort(int *hh, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, h = hh[l + rand_() % (r - l)], tmp;

		while (j < k) {
			if (ww[hh[j]] == ww[h])
				j++;
			else if (ww[hh[j]] < ww[h]) {
				tmp = hh[i], hh[i] = hh[j], hh[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = hh[j], hh[j] = hh[k], hh[k] = tmp;
			}
		}
		sort(hh, l, i);
		l = k;
	}
}

int ds[N], uu[M], vv[M];

int find(int i) {
	return ds[i] < 0 ? i : find(ds[i]);
}

int cnt;

int join(int i, int j) {
	i = find(i);
	j = find(j);
	uu[cnt] = i, vv[cnt] = j, cnt++;
	if (i == j)
		return 0;
	if (ds[i] > ds[j])
		ds[i] = j;
	else {
		if (ds[i] == ds[j])
			ds[i]--;
		ds[j] = i;
	}
	return 1;
}

void undo() {
	int i = uu[cnt - 1], j = vv[cnt - 1];

	if (ds[i] == j)
		ds[i] = -1;
	if (ds[j] == i)
		ds[j] = -1;
	cnt--;
}

long long solve_(int *hh, int m, char *used, int x) {
	int h, hl, hr, cnt_;
	long long w;

	for (h = 0; h < m; h++)
		used[hh[h]] = 0;
	hr = 0;
	while (hr < m && ww[hh[hr]] < x)
		hr++;
	hl = hr - 1, w = 0, cnt_ = cnt;
	while (hl >= 0 || hr < m) {
		int hl_ = hl < 0 ? -1 : hh[hl], hr_ = hr >= m ? -1 : hh[hr];
		int dl = hl < 0 ? INF : x - ww[hl_], dr = hr >= m ? INF : ww[hr_] - x;

		if (dl <= dr) {
			if (join(ii[hl_], jj[hl_]))
				used[hl_] = 1, w += dl;
			hl--;
		} else {
			if (join(ii[hr_], jj[hr_]))
				used[hr_] = 1, w += dr;
			hr++;
		}
	}
	while (cnt > cnt_)
		undo();
	return w;
}

char used1[M], used2[M];

void solve(int *hh, int m, int l, int r, long long a, long long b) {
	int *hh_, m_, h, h_, g, cnt_;

	if (r - l == 1) {
		ans[l] = a * xx[l] + b + solve_(hh, m, used1, xx[l]);
		return;
	}
	solve_(hh, m, used1, xx[l]), solve_(hh, m, used2, xx[r]);
	cnt_ = cnt;
	m_ = 0;
	for (h = 0; h < m; h++) {
		h_ = hh[h];
		if (xx[l] <= ww[h_] && ww[h_] <= xx[r] || used1[h_] != used2[h_])
			m_++;
		else if (used1[h_] && used2[h_]) {
			join(ii[h_], jj[h_]);
			if (ww[h_] < xx[l])
				a++, b -= ww[h_];
			else
				a--, b += ww[h_];
		}
	}
	hh_ = (int *) malloc(m_ * sizeof *hh_), m_ = 0;
	for (h = 0; h < m; h++) {
		h_ = hh[h];
		if (xx[l] <= ww[h_] && ww[h_] <= xx[r] || used1[h_] != used2[h_])
			hh_[m_++] = h_;
	}
	g = (l + r) / 2;
	solve(hh_, m_, l, g, a, b), solve(hh_, m_, g, r, a, b);
	while (cnt > cnt_)
		undo();
}

int main() {
	static int hh[M];
	int n, m, q, g, h;

	scanf("%d%d", &n, &m);
	for (h = 0; h < m; h++)
		scanf("%d%d%d", &ii[h], &jj[h], &ww[h]);
	scanf("%d", &q);
	for (g = 0; g < q; g++)
		scanf("%d", &xx[g]);
	xx[q] = INF;
	for (h = 0; h < m; h++)
		hh[h] = h;
	sort(hh, 0, m);
	memset(ds, -1, n * sizeof *ds);
	solve(hh, m, 0, q, 0, 0);
	for (g = 0; g < q; g++)
		printf("%lld\n", ans[g]);
	return 0;
}

Compilation message

reconstruction.c: In function 'solve':
reconstruction.c:115:23: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  115 |   if (xx[l] <= ww[h_] && ww[h_] <= xx[r] || used1[h_] != used2[h_])
      |       ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
reconstruction.c:128:23: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  128 |   if (xx[l] <= ww[h_] && ww[h_] <= xx[r] || used1[h_] != used2[h_])
      |       ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
reconstruction.c: In function 'main':
reconstruction.c:141:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
reconstruction.c:143:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  143 |   scanf("%d%d%d", &ii[h], &jj[h], &ww[h]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
reconstruction.c:144:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  144 |  scanf("%d", &q);
      |  ^~~~~~~~~~~~~~~
reconstruction.c:146:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  146 |   scanf("%d", &xx[g]);
      |   ^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 488 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 288 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 488 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 288 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 109 ms 5832 KB Output is correct
21 Correct 106 ms 5896 KB Output is correct
22 Correct 110 ms 5796 KB Output is correct
23 Correct 105 ms 5852 KB Output is correct
24 Correct 117 ms 6064 KB Output is correct
25 Correct 107 ms 5848 KB Output is correct
26 Correct 111 ms 5784 KB Output is correct
27 Correct 112 ms 5836 KB Output is correct
28 Correct 92 ms 5736 KB Output is correct
29 Correct 75 ms 5708 KB Output is correct
30 Correct 111 ms 5744 KB Output is correct
31 Correct 120 ms 5576 KB Output is correct
32 Correct 81 ms 5124 KB Output is correct
33 Correct 106 ms 5988 KB Output is correct
34 Correct 80 ms 6220 KB Output is correct
35 Correct 98 ms 5348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 292 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1424 ms 79292 KB Output is correct
5 Correct 1413 ms 79300 KB Output is correct
6 Correct 1421 ms 79484 KB Output is correct
7 Correct 1314 ms 79464 KB Output is correct
8 Correct 1140 ms 77904 KB Output is correct
9 Correct 596 ms 75724 KB Output is correct
10 Correct 515 ms 72344 KB Output is correct
11 Correct 456 ms 74328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 488 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 288 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 288 KB Output is correct
20 Correct 339 ms 65300 KB Output is correct
21 Correct 289 ms 65404 KB Output is correct
22 Correct 312 ms 65380 KB Output is correct
23 Correct 284 ms 65424 KB Output is correct
24 Correct 287 ms 65356 KB Output is correct
25 Correct 298 ms 65316 KB Output is correct
26 Correct 279 ms 65324 KB Output is correct
27 Correct 326 ms 65476 KB Output is correct
28 Correct 276 ms 65344 KB Output is correct
29 Correct 292 ms 65396 KB Output is correct
30 Correct 277 ms 65440 KB Output is correct
31 Correct 292 ms 65212 KB Output is correct
32 Correct 293 ms 65876 KB Output is correct
33 Correct 281 ms 65296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 488 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 288 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 109 ms 5832 KB Output is correct
21 Correct 106 ms 5896 KB Output is correct
22 Correct 110 ms 5796 KB Output is correct
23 Correct 105 ms 5852 KB Output is correct
24 Correct 117 ms 6064 KB Output is correct
25 Correct 107 ms 5848 KB Output is correct
26 Correct 111 ms 5784 KB Output is correct
27 Correct 112 ms 5836 KB Output is correct
28 Correct 92 ms 5736 KB Output is correct
29 Correct 75 ms 5708 KB Output is correct
30 Correct 111 ms 5744 KB Output is correct
31 Correct 120 ms 5576 KB Output is correct
32 Correct 81 ms 5124 KB Output is correct
33 Correct 106 ms 5988 KB Output is correct
34 Correct 80 ms 6220 KB Output is correct
35 Correct 98 ms 5348 KB Output is correct
36 Correct 648 ms 16128 KB Output is correct
37 Correct 616 ms 15460 KB Output is correct
38 Correct 631 ms 15396 KB Output is correct
39 Correct 636 ms 15604 KB Output is correct
40 Correct 648 ms 15760 KB Output is correct
41 Correct 638 ms 16176 KB Output is correct
42 Correct 664 ms 16064 KB Output is correct
43 Correct 658 ms 16076 KB Output is correct
44 Correct 572 ms 16684 KB Output is correct
45 Correct 261 ms 12016 KB Output is correct
46 Correct 656 ms 16112 KB Output is correct
47 Correct 657 ms 16112 KB Output is correct
48 Correct 397 ms 11160 KB Output is correct
49 Correct 517 ms 14124 KB Output is correct
50 Correct 230 ms 11832 KB Output is correct
51 Correct 184 ms 8748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 488 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 288 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 109 ms 5832 KB Output is correct
21 Correct 106 ms 5896 KB Output is correct
22 Correct 110 ms 5796 KB Output is correct
23 Correct 105 ms 5852 KB Output is correct
24 Correct 117 ms 6064 KB Output is correct
25 Correct 107 ms 5848 KB Output is correct
26 Correct 111 ms 5784 KB Output is correct
27 Correct 112 ms 5836 KB Output is correct
28 Correct 92 ms 5736 KB Output is correct
29 Correct 75 ms 5708 KB Output is correct
30 Correct 111 ms 5744 KB Output is correct
31 Correct 120 ms 5576 KB Output is correct
32 Correct 81 ms 5124 KB Output is correct
33 Correct 106 ms 5988 KB Output is correct
34 Correct 80 ms 6220 KB Output is correct
35 Correct 98 ms 5348 KB Output is correct
36 Correct 1 ms 292 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1424 ms 79292 KB Output is correct
40 Correct 1413 ms 79300 KB Output is correct
41 Correct 1421 ms 79484 KB Output is correct
42 Correct 1314 ms 79464 KB Output is correct
43 Correct 1140 ms 77904 KB Output is correct
44 Correct 596 ms 75724 KB Output is correct
45 Correct 515 ms 72344 KB Output is correct
46 Correct 456 ms 74328 KB Output is correct
47 Correct 1 ms 288 KB Output is correct
48 Correct 339 ms 65300 KB Output is correct
49 Correct 289 ms 65404 KB Output is correct
50 Correct 312 ms 65380 KB Output is correct
51 Correct 284 ms 65424 KB Output is correct
52 Correct 287 ms 65356 KB Output is correct
53 Correct 298 ms 65316 KB Output is correct
54 Correct 279 ms 65324 KB Output is correct
55 Correct 326 ms 65476 KB Output is correct
56 Correct 276 ms 65344 KB Output is correct
57 Correct 292 ms 65396 KB Output is correct
58 Correct 277 ms 65440 KB Output is correct
59 Correct 292 ms 65212 KB Output is correct
60 Correct 293 ms 65876 KB Output is correct
61 Correct 281 ms 65296 KB Output is correct
62 Correct 648 ms 16128 KB Output is correct
63 Correct 616 ms 15460 KB Output is correct
64 Correct 631 ms 15396 KB Output is correct
65 Correct 636 ms 15604 KB Output is correct
66 Correct 648 ms 15760 KB Output is correct
67 Correct 638 ms 16176 KB Output is correct
68 Correct 664 ms 16064 KB Output is correct
69 Correct 658 ms 16076 KB Output is correct
70 Correct 572 ms 16684 KB Output is correct
71 Correct 261 ms 12016 KB Output is correct
72 Correct 656 ms 16112 KB Output is correct
73 Correct 657 ms 16112 KB Output is correct
74 Correct 397 ms 11160 KB Output is correct
75 Correct 517 ms 14124 KB Output is correct
76 Correct 230 ms 11832 KB Output is correct
77 Correct 184 ms 8748 KB Output is correct
78 Correct 1451 ms 78908 KB Output is correct
79 Correct 1412 ms 80188 KB Output is correct
80 Correct 1440 ms 79196 KB Output is correct
81 Correct 1384 ms 79500 KB Output is correct
82 Correct 1398 ms 78808 KB Output is correct
83 Correct 1439 ms 78952 KB Output is correct
84 Correct 1443 ms 78996 KB Output is correct
85 Correct 1480 ms 78964 KB Output is correct
86 Correct 1136 ms 84476 KB Output is correct
87 Correct 649 ms 77276 KB Output is correct
88 Correct 1473 ms 79296 KB Output is correct
89 Correct 1437 ms 79180 KB Output is correct
90 Correct 884 ms 73096 KB Output is correct
91 Correct 1060 ms 77004 KB Output is correct
92 Correct 589 ms 78020 KB Output is correct
93 Correct 555 ms 72264 KB Output is correct