Submission #1285359

#TimeUsernameProblemLanguageResultExecution timeMemory
1285359lmquanFire (BOI24_fire)C++20
100 / 100
75 ms27836 KiB
#define taskname ""
#include <bits/stdc++.h>
using namespace std;
const int kMaxLog = 30;

int main() {
  if (fopen(taskname".inp", "r")) {
    freopen(taskname".inp", "r", stdin);
    freopen(taskname".out", "w", stdout);
  }
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m;
  cin >> n >> m;

  vector<pair<int, int>> s;

  for (int i = 1; i <= n; i++) {
    int l, r;
    cin >> l >> r;
    s.push_back(make_pair(l, r + (r < l ? m : 0)));
  }

  sort(s.begin(), s.end(), [](pair<int, int> x, pair<int, int> y) {
    return (x.first < y.first || (x.first == y.first && x.second > y.second));
  });

  vector<pair<int, int>> k;
  int maxr = 0;
  for (auto [l, r] : s) {
    if (r <= maxr) {
      continue;
    }
    k.push_back(make_pair(l, r));
    maxr = r;
  }

  vector<vector<int>> jump(kMaxLog, vector<int>(k.size(), -1));

  int p = 0;
  for (int i = 0; i < k.size(); i++) {
    p = max(p, i);
    while (p + 1 < k.size() && k[p + 1].first <= k[i].second) {
      p++;
    }
    jump[0][i] = (p != i) ? p : -1;
  }

  for (int i = 1; i < kMaxLog; i++) {
    for (int j = 0; j < k.size(); j++) {
      if (jump[i - 1][j] == -1) {
        continue;
      }
      jump[i][j] = (jump[i - 1][j] != -1) ? jump[i - 1][jump[i - 1][j]] : -1;
    }
  }

  int result = m + 1;

  for (int i = 0; i < k.size(); i++) {
    if (k[i].second >= k[i].first + m) {
      result = 1;
      break;
    }

    int c = 1, t = i;
    for (int j = kMaxLog - 1; j >= 0; j--) {
      if (jump[j][t] != -1 && k[jump[j][t]].second < k[i].first + m - 1) {
        c += (1 << j), t = jump[j][t];
      }
    }

    if (jump[0][t] != -1) {
      result = min(result, c + 1);
    }
  }

  cout << (result > m ? -1 : result);

  return 0;
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:8:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    8 |     freopen(taskname".inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:9:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 |     freopen(taskname".out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...