Submission #1281435

#TimeUsernameProblemLanguageResultExecution timeMemory
1281435red_soulsFire (BOI24_fire)C++17
40 / 100
210 ms91452 KiB
#include <bits/stdc++.h>
#define ll long long
#define task "FIRE"
using namespace std;

const int N = 1e6 + 16;
const ll INF = 1e18;
int n, m;
ll s[N], e[N];

namespace sub3 {
    ll cnt, result = INF;
    pair <ll, ll> a[N];

    vector < pair <ll, ll> > checkLines;

    bool cmp(pair <ll, ll> a, pair <ll, ll> b) {
        if (a.first == b.first) {
            return a.second > b.second;
        }
        return a.first < b.first;
    }

    ll query(pair <ll, ll> target) {
        ll curP = 1, p = target.first, cnt = 0;
        while (p <= target.second) {
            ll r = -1;
            while (curP <= n && a[curP].first <= p) {
                r = max(a[curP].second, r);
                curP++;
            }
            if (r < p) {
                return -1;
            }
            p = r + 1;
            cnt++;
        }
        return cnt;
    }

    void solve() {
        checkLines.push_back({0, m - 1});
        for (int i = 1; i <= n; i++) {
            if (s[i] > e[i]) {
                checkLines.push_back({e[i], e[i] + m - 1});
                e[i] += m;
            }
            a[i] = {s[i], e[i] - 1};
        }
        sort(a + 1, a + 1 + n, cmp);
        for (auto x : checkLines) {
            ll ans = query(x);
            if (ans == -1) {
                continue;
            }
            result = min(result, ans);
        }
        if (result == INF) {
            result = -1;
        }
        cout << result;
    }
}

namespace sub6 {
    ll cnt, result = INF;
    pair <ll, ll> a[N];

    vector < pair <ll, ll> > checkLines;
    ll maxx;

    void compress() {
        vector <ll> c;
        for (int i = 1; i <= n; i++) {
            c.push_back(a[i].first);
            c.push_back(a[i].second);
        }
        for (auto x : checkLines) {
            c.push_back(x.first);
            c.push_back(x.second);
        }
        sort(c.begin(), c.end());
        c.erase(unique(c.begin(), c.end()), c.end());
        for (int i = 1; i <= n; i++) {
            a[i].first = lower_bound(c.begin(), c.end(), a[i].first) - c.begin() + 1;
            a[i].second = lower_bound(c.begin(), c.end(), a[i].second) - c.begin() + 1;
            maxx = max(maxx, a[i].first);
            maxx = max(maxx, a[i].second);
        }
        for (auto &x : checkLines) {
            x.first = lower_bound(c.begin(), c.end(), x.first) - c.begin() + 1;
            x.second = lower_bound(c.begin(), c.end(), x.second) - c.begin() + 1;
            maxx = max(maxx, x.first);
            maxx = max(maxx, x.second);
        }
    }

    ll maxR[N], up[N][26];

    void solve() {
        checkLines.push_back({0, m - 1});
        for (int i = 1; i <= n; i++) {
            if (s[i] > e[i]) {
                checkLines.push_back({e[i], e[i] + m - 1});
                e[i] += m;
            }
            a[i] = {s[i], e[i] - 1};
        }
        compress();

        for (int i = 1; i <= n; i++) {
            maxR[a[i].first] = a[i].second;
        }
        for (int i = 1; i <= maxx; i++) {
            maxR[i] = max(maxR[i - 1], maxR[i]);
        }
        up[maxx][0] = maxx;
        for (int i = maxx - 1; i >= 1; i--) {
            if (maxR[i] != maxR[i + 1]) {
                up[i][0] = i;
            }
            else {
                up[i][0] = up[i + 1][0];
            }
        }
        for (int j = 1; j <= 20; j++) {
            for (int i = 1; i <= maxx; i++) {
                if (1 <= up[i][j - 1] && up[i][j - 1] <= maxx) {
                    up[i][j] = up[up[i][j - 1] + 1][j - 1];
                }
            }
        }
        for (auto x : checkLines) {
            int u = x.first, v = x.second, ans = 0;
            while (u <= v) {
                int k = -1;
                for (int i = 20; i >= 0; i--) {
                    if (up[u][i] == 0) {
                        continue;
                    }
                    if (up[u][i] < v) {
                        k = i;
                        break;
                    }
                }
                if (k == -1) {
                    break;
                }
                u = up[u][k] + 1;
                ans += (1 << k);
            }
            ans++;
            result = min(result, 1LL * ans);
        }
        if (result > n) {
            result = -1;
        }
        cout << result;
    }
}

int main() {
    ios_base :: sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }

    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> s[i] >> e[i];
    }
    if (n <= 5000) {
        sub3 :: solve();
    }
    else {
        sub6 :: solve();
    }

    return 0;
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:168:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  168 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:169:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  169 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...