Submission #682613

#TimeUsernameProblemLanguageResultExecution timeMemory
682613peijarLong Mansion (JOI17_long_mansion)C++17
25 / 100
3062 ms42744 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;

string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
  bool first = true;
  string res = "[";
  for (const auto &x : v) {
    if (!first)
      res += ", ";
    first = false;
    res += to_string(x);
  }
  res += "]";
  return res;
}

void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
  cout << ' ' << to_string(H);
  dbg_out(T...);
}

#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

signed main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int nbChambres;
  cin >> nbChambres;
  vector<int> type(nbChambres - 1);
  for (int &t : type) {
    cin >> t;
    --t;
  }

  vector<vector<int>> posType(nbChambres);
  for (int i = 0; i < nbChambres; ++i) {
    int cnt;
    cin >> cnt;
    while (cnt--) {
      int t;
      cin >> t;
      --t;
      posType[t].push_back(i);
    }
  }
  vector<int> L(nbChambres), R(nbChambres);
  for (int iChambre = nbChambres - 1; iChambre >= 0; --iChambre) {
    int j = iChambre;
    while (j + 1 < nbChambres) {
      int t = type[j];
      auto fst = lower_bound(posType[t].begin(), posType[t].end(), iChambre) -
                 posType[t].begin();
      if (fst == (int)posType[t].size() or posType[t][fst] > j)
        break;
      j = R[j + 1];
    }
    R[iChambre] = j;
  }

  dbg(R);

  for (int iChambre = 0; iChambre < nbChambres; ++iChambre) {
    L[iChambre] = iChambre;
    while (L[iChambre] >= 0) {
      while (R[iChambre] + 1 < nbChambres) {
        int t = type[R[iChambre]];
        auto fst =
            lower_bound(posType[t].begin(), posType[t].end(), L[iChambre]) -
            posType[t].begin();
        if (fst == (int)posType[t].size() or posType[t][fst] > R[iChambre])
          break;
        R[iChambre] = R[R[iChambre] + 1];
      }
      if (L[iChambre] == 0)
        break;

      // can we reduce L[iChambre] ?
      int t = type[L[iChambre] - 1];
      auto fst =
          lower_bound(posType[t].begin(), posType[t].end(), L[iChambre]) -
          posType[t].begin();
      if (fst == (int)posType[t].size() or posType[t][fst] > R[iChambre])
        break;
      L[iChambre] = L[L[iChambre] - 1];
      R[iChambre] = max(R[iChambre], R[L[iChambre]]);
    }
  }

  dbg(L);
  dbg(R);

  int Q;
  cin >> Q;
  while (Q--) {
    int from, to;
    cin >> from >> to;
    --from, --to;
    cout << (L[from] <= to and to <= R[from] ? "YES" : "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...