Submission #1066951

#TimeUsernameProblemLanguageResultExecution timeMemory
1066951vjudge1Fire (BOI24_fire)C++17
100 / 100
123 ms24264 KiB
#include <bits/stdc++.h> using namespace std; using i64 = long long; constexpr int mxN = 200'000; int N, M; pair<int,int> se[mxN + 1]; int nxt[mxN + 1][21]; vector<int> normal; i64 dp[3 * mxN + 5]; int getNorm(int x) { int lo = 0, hi = (int) normal.size() - 1; while (lo <= hi) { int mid = lo + (hi - lo) / 2; if (normal[mid] == x) return mid; else if (normal[mid] < x) lo = mid + 1; else hi = mid - 1; } return -1; } void solve() { cin >> N >> M; for (int i = 0; i < N; i++) { cin >> se[i].first >> se[i].second; if (se[i].second < se[i].first) se[i].second += M; normal.emplace_back(se[i].first); normal.emplace_back(se[i].second); } sort(normal.begin(), normal.end()); normal.erase(unique(normal.begin(), normal.end()), normal.end()); sort(se, se + N); priority_queue<pair<int,int>> pq; int id = 0, r = 0; for (int i = 0; i < (int) normal.size(); i++) { for (; id < N && se[id].first == normal[i]; id++) { pq.push({-se[id].second, id}); if (se[id].second > se[r].second) { r = id; } } if (!pq.empty()) { int tmp = r; for (; !pq.empty() && -pq.top().first == normal[i]; pq.pop()) { nxt[pq.top().second][0] = tmp; } } } /* cout << "Nxt: "; for (int i = 0; i < N; i++) { cout << nxt[i][0] << " " ; } cout << endl; */ for (int j = 1; j < 20; j++) { for (int i = 0; i < N; i++) { nxt[i][j] = nxt[nxt[i][j - 1]][j - 1]; } } int ans = N + 1; for (int i = 0; i < N; i++) { int now = i, cnt = 1; for (int j = 19; j >= 0; j--) { if (se[nxt[now][j]].second < se[i].first + M) { now = nxt[now][j]; cnt += (1 << j); } } now = nxt[now][0]; cnt++; if (se[now].second >= se[i].first + M) { ans = min(ans, cnt); } } if (ans > N) ans = -1; cout << ans << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); int tc = 1; //cin >> tc; while (tc--) { solve(); } 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...