# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
21040 | ainta | Long Mansion (JOI17_long_mansion) | C++14 | 1363 ms | 65844 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<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#define SZ 524288
using namespace std;
vector<int>E[501000];
int C[501000], L[501000], R[501000], LL[501000], RR[501000];
int n, Q;
set<int>Set;
struct point{
int x, num;
bool operator<(const point &p)const{
return x<p.x;
}
}ord[501000];
struct Tree{
int Mn, Mx;
}IT[SZ+SZ];
Tree Get(int b, int e){
b += SZ, e += SZ;
Tree r = {n+1,0};
while(b<=e){
r.Mn = min(r.Mn,min(IT[b].Mn,IT[e].Mn));
r.Mx = max(r.Mx,max(IT[b].Mx,IT[e].Mx));
b=(b+1)>>1,e=(e-1)>>1;
}
return r;
}
int main(){
int i, a, b;
scanf("%d",&n);
for(i=1;i<n;i++)scanf("%d",&C[i]);
for(i=1;i<=n;i++){
scanf("%d",&a);
while(a--){
scanf("%d",&b);
E[b].push_back(i);
}
}
for(i=1;i<=n;i++){
a = C[i-1];
if(E[a].empty()) L[i] = n+1;
else{
int t = lower_bound(E[a].begin(),E[a].end(),i)-E[a].begin();
if(t == E[a].size())L[i] = n+1;
else L[i] = E[a][t];
}
}
for(i=n;i>=1;i--){
a = C[i];
if(E[a].empty()) R[i] = 0;
else{
int t = lower_bound(E[a].begin(),E[a].end(),i+1)-E[a].begin();
if(t == 0)R[i] = 0;
else R[i] = E[a][t-1];
}
}
for(i=1;i<=n;i++){
ord[i].x=L[i],ord[i].num = i;
Set.insert(i);
}
sort(ord+1,ord+n+1);
int pv = 1;
for(i=1;i<=n;i++){
LL[i] = n+1;
while(pv<=n && ord[pv].x <= i){
Set.erase(ord[pv].num);
pv++;
}
if(Set.empty())continue;
auto it = Set.lower_bound(R[i]+1);
if(it!=Set.end() && (*it) <= i){
LL[i] = *it;
}
}
Set.clear();
for(i=1;i<=n;i++){
ord[i].x=R[i],ord[i].num = i;
Set.insert(i);
}
sort(ord+1,ord+n+1);
pv = n;
for(i=n;i>=1;i--){
RR[i] = 0;
while(pv>=1 && ord[pv].x >= i){
Set.erase(ord[pv].num);
pv--;
}
if(Set.empty())continue;
auto it = Set.lower_bound(L[i]);
if(it!=Set.begin()){
it--;
if((*it) >= i)RR[i] = *it;
}
}
scanf("%d",&Q);
for(i=1;i<=n;i++){
a = SZ+i;
IT[a].Mn = LL[i], IT[a].Mx = RR[i];
while(a!=1){
a>>=1;
IT[a].Mn = min(IT[a+a].Mn,IT[a+a+1].Mn);
IT[a].Mx = max(IT[a+a].Mx,IT[a+a+1].Mx);
}
}
while(Q--){
scanf("%d%d",&a,&b);
if(a<b){
if(Get(a,b-1).Mn <= a)puts("NO");
else puts("YES");
}
else{
if(Get(b+1,a).Mx >= a)puts("NO");
else puts("YES");
}
}
}
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... |