Submission #745259

# Submission time Handle Problem Language Result Execution time Memory
745259 2023-05-19T16:21:20 Z rainboy RMQ (NOI17_rmq) C
100 / 100
38 ms 3484 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 ");
				printf("\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 ");
						printf("\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 ", aa[i]);
	printf("\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 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 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 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 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 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 38 ms 3340 KB Output is correct
28 Correct 31 ms 3276 KB Output is correct
29 Correct 25 ms 3012 KB Output is correct
30 Correct 33 ms 3484 KB Output is correct
31 Correct 27 ms 2832 KB Output is correct
32 Correct 32 ms 3032 KB Output is correct
33 Correct 12 ms 2132 KB Output is correct
34 Correct 5 ms 1492 KB Output is correct
35 Correct 10 ms 2464 KB Output is correct
36 Correct 26 ms 2012 KB Output is correct
37 Correct 34 ms 2404 KB Output is correct
38 Correct 2 ms 980 KB Output is correct
39 Correct 23 ms 2668 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 0 ms 212 KB Output is correct