Submission #998334

#TimeUsernameProblemLanguageResultExecution timeMemory
998334steveonalexFire (BOI24_fire)C++17
100 / 100
77 ms16892 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
 
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
 
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
 
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }
 
template <class T>
    void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }
 
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

const int N = 2e5 + 69;
const int LOG_N= 18;
const int INF = MASK(LOG_N);

int n, m;
int nxt[LOG_N][N];
int pref[N];

int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n >> m;
    vector<pair<int,int>> a(n);
    for(int i = 0; i<n; ++i) {
        int l, r; cin >> l >> r;
        if (l > r) r += m;
        a[i] = {l, r};
    }

    sort(ALL(a));
    pref[0] = 0;
    for(int i = 1; i<n; ++i){
        pref[i] = pref[i-1];
        if (a[pref[i]].second < a[i].second) pref[i] = i;
    }

    for(int i = 0; i<n; ++i){
        int idx = lower_bound(ALL(a), make_pair(a[i].second + 1, -1)) - a.begin() - 1;
        idx = pref[idx];
        nxt[0][i] = idx;
    }
    for(int j = 1; j < LOG_N; ++j) for(int i = 0; i<n; ++i) 
        nxt[j][i] = nxt[j-1][nxt[j-1][i]];
    int ans = INF;
    for(int i = 0; i<n; ++i){   
        int cu = 0;
        int u = i;
        for(int j = LOG_N - 1; j>=0; --j) {
            int v = nxt[j][u];
            if (a[v].second < a[i].first + m) {
                cu += MASK(j);
                u = v;
            }
        }
        minimize(ans, cu + 2);
    }

    if (ans > n) ans = -1;
    cout << ans << "\n";

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