Submission #358919

#TimeUsernameProblemLanguageResultExecution timeMemory
358919ijxjdjdLong Mansion (JOI17_long_mansion)C++14
100 / 100
503 ms43116 KiB
#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; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...