Submission #857807

# Submission time Handle Problem Language Result Execution time Memory
857807 2023-10-07T02:50:05 Z NeroZein Event Hopping (BOI22_events) C++17
0 / 100
1500 ms 10512 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif

const int LOG = 18;
const int N = 1e5 + 5;

int s[N], e[N]; 
int nx[LOG][N]; 

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, q;
  cin >> n >> q; 
  vector<pair<int, int>> ev;
  for (int i = 1; i <= n; ++i) {
    cin >> s[i] >> e[i];
    ev.emplace_back(s[i], ~i);
    ev.emplace_back(e[i], i); 
  }
  sort(ev.begin(), ev.end()); 
  set<pair<int, int>> active;
  for (int i = 0; i < 2 * n; ++i) {
    auto [x, y] = ev[i];
    if (y < 0) {
      active.emplace(e[~y], ~y); 
    } else {
      active.erase({x, y});
      if (!active.empty()) {
        auto tmp = *active.rbegin(); 
        nx[0][y] = tmp.second;
      }
    }
  }
  for (int j = 1; j < LOG; ++j) {
    for (int i = 1; i <= n; ++i) {
      nx[j][i] = nx[j - 1][nx[j - 1][i]]; 
    }
  }
  auto intersect = [&](int i, int j) -> bool {
    return s[j] <= e[i] && e[i] <= e[j];
  };
  while (q--) {
    int st, en;
    cin >> st >> en;
    if (st == en) {
      cout << 0 << '\n';
      continue;
    }
    if (intersect(st, en)) {
      cout << 1 << '\n';
      continue;
    }
    int ans = 0; 
    for (int j = LOG - 1; j >= 0; --j) {
      if (nx[j][st] && e[nx[j][st]] < s[en]) {
        st = nx[j][st]; 
        ans += 1 << j; 
      }
    }
    //deb(st) cout << '\n';
    bool f = false; 
    for (int j = 1; j <= n; ++j) {
      if (intersect(st, j) && intersect(j, en)) {
        cout << ans + 2 << '\n';
        f = true;
        break; 
      }
    }
    if (!f) {
      cout << "impossible" << '\n'; 
    }
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6644 KB Output is correct
2 Correct 805 ms 10512 KB Output is correct
3 Execution timed out 1548 ms 10324 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 2 ms 6744 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Incorrect 2 ms 6492 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 2 ms 6744 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Incorrect 2 ms 6492 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 2 ms 6744 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Incorrect 2 ms 6492 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 846 ms 10436 KB Output is correct
2 Execution timed out 1590 ms 10324 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6644 KB Output is correct
2 Correct 805 ms 10512 KB Output is correct
3 Execution timed out 1548 ms 10324 KB Time limit exceeded
4 Halted 0 ms 0 KB -