답안 #695084

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
695084 2023-02-04T17:26:46 Z rainboy Sushi (JOI16_sushi) C
100 / 100
4820 ms 11756 KB
#include <stdio.h>
#include <string.h>

#define N	400000
#define K	633	/* K = ceil(sqrt(N)) */
#define M	((N + K - 1) / K)

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

int aa[N], aa_[N], n; char upd[N];
int iq[M][K + 1], pq[N], cnt[M], h1;

int lt(int i, int j) { return aa[i] > aa[j]; }

int p2(int p) {
	return (p *= 2) > cnt[h1] ? 0 : (p < cnt[h1] && lt(iq[h1][p + 1], iq[h1][p]) ? p + 1 : p);
}

void pq_up(int i) {
	int p, q, j;

	for (p = pq[i]; (q = p / 2) && lt(i, j = iq[h1][q]); p = q)
		iq[h1][pq[j] = p] = j;
	iq[h1][pq[i] = p] = i;
}

void pq_dn(int i) {
	int p, q, j;

	for (p = pq[i]; (q = p2(p)) && lt(j = iq[h1][q], i); p = q)
		iq[h1][pq[j] = p] = j;
	iq[h1][pq[i] = p] = i;
}

int pq_first() {
	return iq[h1][1];
}

void pq_add(int i) {
	pq[i] = ++cnt[h1], pq_up(i);
}

int pq_remove_first() {
	int i = iq[h1][1], j = iq[h1][cnt[h1]--];

	if (j != i)
		pq[j] = 1, pq_dn(j);
	pq[i] = 0;
	return i;
}

void build(int h) {
	int l = h * K, r = min((h + 1) * K, n) - 1, i, p;

	h1 = h;
	memset(pq + l, 0, (r - l + 1) * sizeof *pq), cnt[h1] = 0;
	for (i = l; i <= r; i++)
		iq[h1][pq[i] = ++cnt[h1]] = i;
	for (p = cnt[h1] / 2; p > 0; p--)
		pq_dn(iq[h1][p]);
}

void refresh(int h) {
	int l = h * K, r = min((h + 1) * K, n) - 1, i;

	h1 = h;
	memset(pq + l, 0, (r - l + 1) * sizeof *pq), cnt[h1] = 0;
	for (i = l; i <= r; i++)
		if (upd[i])
			aa[i] = -aa[i], pq_add(i);
	for (i = l; i <= r; i++) {
		if (!upd[i])
			aa[i] = -aa[i], pq_add(i);
		aa_[i] = -aa[pq_remove_first()];
	}
	memcpy(aa + l, aa_ + l, (r - l + 1) * sizeof *aa);
	memset(upd + l, 0, (r - l + 1) * sizeof *upd);
	build(h);
}

int walk(int h, int l, int r, int a) {
	int i, tmp;

	for (i = l; i <= r; i++)
		if (a < aa[i])
			tmp = a, a = aa[i], aa[i] = tmp;
	build(h);
	return a;
}

int run(int h, int a) {
	int i, tmp;

	h1 = h, i = pq_first();
	if (a < aa[i])
		tmp = a, a = aa[i], aa[i] = tmp, upd[i] = 1, pq_dn(i);
	return a;
}

int main() {
	int q, h, i;

	scanf("%d%d", &n, &q);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	for (h = 0; h * K < n; h++)
		build(h);
	while (q--) {
		int s, t, hs, ht, a;

		scanf("%d%d%d", &s, &t, &a), s--, t--, hs = s / K, ht = t / K;
		if (s <= t) {
			if (hs == ht) {
				refresh(hs);
				a = walk(hs, s, t, a);
			} else {
				refresh(hs), refresh(ht);
				a = walk(hs, s, (hs + 1) * K - 1, a);
				for (h = hs + 1; h < ht; h++)
					a = run(h, a);
				a = walk(ht, ht * K, t, a);
			}
		} else {
			if (hs == ht)
				refresh(hs);
			else
				refresh(hs), refresh(ht);
			a = walk(hs, s, min((hs + 1) * K, n) - 1, a);
			for (h = hs + 1; h * K < n; h++)
				a = run(h, a);
			for (h = 0; h < ht; h++)
				a = run(h, a);
			a = walk(ht, ht * K, t, a);
		}
		printf("%d\n", a);
	}
	return 0;
}

Compilation message

sushi.c: In function 'main':
sushi.c:103:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |  scanf("%d%d", &n, &q);
      |  ^~~~~~~~~~~~~~~~~~~~~
