답안 #645728

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
645728 2022-09-27T18:05:55 Z rainboy Fish 2 (JOI22_fish2) C
60 / 100
4000 ms 4200 KB
#include <stdio.h>
#include <string.h>

#define N	100000
#define N_	(1 << 17)	/* N_ = pow2(ceil(log2(N + 1))) */
#define INF	0x7fffffff
#define LG	30

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

int aa[N], st1[N_ * 2], st2[N_ * 2], kk2[N_ * 2], lz2[N_], h_, n_;

void pul1(int i) {
	st1[i] = st1[i << 1 | 0] <= INF - st1[i << 1 | 1] ? st1[i << 1 | 0] + st1[i << 1 | 1] : INF;
}

void build(int *aa, int n) {
	int i;

	h_ = 0;
	while (1 << h_ <= n)
		h_++;
	n_ = 1 << h_;
	for (i = 0; i < n; i++)
		st1[n_ + i] = aa[i], kk2[n_ + i] = 1;
	for (i = n_ - 1; i > 0; i--)
		pul1(i);
}

void update1(int i, int a) {
	i += n_;
	st1[i] = a;
	while (i > 1)
		pul1(i >>= 1);
}

int queryl1(int r, int s) {
	int l = 0;

	for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1)
		if ((r & 1) == 0) {
			if (s > st1[r])
				s -= st1[r--];
			else {
				while (r < n_)
					if (s <= st1[r << 1 | 1])
						r = r << 1 | 1;
					else
						s -= st1[r << 1 | 1], r = r << 1 | 0;
				return r - n_;
			}
		}
	return -1;
}

int queryr1(int l, int s) {
	int r = n_ - 1;

	for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1)
		if ((l & 1) == 1) {
			if (s > st1[l])
				s -= st1[l++];
			else {
				while (l < n_)
					if (s <= st1[l << 1 | 0])
						l = l << 1 | 0;
					else
						s -= st1[l << 1 | 0], l = l << 1 | 1;
				return l - n_;
			}
		}
	return n_;
}

void put2(int i, int x) {
	st2[i] += x;
	if (i < n_)
		lz2[i] += x;
}

void pus2(int i) {
	if (lz2[i])
		put2(i << 1 | 0, lz2[i]), put2(i << 1 | 1, lz2[i]), lz2[i] = 0;
}

void pul2(int i) {
	if (!lz2[i]) {
		int l = i << 1, r = l | 1;

		if (st2[l] < st2[r])
			st2[i] = st2[l], kk2[i] = kk2[l];
		else if (st2[l] > st2[r])
			st2[i] = st2[r], kk2[i] = kk2[r];
		else
			st2[i] = st2[l], kk2[i] = kk2[l] + kk2[r];
	}
}

void push2(int i) {
	int h;

	for (h = h_; h > 0; h--)
		pus2(i >> h);
}

void pull2(int i) {
	while (i > 1)
		pul2(i >>= 1);
}

void update2(int l, int r, int x) {
	int l_ = l += n_, r_ = r += n_;

	push2(l_), push2(r_);
	for ( ; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1)
			put2(l++, x);
		if ((r & 1) == 0)
			put2(r--, x);
	}
	pull2(l_), pull2(r_);
}

int query2(int l, int r) {
	int x = INF, k = 0;

	push2(l += n_), push2(r += n_);
	for ( ; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1) {
			if (x > st2[l])
				x = st2[l], k = 0;
			if (x == st2[l])
				k += kk2[l];
			l++;
		}
		if ((r & 1) == 0) {
			if (x > st2[r])
				x = st2[r], k = 0;
			if (x == st2[r])
				k += kk2[r];
			r--;
		}
	}
	return x == 0 ? k : 0;
}

void upd(int l, int r, int i, int x) {
	int j;

	j = max(queryl1(i - 1, aa[i]), l - 1);
	if (j < l || aa[j] > aa[i])
		update2(j + 1, i - 1, x);
	j = min(queryr1(i + 1, aa[i]), r + 1);
	if (j > r || aa[j] >= aa[i])
		update2(i + 1, j - 1, x);
}

int main() {
	int n, q, i, j;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	build(aa, n);
	scanf("%d", &q);
	for (i = 0; i < n; i++)
		upd(0, n - 1, i, 1);
	while (q--) {
		static char used[N];
		static int qu[N];
		int t, a, l, r, lg, cnt, h;

		scanf("%d", &t);
		if (t == 1) {
			scanf("%d%d", &i, &a), i--;
			cnt = 0;
			qu[cnt++] = i;
			for (lg = 0; lg <= LG; lg++) {
				j = queryl1(i - 1, 1 << lg);
				if (j >= 0 && !used[j])
					used[j] = 1, qu[cnt++] = j;
				j = queryr1(i + 1, 1 << lg);
				if (j < n && !used[j])
					used[j] = 1, qu[cnt++] = j;
			}
			for (h = 0; h < cnt; h++)
				upd(0, n - 1, qu[h], -1);
			aa[i] = a, update1(i, a);
			for (h = 0; h < cnt; h++)
				upd(0, n - 1, qu[h], 1);
			for (h = 0; h < cnt; h++)
				used[qu[h]] = 0;
		} else {
			scanf("%d%d", &l, &r), l--, r--;
			if (l == 0 && r == n - 1) {
				printf("%d\n", query2(l, r));
				continue;
			}
			cnt = 0;
			for (lg = 0; lg <= LG; lg++) {
				j = queryl1(l - 1, 1 << lg);
				if (j >= 0 && !used[j])
					used[j] = 1, qu[cnt++] = j;
				j = queryr1(l, 1 << lg);
				if (j < n && !used[j])
					used[j] = 1, qu[cnt++] = j;
				j = queryl1(r, 1 << lg);
				if (j >= 0 && !used[j])
					used[j] = 1, qu[cnt++] = j;
				j = queryr1(r + 1, 1 << lg);
				if (j < n && !used[j])
					used[j] = 1, qu[cnt++] = j;
			}
			for (h = 0; h < cnt; h++) {
				upd(0, n - 1, qu[h], -1);
				if (l <= qu[h] && qu[h] <= r)
					upd(l, r, qu[h], 1);
			}
			printf("%d\n", query2(l, r));
			for (h = 0; h < cnt; h++) {
				if (l <= qu[h] && qu[h] <= r)
					upd(l, r, qu[h], -1);
				upd(0, n - 1, qu[h], 1);
			}
			for (h = 0; h < cnt; h++)
				used[qu[h]] = 0;
		}
	}
	return 0;
}

Compilation message

fish2.c: In function 'main':
fish2.c:162:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  162 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
fish2.c:164:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  164 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
fish2.c:166:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  166 |  scanf("%d", &q);
      |  ^~~~~~~~~~~~~~~
fish2.c:174:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  174 |   scanf("%d", &t);
      |   ^~~~~~~~~~~~~~~
fish2.c:176:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  176 |    scanf("%d%d", &i, &a), i--;
      |    ^~~~~~~~~~~~~~~~~~~~~
fish2.c:195:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  195 |    scanf("%d%d", &l, &r), l--, r--;
      |    ^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 10 ms 336 KB Output is correct
