Submission #1173104

#TimeUsernameProblemLanguageResultExecution timeMemory
1173104domblyEvent Hopping (BOI22_events)C++20
0 / 100
1595 ms3524 KiB
#include <bits/stdc++.h>
#define F first
#define S second

using namespace std;

const int N = 1e5 + 10;

const int LOG = 17;

int n, l[N], r[N], up[N][LOG];

bool cmp(int i, int j) {
  if(r[i] == r[j]) return l[i] < l[j];
  return r[i] < r[j];
}

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

  int q;
  cin >> n >> q;
  for(int i = 1; i <= n; i++) cin >> l[i] >> r[i];
  vector<int> order(n + 1), p(n + 1);
  for(int i = 1; i <= n; i++) order[i] = i;
  for(int i = 1; i <= n; i++) p[order[i]] = i;
  sort(order.begin() + 1, order.end(), cmp);
  //for(int i = 1; i <= n; i++) cout << l[order[i]] << " " << r[order[i]] << endl;
  for(int i = 1; i <= n; i++) {
    int mn = 0;
    for(int j = 1; j <= n; j++) {
      if(r[order[j]] >= l[order[i]] && r[order[i]] >= r[order[j]]) {
        if(mn == 0 || l[order[mn]] > l[order[j]]) mn = j;
      }
    }
   // cout << i << " " << mn << " dbg" << endl;
    up[i][0] = mn;
  }
  for(int j = 1; j < LOG; j++) {
    for(int i = 1; i <= n; i++) {
      up[i][j] = up[up[i][j - 1]][j - 1];
    }
  }
  while(q--) {
    int x, y;
    cin >> x >> y;
    x = p[x]; y = p[y];
    if(x == y) {
      cout << "0\n";
      continue;
    }
    if(x > y) {
      cout << "impossible\n";
      continue;
    }
    if (l[y] <= r[x] && r[x] <= r[y]) {
      cout << "1\n";
      continue;
    }
    int res = 1;
    for(int j = LOG - 1; j >= 0; j--) {
      if(up[y][j] > x) {
        res += (1 << j);
        y = up[y][j];
      }
    }
    if(up[y][0] > x || up[y][0] == 0) {
      cout << "impossible\n";
    } else {
      cout << res << "\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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...