답안 #358905

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
358905 2021-01-26T07:15:57 Z ijxjdjd Long Mansion (JOI17_long_mansion) C++14
0 / 100
3000 ms 16940 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 (l != 0 && cont(l, r, to[l-1])) {
                l = interval[l-1].first;
                r = max(r, interval[l-1].second);
            }
            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 == 68) {
//            cout << x << '\n';
//        }
        if (interval[x].first <= y && y <= interval[x].second) {
            cout << "YES" << '\n';
        }
        else cout << "NO" << '\n';
    }
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 12268 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 12268 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3070 ms 16940 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 12268 KB Output isn't correct
2 Halted 0 ms 0 KB -