Submission #1305653

#TimeUsernameProblemLanguageResultExecution timeMemory
1305653vako_pLong Mansion (JOI17_long_mansion)C++20
10 / 100
3095 ms33660 KiB
#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define pb push_back
#define ff first
#define sd second
#define debug(x) cerr << #x << " -----> " << x << endl;
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")

const int mxN = 1e6 + 5;
ll n,l[mxN],r[mxN],lf[mxN],rt[mxN],L[mxN],R[mxN],b[mxN],c[mxN];
vector<ll> a[mxN];

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
  cin >> n;
  for(int i = 1; i <= n - 1; i++) cin >> c[i];
  for(int i = 1; i <= n; i++){
    cin >> b[i];
    a[i].resize(b[i]);
    for(int j = 0; j < b[i]; j++) cin >> a[i][j]; 
  }
  vector<pair<ll,ll>>  v;
  for(int i = 1; i <= n - 1; i++){
    bool ok = false;
    r[i] = n + 1;
    for(int j = i; j > 0; j--){
      for(auto it : a[j]) if(it == c[i]) ok = true;
      if(ok){
        l[i] = j;
        break;
      }
    } 
    if(!l[i]) v.pb({0, i});
    ok = false;
    for(int j = i + 1; j <= n; j++){
      for(auto it : a[j]) if(it == c[i]) ok = true;
      if(ok){
        r[i] = j;
        break;
      }
    } 
    if(r[i] == n + 1) v.pb({i + 1, n + 1});
  }
  for(int i = 1; i <= n - 1; i++){
    lf[i] = n + 1; 
    rt[i] = 0;
    for(int j = i + 1; j <= n - 1; j++){
      if(r[i] > j and l[j] <= i){
        rt[i] = j;
        v.pb({i + 1, j});
      } 
    }
    for(int j = i - 1; j >= 1; j--){
      if(l[i] <= j and r[j] > i){
        lf[i] = j;
        v.pb({j + 1, i});
      } 
    }
  }
  for(int i = 1; i <= n; i++){
    R[i] = n + 1;
    for(int j = i; j < n; j++){
      if(!l[j] or lf[j] < i){
        R[i] = j;
        break;
      }
    }
    for(int j = i - 1; j > 0; j--){
      if(r[j] == n + 1 or rt[j] >= i){
        L[i] = j + 1;
        break;
      }
    }
    // cout << L[i] << ' ' << R[i] << '\n';
  }
  ll q;
  cin >> q;
  while(q--){
    ll x,y,lx,rx;
    cin >> x >> y;
    lx = rx = x;
    while(lx > y or rx < y){
      bool ok = true;
      if(lx > 1){
        if(r[lx - 1] >= lx and r[lx - 1] <= rx){
          if(lx < x) assert(rt[lx] < x and r[lx] != n + 1);
          lx--;
          ok = false;
        }
      }
      if(rx < n){
        if(l[rx] >= lx and l[rx] <= rx){
          assert(lf[rx] >= x and l[rx]);
          rx++;
          ok = false;
        }
      }
      if(ok) break;
    }
    if(y >= lx and y <= rx) cout << "YES" << '\n';
    else cout << "NO" << '\n';
    // if(y >= L[x] and y <= R[x]) cout << "YES" << '\n';
    // else cout << "NO" << '\n';
    // ll l1 = 0,r1 = n + 1;
    // for(auto it : v) if(it.ff <= x and it.sd >= x){l1 = max(l1, it.ff); r1 = min(r1, it.sd);}
    // if(y >= l1 and y <= r1) cout << "YES" << '\n';
    // else cout << "NO" << '\n';
  }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...