# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
21133 | khsoo01 | Long Mansion (JOI17_long_mansion) | C++11 | 469 ms | 48576 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int inf = 1e9;
int n, a[500005], l[500005], r[500005], lst[500005];
int q, xl[500005], xr[500005];
vector<int> st, ky[500005];
struct minseg {
int val[1111111], lim;
void init () {
for(lim=1;lim<=n+1;lim<<=1);
val[lim] = inf;
for(int i=1;i<=n;i++) {
val[lim + i] = l[i];
}
for(int i=lim;--i;) {
val[i] = min(val[2*i], val[2*i+1]);
}
}
void update (int P, int V) {
P += lim;
val[P] = V; P >>= 1;
while(P) {
val[P] = min(val[2*P], val[2*P+1]);
P >>= 1;
}
}
int query (int X, int SS, int SE, int P) {
if(SS == SE) return SS;
int mid = (SS+SE)/2;
return (val[2*P] < X ? query(X, SS, mid, 2*P) : query(X, mid+1, SE, 2*P+1));
}
int query (int X) {return query(X, 0, lim-1, 1);}
} seg;
int main()
{
scanf("%d",&n);
for(int i=1;i<n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++) {
int M;
scanf("%d",&M);
for(int j=0;j<M;j++) {
int tmp;
scanf("%d",&tmp);
ky[i].push_back(tmp);
}
}
for(int i=1;i<n;i++) {
for(auto &j : ky[i]) lst[j] = i;
l[i+1] = lst[a[i]];
}
for(int i=1;i<=n;i++) lst[i] = n+1;
r[n] = n+1;
for(int i=n;i>1;i--) {
for(auto &j : ky[i]) lst[j] = i;
r[i-1] = lst[a[i-1]];
}
seg.init();
for(int i=1;i<=n;i++) {
xl[i] = i; xr[i] = i;
seg.update(i, inf);
while(true) {
xr[i] = seg.query(xl[i]) - 1;
if(xl[i] == 1 || r[xl[i]-1] > xr[i]) break;
xl[i] = st.back(); st.pop_back();
}
st.push_back(xl[i]);
}
scanf("%d",&q);
for(int i=1;i<=q;i++) {
int A, B;
scanf("%d%d",&A,&B);
puts(xl[A] <= B && B <= xr[A] ? "YES" : "NO");
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |