답안 #358919

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
358919 2021-01-26T07:25:43 Z ijxjdjd Long Mansion (JOI17_long_mansion) C++14
100 / 100
503 ms 43116 KB
#include <bits/stdc++.h>
#define FR(i, N) for (int i = 0; i < int(N); i++)
using namespace std;

using ll = long long;
const int MAXN = (int)(5e5) + 5;
int to[MAXN];
vector<int> has[MAXN];
pair<int, int> interval[MAXN];
int maxright[MAXN];
bool cont(int l, int r, int k) {
    if (has[k].size() == 0 || has[k][0] > r || has[k].back() < l || k < 0) {
        return false;
    }
    else {
        int low = 0;
        int high = has[k].size()-1;
        while (low < high) {
            int mid = (low + high)/2;
            if (has[k][mid] >= l) {
                high = mid;
            }
            else {
                low = mid+1;
            }
        }
        int sh = high;
        low = 0; high = has[k].size()-1;
        while (low < high) {
            int mid = (low + high+1)/2;
            if (has[k][mid] <= r) {
                low = mid;
            }
            else {
                high = mid-1;
            }
        }
        return (low - sh + 1) > 0;
    }
}
int main() {
//    freopen("input.in", "r", stdin);
	cin.tie(0);
	cin.sync_with_stdio(0);
	int N, Q;
	cin >> N;
	to[N-1] = -1;
	FR(i, N-1) {
        cin >> to[i];
        to[i]--;
	}
	FR(i, N) {
        int B;
        cin >> B;
        FR(j, B) {
            int a;
            cin >> a;
            a--;
            has[a].push_back(i);
        }
	}
//	maxright[N-1] = N-1;
//	for (int i = N-2; i >= 0; i--) {
//        int u = i;
////        if (i == 1704) {
////            cout << "Wrong";
////        }
//        while (cont(i, u, to[u])) {
//            u = maxright[u+1];
//        }
//        maxright[i] = u;
////        if (maxright[i] < i) {
////            cout << "Wrong";
////        }
//	}
	for (int i = 0; i < N; i++) {
        int l = i;
        int r = i;
        while (true) {
//            r = maxright[r];
//            if (i == 562) {
//                cout << "Wrong";
//            }
            if (l != 0 && cont(l, r, to[l-1])) {
                r = max(r, interval[l-1].second);
                l = interval[l-1].first;
            }
            else if (r != N-1 && cont(l, r, to[r])) {
                r++;
            }
            else {
                break;
            }
        }
        interval[i] = {l, r};
	}
	cin >> Q;
	FR(i, Q) {
        int x, y;
        cin >> x >> y;
        x--, y--;
//        if (i == 515) {
//            cout << x << " " << y << '\n';
//            cout << interval[x].first << " " << interval[x].second << '\n';
//        }
        if (interval[x].first <= y && y <= interval[x].second) {
            cout << "YES" << '\n';
        }
        else cout << "NO" << '\n';
    }
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12268 KB Output is correct
2 Correct 12 ms 12268 KB Output is correct
3 Correct 13 ms 12268 KB Output is correct
4 Correct 11 ms 12268 KB Output is correct
5 Correct 11 ms 12268 KB Output is correct
6 Correct 11 ms 12268 KB Output is correct
7 Correct 11 ms 12268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12268 KB Output is correct
2 Correct 12 ms 12268 KB Output is correct
3 Correct 13 ms 12268 KB Output is correct
4 Correct 11 ms 12268 KB Output is correct
5 Correct 11 ms 12268 KB Output is correct
6 Correct 11 ms 12268 KB Output is correct
7 Correct 11 ms 12268 KB Output is correct
8 Correct 143 ms 18144 KB Output is correct
9 Correct 144 ms 18188 KB Output is correct
10 Correct 152 ms 17112 KB Output is correct
11 Correct 146 ms 17260 KB Output is correct
12 Correct 138 ms 17772 KB Output is correct
13 Correct 141 ms 18412 KB Output is correct
14 Correct 151 ms 18412 KB Output is correct
15 Correct 139 ms 18220 KB Output is correct
16 Correct 137 ms 18028 KB Output is correct
17 Correct 142 ms 18284 KB Output is correct
18 Correct 140 ms 18360 KB Output is correct
19 Correct 138 ms 18412 KB Output is correct
20 Correct 138 ms 18284 KB Output is correct
21 Correct 151 ms 18164 KB Output is correct
22 Correct 157 ms 18312 KB Output is correct
23 Correct 141 ms 18028 KB Output is correct
24 Correct 141 ms 18028 KB Output is correct
25 Correct 144 ms 18156 KB Output is correct
26 Correct 142 ms 18028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 269 ms 19948 KB Output is correct
2 Correct 275 ms 24172 KB Output is correct
3 Correct 265 ms 24024 KB Output is correct
4 Correct 316 ms 24464 KB Output is correct
5 Correct 270 ms 24556 KB Output is correct
6 Correct 265 ms 22764 KB Output is correct
7 Correct 229 ms 22672 KB Output is correct
8 Correct 219 ms 22508 KB Output is correct
9 Correct 226 ms 22764 KB Output is correct
10 Correct 230 ms 22508 KB Output is correct
11 Correct 219 ms 22508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12268 KB Output is correct
2 Correct 12 ms 12268 KB Output is correct
3 Correct 13 ms 12268 KB Output is correct
4 Correct 11 ms 12268 KB Output is correct
5 Correct 11 ms 12268 KB Output is correct
6 Correct 11 ms 12268 KB Output is correct
7 Correct 11 ms 12268 KB Output is correct
8 Correct 143 ms 18144 KB Output is correct
9 Correct 144 ms 18188 KB Output is correct
10 Correct 152 ms 17112 KB Output is correct
11 Correct 146 ms 17260 KB Output is correct
12 Correct 138 ms 17772 KB Output is correct
13 Correct 141 ms 18412 KB Output is correct
14 Correct 151 ms 18412 KB Output is correct
15 Correct 139 ms 18220 KB Output is correct
16 Correct 137 ms 18028 KB Output is correct
17 Correct 142 ms 18284 KB Output is correct
18 Correct 140 ms 18360 KB Output is correct
19 Correct 138 ms 18412 KB Output is correct
20 Correct 138 ms 18284 KB Output is correct
21 Correct 151 ms 18164 KB Output is correct
22 Correct 157 ms 18312 KB Output is correct
23 Correct 141 ms 18028 KB Output is correct
24 Correct 141 ms 18028 KB Output is correct
25 Correct 144 ms 18156 KB Output is correct
26 Correct 142 ms 18028 KB Output is correct
27 Correct 269 ms 19948 KB Output is correct
28 Correct 275 ms 24172 KB Output is correct
29 Correct 265 ms 24024 KB Output is correct
30 Correct 316 ms 24464 KB Output is correct
31 Correct 270 ms 24556 KB Output is correct
32 Correct 265 ms 22764 KB Output is correct
33 Correct 229 ms 22672 KB Output is correct
34 Correct 219 ms 22508 KB Output is correct
35 Correct 226 ms 22764 KB Output is correct
36 Correct 230 ms 22508 KB Output is correct
37 Correct 219 ms 22508 KB Output is correct
38 Correct 373 ms 33128 KB Output is correct
39 Correct 393 ms 33964 KB Output is correct
40 Correct 347 ms 31060 KB Output is correct
41 Correct 503 ms 31492 KB Output is correct
42 Correct 250 ms 24172 KB Output is correct
43 Correct 229 ms 24044 KB Output is correct
44 Correct 320 ms 29932 KB Output is correct
45 Correct 342 ms 30060 KB Output is correct
46 Correct 331 ms 30444 KB Output is correct
47 Correct 226 ms 24428 KB Output is correct
48 Correct 238 ms 23788 KB Output is correct
49 Correct 323 ms 29560 KB Output is correct
50 Correct 349 ms 29804 KB Output is correct
51 Correct 367 ms 30956 KB Output is correct
52 Correct 244 ms 31212 KB Output is correct
53 Correct 300 ms 36968 KB Output is correct
54 Correct 336 ms 43116 KB Output is correct
55 Correct 322 ms 37100 KB Output is correct