Submission #1342369

#TimeUsernameProblemLanguageResultExecution timeMemory
1342369ramzialoulouFire (BOI24_fire)C++20
100 / 100
125 ms18440 KiB
#include <bits/stdc++.h>
 
using namespace std;

const int N = int(2e5) + 9;
const int LOG = 20;
int up[LOG][N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> a(n);
  for (int i = 0; i < n; i++) {
    int s, e;
    cin >> s >> e;
    if (s > e) {
      e += m;
    }
    a[i] = {s, e};
  }
  sort(a.begin(), a.end());
  vector<int> mx(n);
  for (int i = 1; i < n; i++) {
    mx[i] = (a[i].second > a[mx[i - 1]].second ? i : mx[i - 1]);
  }
  for (int i = 0; i < n; i++) {
    auto [s, e] = a[i];
    int j = upper_bound(a.begin(), a.end(), make_pair(e + 1, -1)) - a.begin() - 1;
    up[0][i] = mx[j];
  }
  for (int j = 1; j < LOG; j++) {
    for (int i = 0; i < n; i++) {
      up[j][i] = up[j - 1][up[j - 1][i]];
    }
  }
  auto check = [&](int k) {
    for (int i = 0; i < n; i++) {
      int x = i;
      for (int j = 0; j < LOG; j++) {
        if ((k - 1) >> j & 1) {
          x = up[j][x];
        }
      }
      if (a[x].second - a[i].first >= m) {
        return true;
      }
    }
    return false;
  };
  int low = 1, high = n;
  while (low <= high) {
    int mid = (low + high) >> 1;
    if (check(mid)) {
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  cout << (high + 1 > n ? -1 : high + 1) << '\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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...