Submission #125488

# Submission time Handle Problem Language Result Execution time Memory
125488 2019-07-05T13:18:03 Z imyujin Long Mansion (JOI17_long_mansion) C++14
100 / 100
1192 ms 142208 KB
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

#define MAXN 500005

int N;
vector<int> A[MAXN];
int B[MAXN], C[MAXN], X[MAXN], Y[MAXN];
int left[MAXN], rig[MAXN], lefinv[MAXN], riginv[MAXN];
int L[MAXN], R[MAXN], Rinv[MAXN];
int lastkey[MAXN], sparse[20][MAXN];
vector<int> mn[MAXN], mnt[MAXN];

void findleft(vector<int> A[], int C[], int left[]) {
	for(int i = 1; i <= N; i++) lastkey[i] = 0;
	for(int i = 1; i < N; i++) {
		for(auto a : A[i]) lastkey[a] = i;
		left[i] = lastkey[C[i]];
	}
	left[0] = left[N] = 0;
}

void setL(int left[], int rig[], int L[]) {
	priority_queue<int> pq, pqt;

	for(int i = 0; i <= N; i++) sparse[0][i] = left[i];
	for(int i = 1; i < 20; i++) for(int j = (1 << i) - 1; j <= N; j++)
		sparse[i][j] = min(sparse[i - 1][j], sparse[i - 1][j - (1 << i - 1)]);

	for(int i = 1; i <= N; i++) {
		mn[i].clear();
		mnt[i].clear();
	}
	for(int i = 0; i <= N; i++) {
		int t = rig[i] - 1;
		for(int j = 19; j >= 0; j--) if(t - (1 << j) + 1 >= 0 && sparse[j][t] > i) t -= (1 << j);
		t = max(t, i);
		mn[i + 1].push_back(i + 1);
		mnt[t + 1].push_back(i + 1);
	}
	for(int i = 1; i <= N; i++) {
		for(auto a : mn[i]) pq.push(a);
		for(auto a : mnt[i]) pqt.push(a);
		while(!pqt.empty() && pq.top() == pqt.top()) {
			pq.pop();
			pqt.pop();
		}
		L[i] = pq.top();
	}
}

int main() {
	int Q;

	scanf("%d", &N);
	for(int i = 1; i < N; i++) scanf("%d", C + i);
	for(int i = 1; i <= N; i++) {
		scanf("%d", B + i);
		for(int j = 0; j < B[i]; j++) {
			int t;
			scanf("%d", &t);
			A[i].push_back(t);
		}
	}
	scanf("%d", &Q);
	for(int i = 0; i < Q; i++) scanf("%d%d", X + i, Y + i);

	findleft(A, C, left);
	for(int i = 1; i <= N / 2; i++) swap(A[i], A[N - i + 1]);
	for(int i = 1; i <= (N - 1) / 2; i++) swap(C[i], C[N - i]);
	findleft(A, C, lefinv);
	for(int i = 0; i <= N; i++) {
		rig[i] = N - lefinv[N - i] + 1;
		riginv[i] = N - left[N - i] + 1;
	}

	setL(left, rig, L);
	setL(lefinv, riginv, Rinv);
	for(int i = 1; i <= N; i++) R[i] = N - Rinv[N - i + 1] + 1;

	for(int i = 0; i < Q; i++) {
		if(L[X[i]] <= Y[i] && Y[i] <= R[X[i]]) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

Compilation message

long_mansion.cpp: In function 'void setL(int*, int*, int*)':
long_mansion.cpp:32:66: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   sparse[i][j] = min(sparse[i - 1][j], sparse[i - 1][j - (1 << i - 1)]);
                                                                ~~^~~
long_mansion.cpp: In function 'int main()':
long_mansion.cpp:59:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
long_mansion.cpp:60:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i = 1; i < N; i++) scanf("%d", C + i);
                             ~~~~~^~~~~~~~~~~~~
long_mansion.cpp:62:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", B + i);
   ~~~~~^~~~~~~~~~~~~
long_mansion.cpp:65:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d", &t);
    ~~~~~^~~~~~~~~~
long_mansion.cpp:69:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &Q);
  ~~~~~^~~~~~~~~~
long_mansion.cpp:70:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i = 0; i < Q; i++) scanf("%d%d", X + i, Y + i);
                             ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 38 ms 36216 KB Output is correct
