제출 #1066954

#제출 시각아이디문제언어결과실행 시간메모리
1066954vjudge1Fire (BOI24_fire)C++17
100 / 100
110 ms21720 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;

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;
            }
        }
    }

    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...