Submission #1305676

#TimeUsernameProblemLanguageResultExecution timeMemory
1305676vako_pLong Mansion (JOI17_long_mansion)C++20
100 / 100
1121 ms158576 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];
  vector<vector<ll>> v(n + 5);
  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];
      v[a[i][j]].pb(i);
    } 
  }
  for(int i = 1; i <= n - 1; i++){
    bool ok = false;
    r[i] = n + 1;
    auto it = upper_bound(v[c[i]].begin(), v[c[i]].end(), i);
    if(it != v[c[i]].end()) r[i] = *it;
    if(it != v[c[i]].begin()) l[i] = *(--it);
  }
  vector<pair<ll,ll>> vv; 
  vector<ll> nums[2][n + 5];
  for(int i = 1; i <= n - 1; i++){
    nums[0][r[i] - 1].pb(i);
    nums[1][i].pb(i);
  }
  multiset<ll> s;
  s.insert(0);
  for(int i = n; i > 0; i--){
    for(auto it : nums[0][i]) s.insert(it);
    for(auto it : nums[1][i]) s.erase(s.find(it));
    nums[0][i].clear();
    nums[1][i].clear();
    lf[i] = n + 1;
    auto it = s.lower_bound(l[i]);
    if(it != s.end()) lf[i] = *it;
  }
  s.clear();
  s.insert(n);
  for(int i = 1; i <= n - 1; i++){
    nums[0][l[i]].pb(i);
    nums[1][i].pb(i);
  }
  for(int i = 0; i <= n - 1; i++){
    for(auto it : nums[0][i]) s.insert(it);
    for(auto it : nums[1][i]) s.erase(s.find(it));
    nums[0][i].clear();
    nums[1][i].clear();
    auto it = s.lower_bound(r[i]);
    if(it != s.begin()){
      --it;
      rt[i] = *it;
    }
  }
  for(int i = 1; i <= n - 1; i++){
  //   lf[i] = n + 1;
  //   rt[i] = 0;
  //   if(r[i] < n + 1) for(int j = i + 1; j <= n - 1; j++){
  //     if(r[i] > j and l[j] <= i){
  //       rt[i] = j;
  //     } 
  //   }
  //   else{
  //     rt[i] = n;
  //   } 
    if(i + 1 <= rt[i]) vv.pb({i + 1, rt[i]});
  //   if(l[i]) for(int j = i - 1; j >= 1; j--){
  //     if(l[i] <= j and r[j] > i){
  //       lf[i] = j;
  //     } 
  //   }
  //   else{
  //     lf[i] = 0;
  //   } 
    if(lf[i] + 1 <= i) vv.pb({lf[i] + 1,i});
  }
  for(auto it : vv){
    // cout << it.ff << ' ' << it.sd << endl;
    nums[0][it.sd].pb(it.ff);
    nums[1][it.ff - 1].pb(it.ff);
  } 
  // multiset<ll> s;
  s.clear();
  s.insert(0);
  for(int i = n; i > 0; i--){
    for(auto it : nums[0][i]) s.insert(it);
    for(auto it : nums[1][i]) s.erase(s.find(it));
    nums[0][i].clear();
    nums[1][i].clear();
    L[i] = *(--s.end());
  }
  s.clear();
  s.insert(n + 1);
  for(auto it : vv){
    nums[0][it.sd].pb(it.sd);
    nums[1][it.ff - 1].pb(it.sd);
  }
  for(int i = n; i > 0; i--){
    for(auto it : nums[0][i]) s.insert(it);
    for(auto it : nums[1][i]) s.erase(s.find(it));
    R[i] = *s.begin();
    // cout << i << ' ' << L[i] << ' ' << R[i] << '\n';
  }
  ll q;
  cin >> q;
  while(q--){
    ll x,y;
    cin >> x >> y;
    if(y >= L[x] and y <= R[x]) 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...