2 Correct 40 ms 36344 KB Output is correct
3 Correct 40 ms 36728 KB Output is correct
4 Correct 38 ms 36216 KB Output is correct
5 Correct 37 ms 36088 KB Output is correct
6 Correct 37 ms 36188 KB Output is correct
7 Correct 38 ms 36216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 36216 KB Output is correct
2 Correct 40 ms 36344 KB Output is correct
3 Correct 40 ms 36728 KB Output is correct
4 Correct 38 ms 36216 KB Output is correct
5 Correct 37 ms 36088 KB Output is correct
6 Correct 37 ms 36188 KB Output is correct
7 Correct 38 ms 36216 KB Output is correct
8 Correct 178 ms 43692 KB Output is correct
9 Correct 177 ms 43640 KB Output is correct
10 Correct 182 ms 44280 KB Output is correct
11 Correct 176 ms 44812 KB Output is correct
12 Correct 167 ms 43128 KB Output is correct
13 Correct 182 ms 43808 KB Output is correct
14 Correct 174 ms 44136 KB Output is correct
15 Correct 168 ms 43896 KB Output is correct
16 Correct 163 ms 43740 KB Output is correct
17 Correct 170 ms 43856 KB Output is correct
18 Correct 165 ms 43896 KB Output is correct
19 Correct 165 ms 43920 KB Output is correct
20 Correct 167 ms 43872 KB Output is correct
21 Correct 164 ms 43640 KB Output is correct
22 Correct 165 ms 43256 KB Output is correct
23 Correct 174 ms 43188 KB Output is correct
24 Correct 174 ms 43128 KB Output is correct
25 Correct 174 ms 42616 KB Output is correct
26 Correct 177 ms 42340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 360 ms 61620 KB Output is correct
2 Correct 366 ms 61696 KB Output is correct
3 Correct 344 ms 61816 KB Output is correct
4 Correct 358 ms 61548 KB Output is correct
5 Correct 360 ms 61400 KB Output is correct
6 Correct 290 ms 61116 KB Output is correct
7 Correct 285 ms 61304 KB Output is correct
8 Correct 282 ms 61316 KB Output is correct
9 Correct 281 ms 61048 KB Output is correct
10 Correct 279 ms 61052 KB Output is correct
11 Correct 279 ms 61028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 36216 KB Output is correct
2 Correct 40 ms 36344 KB Output is correct
3 Correct 40 ms 36728 KB Output is correct
4 Correct 38 ms 36216 KB Output is correct
5 Correct 37 ms 36088 KB Output is correct
6 Correct 37 ms 36188 KB Output is correct
7 Correct 38 ms 36216 KB Output is correct
8 Correct 178 ms 43692 KB Output is correct
9 Correct 177 ms 43640 KB Output is correct
10 Correct 182 ms 44280 KB Output is correct
11 Correct 176 ms 44812 KB Output is correct
12 Correct 167 ms 43128 KB Output is correct
13 Correct 182 ms 43808 KB Output is correct
14 Correct 174 ms 44136 KB Output is correct
15 Correct 168 ms 43896 KB Output is correct
16 Correct 163 ms 43740 KB Output is correct
17 Correct 170 ms 43856 KB Output is correct
18 Correct 165 ms 43896 KB Output is correct
19 Correct 165 ms 43920 KB Output is correct
20 Correct 167 ms 43872 KB Output is correct
21 Correct 164 ms 43640 KB Output is correct
22 Correct 165 ms 43256 KB Output is correct
23 Correct 174 ms 43188 KB Output is correct
24 Correct 174 ms 43128 KB Output is correct
25 Correct 174 ms 42616 KB Output is correct
26 Correct 177 ms 42340 KB Output is correct
27 Correct 360 ms 61620 KB Output is correct
28 Correct 366 ms 61696 KB Output is correct
29 Correct 344 ms 61816 KB Output is correct
30 Correct 358 ms 61548 KB Output is correct
31 Correct 360 ms 61400 KB Output is correct
32 Correct 290 ms 61116 KB Output is correct
33 Correct 285 ms 61304 KB Output is correct
34 Correct 282 ms 61316 KB Output is correct
35 Correct 281 ms 61048 KB Output is correct
36 Correct 279 ms 61052 KB Output is correct
37 Correct 279 ms 61028 KB Output is correct
38 Correct 1192 ms 121032 KB Output is correct
39 Correct 1051 ms 142208 KB Output is correct
40 Correct 781 ms 101408 KB Output is correct
41 Correct 711 ms 141788 KB Output is correct
42 Correct 354 ms 61284 KB Output is correct
43 Correct 329 ms 61288 KB Output is correct
44 Correct 514 ms 81656 KB Output is correct
45 Correct 496 ms 81496 KB Output is correct
46 Correct 472 ms 81400 KB Output is correct
47 Correct 301 ms 61176 KB Output is correct
48 Correct 297 ms 61424 KB Output is correct
49 Correct 436 ms 81400 KB Output is correct
50 Correct 454 ms 81464 KB Output is correct
51 Correct 440 ms 81500 KB Output is correct
52 Correct 433 ms 77584 KB Output is correct
53 Correct 537 ms 95344 KB Output is correct
54 Correct 677 ms 112940 KB Output is correct
55 Correct 567 ms 94884 KB Output is correct