Submission #745257

# Submission time Handle Problem Language Result Execution time Memory
745257 2023-05-19T16:20:14 Z rainboy RMQ (NOI17_rmq) C
100 / 100
36 ms 4788 KB
#include <stdio.h>
#include <string.h>

#define N	100000
#define Q	100000

int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }

unsigned int X = 12345;

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

int n;

int ll[Q], rr[Q], bb[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 (bb[hh[j]] == bb[h])
				j++;
			else if (bb[hh[j]] > bb[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], jj[N]; char marked[N];

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

void join(int i, int j) {
	i = find(i);
	j = find(j);
	if (i == j)
		return;
	if (ds[i] > ds[j])
		ds[i] = j;
	else {
		if (ds[i] == ds[j])
			ds[i]--;
		ds[j] = i, jj[i] = jj[j];
	}
}

int next(int i) {
	return !marked[i] ? i : jj[find(i)] + 1;
}

void mark(int i) {
	marked[i] = 1;
	if (i > 0 && marked[i - 1])
		join(i - 1, i);
	if (i + 1 < n && marked[i + 1])
		join(i, i + 1);
}

int main() {
	static int hh[Q], qu[N], aa[N];
	int q, cnt, h, h_, i, a, l, r;

	scanf("%d%d", &n, &q);
	for (h = 0; h < q; h++)
		scanf("%d%d%d", &ll[h], &rr[h], &bb[h]);
	for (h = 0; h < q; h++)
		hh[h] = h;
	sort(hh, 0, q);
	memset(ds, -1, n * sizeof *ds);
	for (i = 0; i < n; i++)
		jj[i] = i;
	cnt = 0;
	for (a = n - 1, h = 0; a >= 0; a--) {
		h_ = h, l = 0, r = n - 1;
		while (h_ < q && bb[hh[h_]] == a)
			l = max(l, ll[hh[h_]]), r = min(r, rr[hh[h_]]), h_++;
		qu[cnt++] = a;
		if (h < h_) {
			if ((i = next(l)) > r) {
				for (i = 0; i < n; i++)
					printf("-1%c", i + 1 < n ? ' ' : '\n');
				return 0;
			}
			aa[i] = qu[--cnt], mark(i);
			while (h < h_) {
				l = ll[hh[h]], r = rr[hh[h]];
				while ((i = next(l)) <= r) {
					if (cnt == 0) {
						for (i = 0; i < n; i++)
							printf("-1%c", i + 1 < n ? ' ' : '\n');
						return 0;
					}
					aa[i] = qu[--cnt], mark(i);
				}
				h++;
			}
		}
	}
	while ((i = next(0)) < n)
		aa[i] = qu[--cnt], mark(i);
	for (i = 0; i < n; i++)
		printf("%d%c", aa[i], i + 1 < n ? ' ' : '\n');
	return 0;
}

Compilation message

rmq.c: In function 'main':
rmq.c:75:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |  scanf("%d%d", &n, &q);
      |  ^~~~~~~~~~~~~~~~~~~~~
rmq.c:77:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |   scanf("%d%d%d", &ll[h], &rr[h], &bb[h]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 292 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 292 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 292 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 292 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 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 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 300 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 292 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 292 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 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 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 300 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 296 KB Output is correct
27 Correct 36 ms 4656 KB Output is correct
28 Correct 34 ms 4420 KB Output is correct
29 Correct 26 ms 3828 KB Output is correct
30 Correct 36 ms 4788 KB Output is correct
31 Correct 32 ms 3884 KB Output is correct
32 Correct 32 ms 4204 KB Output is correct
33 Correct 9 ms 2120 KB Output is correct
34 Correct 6 ms 1580 KB Output is correct
35 Correct 13 ms 2576 KB Output is correct
36 Correct 25 ms 2960 KB Output is correct
37 Correct 35 ms 3756 KB Output is correct
38 Correct 4 ms 980 KB Output is correct
39 Correct 27 ms 3668 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct