Submission #683922

# Submission time Handle Problem Language Result Execution time Memory
683922 2023-01-19T16:32:46 Z rainboy Long Mansion (JOI17_long_mansion) C
100 / 100
460 ms 92592 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	500000
#define N_	(1 << 19)	/* N_ = pow2(ceil(log2(N + 1))) */

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

int *ea[N], eo[N], *ei[N], *ej[N], eo_[N];

void append(int **ej, int *eo, int i, int j) {
	int o = eo[i]++;

	if (o >= 2 && (o & o - 1) == 0)
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
	ej[i][o] = j;
}

int st[N_ * 2], n_;

void update(int i, int x) {
	for (i += n_; i > 0; i >>= 1)
		st[i] += x;
}

int floor_(int r) {
	int l;

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

int ceil_(int l) {
	int r;

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

int main() {
	static int aa[N], pp[N], ll[N], rr[N], qu[N], ll_[N], rr_[N];
	int n, q, i, j, o, cnt;

	scanf("%d", &n);
	for (i = 0; i + 1 < n; i++)
		scanf("%d", &aa[i]), aa[i]--;
	for (i = 0; i < n; i++) {
		scanf("%d", &eo[i]);
		ea[i] = (int *) malloc(eo[i] * sizeof *ea[i]);
		for (o = eo[i]; o--; )
			scanf("%d", &ea[i][o]), ea[i][o]--;
	}
	for (i = 0; i < n; i++)
		pp[i] = -1;
	for (i = 0; i < n; i++) {
		for (o = eo[i]; o--; ) {
			int a = ea[i][o];

			pp[a] = i;
		}
		ll[i] = i + 1 == n ? -1 : pp[aa[i]];
	}
	for (i = 0; i < n; i++)
		pp[i] = n;
	for (i = n - 1; i >= 0; i--) {
		for (o = eo[i]; o--; ) {
			int a = ea[i][o];

			pp[a] = i;
		}
		rr[i] = i == 0 ? n : pp[aa[i - 1]];
	}
	n_ = 1;
	while (n_ <= n)
		n_ <<= 1;
	for (j = 0; j < n; j++)
		ei[j] = (int *) malloc(2 * sizeof *ei[j]), eo_[j] = 0;
	for (i = 0; i < n; i++)
		if (rr[i] < n)
			append(ei, eo_, rr[i], i);
	memset(st, 0, n_ * 2 * sizeof *st);
	cnt = 0;
	for (j = 0; j < n; j++) {
		update(j, 1);
		for (o = eo_[j]; o--; ) {
			i = ei[j][o];
			update(i, -1);
		}
		qu[cnt++] = j;
		i = ceil_(ll[j] + 1);
		if (i != -1)
			while (cnt && qu[cnt - 1] >= i)
				rr_[qu[--cnt]] = j;
	}
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]), eo_[i] = 0;
	for (j = 0; j < n; j++)
		if (ll[j] >= 0)
			append(ej, eo_, ll[j], j);
	memset(st, 0, n_ * 2 * sizeof *st);
	cnt = 0;
	for (i = n - 1; i >= 0; i--) {
		update(i, 1);
		for (o = eo_[i]; o--; ) {
			j = ej[i][o];
			update(j, -1);
		}
		qu[cnt++] = i;
		j = floor_(rr[i] - 1);
		if (j != -1)
			while (cnt && qu[cnt - 1] <= j)
				ll_[qu[--cnt]] = i;
	}
	scanf("%d", &q);
	while (q--) {
		int i, j;

		scanf("%d%d", &i, &j), i--, j--;
		printf(ll_[i] <= j && j <= rr_[i] ? "YES\n" : "NO\n");
	}
	return 0;
}

Compilation message

long_mansion.c: In function 'append':
long_mansion.c:16:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   16 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
long_mansion.c: In function 'main':
long_mansion.c:62:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
long_mansion.c:64:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |   scanf("%d", &aa[i]), aa[i]--;
      |   ^~~~~~~~~~~~~~~~~~~
long_mansion.c:66:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |   scanf("%d", &eo[i]);
      |   ^~~~~~~~~~~~~~~~~~~
long_mansion.c:69:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |    scanf("%d", &ea[i][o]), ea[i][o]--;
      |    ^~~~~~~~~~~~~~~~~~~~~~
long_mansion.c:132:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  132 |  scanf("%d", &q);
      |  ^~~~~~~~~~~~~~~
long_mansion.c:136:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  136 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 724 KB Output is correct
2 Correct 4 ms 832 KB Output is correct
3 Correct 5 ms 1236 KB Output is correct
4 Correct 3 ms 660 KB Output is correct
5 Correct 4 ms 724 KB Output is correct
6 Correct 3 ms 724 KB Output is correct
7 Correct 3 ms 676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 724 KB Output is correct
2 Correct 4 ms 832 KB Output is correct
3 Correct 5 ms 1236 KB Output is correct
4 Correct 3 ms 660 KB Output is correct
5 Correct 4 ms 724 KB Output is correct
6 Correct 3 ms 724 KB Output is correct
7 Correct 3 ms 676 KB Output is correct
8 Correct 121 ms 6592 KB Output is correct
9 Correct 135 ms 6440 KB Output is correct
10 Correct 115 ms 7036 KB Output is correct
11 Correct 115 ms 7628 KB Output is correct
12 Correct 112 ms 6060 KB Output is correct
13 Correct 111 ms 6692 KB Output is correct
14 Correct 116 ms 6740 KB Output is correct
15 Correct 111 ms 6788 KB Output is correct
16 Correct 106 ms 6476 KB Output is correct
17 Correct 123 ms 6760 KB Output is correct
18 Correct 117 ms 6908 KB Output is correct
19 Correct 134 ms 6812 KB Output is correct
20 Correct 106 ms 6724 KB Output is correct
21 Correct 112 ms 6548 KB Output is correct
22 Correct 127 ms 6556 KB Output is correct
23 Correct 111 ms 6540 KB Output is correct
24 Correct 112 ms 6588 KB Output is correct
25 Correct 129 ms 6536 KB Output is correct
26 Correct 114 ms 6476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 208 ms 25340 KB Output is correct
2 Correct 233 ms 25220 KB Output is correct
3 Correct 203 ms 25216 KB Output is correct
4 Correct 209 ms 25256 KB Output is correct
5 Correct 207 ms 25220 KB Output is correct
6 Correct 215 ms 24596 KB Output is correct
7 Correct 177 ms 24580 KB Output is correct
8 Correct 175 ms 24608 KB Output is correct
9 Correct 186 ms 24536 KB Output is correct
10 Correct 172 ms 24608 KB Output is correct
11 Correct 173 ms 24700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 724 KB Output is correct
2 Correct 4 ms 832 KB Output is correct
3 Correct 5 ms 1236 KB Output is correct
4 Correct 3 ms 660 KB Output is correct
5 Correct 4 ms 724 KB Output is correct
6 Correct 3 ms 724 KB Output is correct
7 Correct 3 ms 676 KB Output is correct
8 Correct 121 ms 6592 KB Output is correct
9 Correct 135 ms 6440 KB Output is correct
10 Correct 115 ms 7036 KB Output is correct
11 Correct 115 ms 7628 KB Output is correct
12 Correct 112 ms 6060 KB Output is correct
13 Correct 111 ms 6692 KB Output is correct
14 Correct 116 ms 6740 KB Output is correct
15 Correct 111 ms 6788 KB Output is correct
16 Correct 106 ms 6476 KB Output is correct
17 Correct 123 ms 6760 KB Output is correct
18 Correct 117 ms 6908 KB Output is correct
19 Correct 134 ms 6812 KB Output is correct
20 Correct 106 ms 6724 KB Output is correct
21 Correct 112 ms 6548 KB Output is correct
22 Correct 127 ms 6556 KB Output is correct
23 Correct 111 ms 6540 KB Output is correct
24 Correct 112 ms 6588 KB Output is correct
25 Correct 129 ms 6536 KB Output is correct
26 Correct 114 ms 6476 KB Output is correct
27 Correct 208 ms 25340 KB Output is correct
28 Correct 233 ms 25220 KB Output is correct
29 Correct 203 ms 25216 KB Output is correct
30 Correct 209 ms 25256 KB Output is correct
31 Correct 207 ms 25220 KB Output is correct
32 Correct 215 ms 24596 KB Output is correct
33 Correct 177 ms 24580 KB Output is correct
34 Correct 175 ms 24608 KB Output is correct
35 Correct 186 ms 24536 KB Output is correct
36 Correct 172 ms 24608 KB Output is correct
37 Correct 173 ms 24700 KB Output is correct
38 Correct 382 ms 77832 KB Output is correct
39 Correct 460 ms 92592 KB Output is correct
40 Correct 338 ms 61632 KB Output is correct
41 Correct 442 ms 91316 KB Output is correct
42 Correct 189 ms 25396 KB Output is correct
43 Correct 239 ms 25420 KB Output is correct
44 Correct 279 ms 43832 KB Output is correct
45 Correct 271 ms 44072 KB Output is correct
46 Correct 291 ms 44224 KB Output is correct
47 Correct 209 ms 25644 KB Output is correct
48 Correct 188 ms 25500 KB Output is correct
49 Correct 310 ms 43964 KB Output is correct
50 Correct 289 ms 44248 KB Output is correct
51 Correct 277 ms 44724 KB Output is correct
52 Correct 273 ms 42988 KB Output is correct
53 Correct 333 ms 61708 KB Output is correct
54 Correct 425 ms 78096 KB Output is correct
55 Correct 315 ms 61664 KB Output is correct