제출 #850516

#제출 시각아이디문제언어결과실행 시간메모리
850516MilosMilutinovicLong Mansion (JOI17_long_mansion)C++14
10 / 100
3083 ms28752 KiB
/**
 *    author:  wxhtzdy
 *    created: 16.09.2023 20:57:23
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int n;
  cin >> n;
  vector<int> c(n);
  for (int i = 0; i + 1 < n; i++) {
    cin >> c[i];
    --c[i];
  }
  vector<set<int>> pos(n);
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int a;
      cin >> a;
      --a;
      pos[a].insert(i);
    }
  }
  auto Have = [&](int L, int R, int k) {
    auto it = pos[k].lower_bound(L);
    return (it != pos[k].end() && *it <= R);    
  };    
  vector<int> L(n), R(n);
  for (int i = 0; i < n; i++) {
    L[i] = R[i] = i;
    while (true) {
      if (L[i] > 0 && Have(L[i], R[i], c[L[i] - 1])) {
        L[i] = L[L[i] - 1];
        R[i] = max(R[i], R[L[i]]);
      } else if (R[i] + 1 < n && Have(L[i], R[i], c[R[i]])) {
        R[i] += 1;        
      } else {
        break;
      }
    }    
  }
  int q;
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    --x; --y;
    cout << (L[x] <= y && y <= R[x] ? "YES" : "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...