6 Correct 15 ms 212 KB Output is correct
7 Correct 11 ms 332 KB Output is correct
8 Correct 21 ms 340 KB Output is correct
9 Correct 19 ms 212 KB Output is correct
10 Correct 3 ms 212 KB Output is correct
11 Correct 4 ms 340 KB Output is correct
12 Correct 5 ms 336 KB Output is correct
13 Correct 11 ms 212 KB Output is correct
14 Correct 4 ms 336 KB Output is correct
15 Correct 8 ms 332 KB Output is correct
16 Correct 9 ms 212 KB Output is correct
17 Correct 6 ms 340 KB Output is correct
18 Correct 14 ms 332 KB Output is correct
19 Correct 4 ms 212 KB Output is correct
20 Correct 6 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 63 ms 3444 KB Output is correct
3 Correct 65 ms 3496 KB Output is correct
4 Correct 65 ms 3516 KB Output is correct
5 Correct 68 ms 3668 KB Output is correct
6 Correct 50 ms 3404 KB Output is correct
7 Correct 46 ms 3568 KB Output is correct
8 Correct 49 ms 3360 KB Output is correct
9 Correct 45 ms 3508 KB Output is correct
10 Correct 53 ms 3516 KB Output is correct
11 Correct 55 ms 3476 KB Output is correct
12 Correct 45 ms 3500 KB Output is correct
13 Correct 48 ms 3504 KB Output is correct
14 Correct 55 ms 3464 KB Output is correct
15 Correct 54 ms 3480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 10 ms 336 KB Output is correct
6 Correct 15 ms 212 KB Output is correct
7 Correct 11 ms 332 KB Output is correct
8 Correct 21 ms 340 KB Output is correct
9 Correct 19 ms 212 KB Output is correct
10 Correct 3 ms 212 KB Output is correct
11 Correct 4 ms 340 KB Output is correct
12 Correct 5 ms 336 KB Output is correct
13 Correct 11 ms 212 KB Output is correct
14 Correct 4 ms 336 KB Output is correct
15 Correct 8 ms 332 KB Output is correct
16 Correct 9 ms 212 KB Output is correct
17 Correct 6 ms 340 KB Output is correct
18 Correct 14 ms 332 KB Output is correct
19 Correct 4 ms 212 KB Output is correct
20 Correct 6 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 63 ms 3444 KB Output is correct
23 Correct 65 ms 3496 KB Output is correct
24 Correct 65 ms 3516 KB Output is correct
25 Correct 68 ms 3668 KB Output is correct
26 Correct 50 ms 3404 KB Output is correct
27 Correct 46 ms 3568 KB Output is correct
28 Correct 49 ms 3360 KB Output is correct
29 Correct 45 ms 3508 KB Output is correct
30 Correct 53 ms 3516 KB Output is correct
31 Correct 55 ms 3476 KB Output is correct
32 Correct 45 ms 3500 KB Output is correct
33 Correct 48 ms 3504 KB Output is correct
34 Correct 55 ms 3464 KB Output is correct
35 Correct 54 ms 3480 KB Output is correct
36 Correct 105 ms 3660 KB Output is correct
37 Correct 139 ms 3692 KB Output is correct
38 Correct 135 ms 3788 KB Output is correct
39 Correct 95 ms 3536 KB Output is correct
40 Correct 126 ms 3660 KB Output is correct
41 Correct 59 ms 3496 KB Output is correct
42 Correct 60 ms 3576 KB Output is correct
43 Correct 106 ms 3680 KB Output is correct
44 Correct 121 ms 3660 KB Output is correct
45 Correct 106 ms 3556 KB Output is correct
46 Correct 86 ms 3676 KB Output is correct
47 Correct 111 ms 3784 KB Output is correct
48 Correct 88 ms 3624 KB Output is correct
49 Correct 120 ms 3684 KB Output is correct
50 Correct 67 ms 3552 KB Output is correct
51 Correct 72 ms 3584 KB Output is correct
52 Correct 74 ms 3508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 63 ms 3444 KB Output is correct
3 Correct 65 ms 3496 KB Output is correct
4 Correct 65 ms 3516 KB Output is correct
5 Correct 68 ms 3668 KB Output is correct
6 Correct 50 ms 3404 KB Output is correct
7 Correct 46 ms 3568 KB Output is correct
8 Correct 49 ms 3360 KB Output is correct
9 Correct 45 ms 3508 KB Output is correct
10 Correct 53 ms 3516 KB Output is correct
11 Correct 55 ms 3476 KB Output is correct
12 Correct 45 ms 3500 KB Output is correct
13 Correct 48 ms 3504 KB Output is correct
14 Correct 55 ms 3464 KB Output is correct
15 Correct 54 ms 3480 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Execution timed out 4078 ms 3900 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 63 ms 3444 KB Output is correct
3 Correct 65 ms 3496 KB Output is correct
4 Correct 65 ms 3516 KB Output is correct
5 Correct 68 ms 3668 KB Output is correct
6 Correct 50 ms 3404 KB Output is correct
7 Correct 46 ms 3568 KB Output is correct
8 Correct 49 ms 3360 KB Output is correct
9 Correct 45 ms 3508 KB Output is correct
10 Correct 53 ms 3516 KB Output is correct
11 Correct 55 ms 3476 KB Output is correct
12 Correct 45 ms 3500 KB Output is correct
13 Correct 48 ms 3504 KB Output is correct
14 Correct 55 ms 3464 KB Output is correct
15 Correct 54 ms 3480 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 3393 ms 4012 KB Output is correct
18 Correct 1040 ms 3860 KB Output is correct
19 Correct 2500 ms 3996 KB Output is correct
20 Correct 837 ms 4200 KB Output is correct
21 Correct 3039 ms 4012 KB Output is correct
22 Correct 1058 ms 3916 KB Output is correct
23 Correct 2974 ms 4000 KB Output is correct
24 Correct 1014 ms 3892 KB Output is correct
25 Correct 2761 ms 3940 KB Output is correct
26 Correct 368 ms 3792 KB Output is correct
27 Correct 496 ms 3520 KB Output is correct
28 Correct 727 ms 3892 KB Output is correct
29 Correct 416 ms 3752 KB Output is correct
30 Correct 494 ms 3724 KB Output is correct
31 Correct 827 ms 3848 KB Output is correct
32 Correct 995 ms 3852 KB Output is correct
33 Correct 1214 ms 3716 KB Output is correct
34 Correct 875 ms 3788 KB Output is correct
35 Correct 1046 ms 3808 KB Output is correct
36 Correct 858 ms 3840 KB Output is correct
37 Correct 931 ms 3788 KB Output is correct
38 Correct 633 ms 4072 KB Output is correct
39 Correct 478 ms 3792 KB Output is correct
40 Correct 242 ms 4124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 10 ms 336 KB Output is correct
6 Correct 15 ms 212 KB Output is correct
7 Correct 11 ms 332 KB Output is correct
8 Correct 21 ms 340 KB Output is correct
9 Correct 19 ms 212 KB Output is correct
10 Correct 3 ms 212 KB Output is correct
11 Correct 4 ms 340 KB Output is correct
12 Correct 5 ms 336 KB Output is correct
13 Correct 11 ms 212 KB Output is correct
14 Correct 4 ms 336 KB Output is correct
15 Correct 8 ms 332 KB Output is correct
16 Correct 9 ms 212 KB Output is correct
17 Correct 6 ms 340 KB Output is correct
18 Correct 14 ms 332 KB Output is correct
19 Correct 4 ms 212 KB Output is correct
20 Correct 6 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 63 ms 3444 KB Output is correct
23 Correct 65 ms 3496 KB Output is correct
24 Correct 65 ms 3516 KB Output is correct
25 Correct 68 ms 3668 KB Output is correct
26 Correct 50 ms 3404 KB Output is correct
27 Correct 46 ms 3568 KB Output is correct
28 Correct 49 ms 3360 KB Output is correct
29 Correct 45 ms 3508 KB Output is correct
30 Correct 53 ms 3516 KB Output is correct
31 Correct 55 ms 3476 KB Output is correct
32 Correct 45 ms 3500 KB Output is correct
33 Correct 48 ms 3504 KB Output is correct
34 Correct 55 ms 3464 KB Output is correct
35 Correct 54 ms 3480 KB Output is correct
36 Correct 105 ms 3660 KB Output is correct
37 Correct 139 ms 3692 KB Output is correct
38 Correct 135 ms 3788 KB Output is correct
39 Correct 95 ms 3536 KB Output is correct
40 Correct 126 ms 3660 KB Output is correct
41 Correct 59 ms 3496 KB Output is correct
42 Correct 60 ms 3576 KB Output is correct
43 Correct 106 ms 3680 KB Output is correct
44 Correct 121 ms 3660 KB Output is correct
45 Correct 106 ms 3556 KB Output is correct
46 Correct 86 ms 3676 KB Output is correct
47 Correct 111 ms 3784 KB Output is correct
48 Correct 88 ms 3624 KB Output is correct
49 Correct 120 ms 3684 KB Output is correct
50 Correct 67 ms 3552 KB Output is correct
51 Correct 72 ms 3584 KB Output is correct
52 Correct 74 ms 3508 KB Output is correct
53 Correct 0 ms 212 KB Output is correct
54 Execution timed out 4078 ms 3900 KB Time limit exceeded
55 Halted 0 ms 0 KB -