제출 #1086455

#제출 시각아이디문제언어결과실행 시간메모리
1086455IBoryFire (BOI24_fire)C++17
0 / 100
1 ms4444 KiB
#include <bits/stdc++.h> #define pii pair<int, int> using namespace std; const int MAX = 400007; int R[19][MAX]; int main() { ios::sync_with_stdio(0); cin.tie(0); int N, M; cin >> N >> M; vector<pii> T, ST; for (int i = 1; i <= N; ++i) { int a, b; cin >> a >> b; if (b < a) b += M; T.emplace_back(a, b); T.emplace_back(a + M, b + M); } sort(T.begin(), T.end()); for (auto [a, b] : T) { while (!ST.empty() && ST.back().first == a && ST.back().second <= b) ST.pop_back(); if (ST.empty() || ST.back().second < b) ST.emplace_back(a, b); } N = ST.size(); for (int i = 0; i < N; ++i) { auto [a, b] = ST[i]; auto it = lower_bound(ST.begin(), ST.end(), pii{b, (int)2e9}) - ST.begin(); R[0][i] = it - 1; assert(ST[R[0][i]].first <= b); if (i) { auto [c, d] = ST[i - 1]; assert(c < a && d < b); } } for (int n = 1; n < 19; ++n) for (int i = 0; i < N; ++i) R[n][i] = R[n - 1][R[n - 1][i]]; int ans = 1e9; for (int i = 0; i < N; ++i) { int cur = i, goal = ST[cur].first + M; for (int n = 18; n >= 0; --n) { auto [a, b] = ST[R[n][cur]]; if (b < goal) cur = R[n][cur]; } cur = R[0][cur]; if (goal <= ST[cur].second) ans = min(ans, cur - i + 1); } cout << (ans == 1e9 ? -1 : ans); 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...