sushi.c:105:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
sushi.c:111:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |   scanf("%d%d%d", &s, &t, &a), s--, t--, hs = s / K, ht = t / K;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 352 KB Output is correct
2 Correct 98 ms 340 KB Output is correct
3 Correct 95 ms 352 KB Output is correct
4 Correct 95 ms 340 KB Output is correct
5 Correct 96 ms 344 KB Output is correct
6 Correct 97 ms 340 KB Output is correct
7 Correct 79 ms 340 KB Output is correct
8 Correct 80 ms 340 KB Output is correct
9 Correct 96 ms 348 KB Output is correct
10 Correct 94 ms 340 KB Output is correct
11 Correct 100 ms 352 KB Output is correct
12 Correct 101 ms 352 KB Output is correct
13 Correct 102 ms 340 KB Output is correct
14 Correct 75 ms 348 KB Output is correct
15 Correct 81 ms 352 KB Output is correct
16 Correct 53 ms 344 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3352 ms 5532 KB Output is correct
2 Correct 3321 ms 5536 KB Output is correct
3 Correct 1069 ms 5152 KB Output is correct
4 Correct 3354 ms 5580 KB Output is correct
5 Correct 3757 ms 5680 KB Output is correct
6 Correct 4166 ms 5744 KB Output is correct
7 Correct 4077 ms 5588 KB Output is correct
8 Correct 4096 ms 5588 KB Output is correct
9 Correct 957 ms 5108 KB Output is correct
10 Correct 3686 ms 5536 KB Output is correct
11 Correct 997 ms 4976 KB Output is correct
12 Correct 3991 ms 5584 KB Output is correct
13 Correct 3747 ms 5596 KB Output is correct
14 Correct 3587 ms 5652 KB Output is correct
15 Correct 1179 ms 5056 KB Output is correct
16 Correct 3614 ms 5596 KB Output is correct
17 Correct 4330 ms 5532 KB Output is correct
18 Correct 4820 ms 5560 KB Output is correct
19 Correct 4503 ms 5652 KB Output is correct
20 Correct 4702 ms 5704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 352 KB Output is correct
2 Correct 98 ms 340 KB Output is correct
3 Correct 95 ms 352 KB Output is correct
4 Correct 95 ms 340 KB Output is correct
5 Correct 96 ms 344 KB Output is correct
6 Correct 97 ms 340 KB Output is correct
7 Correct 79 ms 340 KB Output is correct
8 Correct 80 ms 340 KB Output is correct
9 Correct 96 ms 348 KB Output is correct
10 Correct 94 ms 340 KB Output is correct
11 Correct 100 ms 352 KB Output is correct
12 Correct 101 ms 352 KB Output is correct
13 Correct 102 ms 340 KB Output is correct
14 Correct 75 ms 348 KB Output is correct
15 Correct 81 ms 352 KB Output is correct
16 Correct 53 ms 344 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 3352 ms 5532 KB Output is correct
24 Correct 3321 ms 5536 KB Output is correct
25 Correct 1069 ms 5152 KB Output is correct
26 Correct 3354 ms 5580 KB Output is correct
27 Correct 3757 ms 5680 KB Output is correct
28 Correct 4166 ms 5744 KB Output is correct
29 Correct 4077 ms 5588 KB Output is correct
30 Correct 4096 ms 5588 KB Output is correct
31 Correct 957 ms 5108 KB Output is correct
32 Correct 3686 ms 5536 KB Output is correct
33 Correct 997 ms 4976 KB Output is correct
34 Correct 3991 ms 5584 KB Output is correct
35 Correct 3747 ms 5596 KB Output is correct
36 Correct 3587 ms 5652 KB Output is correct
37 Correct 1179 ms 5056 KB Output is correct
38 Correct 3614 ms 5596 KB Output is correct
39 Correct 4330 ms 5532 KB Output is correct
40 Correct 4820 ms 5560 KB Output is correct
41 Correct 4503 ms 5652 KB Output is correct
42 Correct 4702 ms 5704 KB Output is correct
43 Correct 1941 ms 7168 KB Output is correct
44 Correct 2039 ms 11504 KB Output is correct
45 Correct 1522 ms 8076 KB Output is correct
46 Correct 1988 ms 11656 KB Output is correct
47 Correct 1963 ms 11644 KB Output is correct
48 Correct 4197 ms 11604 KB Output is correct
49 Correct 4487 ms 11588 KB Output is correct
50 Correct 4176 ms 11660 KB Output is correct
51 Correct 4327 ms 11668 KB Output is correct
52 Correct 1379 ms 11524 KB Output is correct
53 Correct 1430 ms 11592 KB Output is correct
54 Correct 3661 ms 11580 KB Output is correct
55 Correct 3606 ms 11700 KB Output is correct
56 Correct 3771 ms 11756 KB Output is correct
57 Correct 3692 ms 11628 KB Output is correct
58 Correct 4027 ms 11548 KB Output is correct
59 Correct 4072 ms 11644 KB Output is correct
60 Correct 4373 ms 10004 KB Output is correct
61 Correct 4576 ms 9992 KB Output is correct
62 Correct 4796 ms 9988 KB Output is correct
63 Correct 4582 ms 10044 KB Output is correct
64 Correct 1158 ms 8132 KB Output is correct
65 Correct 2328 ms 8648 KB Output is correct
66 Correct 2230 ms 8580 KB Output is correct
67 Correct 2394 ms 11564 KB Output is correct
68 Correct 2486 ms 11644 KB Output is correct
69 Correct 2836 ms 11548 KB Output is correct
70 Correct 2658 ms 11576 KB Output is correct