Submission #1281432

#TimeUsernameProblemLanguageResultExecution timeMemory
1281432red_soulsFire (BOI24_fire)C++17
40 / 100
214 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);
        }
    }

    /*
    struct node {
        ll val = -INF, lazy = -INF;

        node operator + (const node &other) const {
            node ans;
            ans.val = max(val, other.val);
            return ans;
        }
    };

    node segmentTree[N << 2];

    void down(int k) {
        ll t = segmentTree[k].lazy;

        segmentTree[k << 1].val = max(segmentTree[k << 1].val, t);
        segmentTree[k << 1].lazy = max(segmentTree[k << 1].lazy, t);

        segmentTree[k << 1 | 1].val = max(segmentTree[k << 1 | 1].val, t);
        segmentTree[k << 1 | 1].lazy = max(segmentTree[k << 1 | 1].lazy, t);

        segmentTree[k].lazy = -INF;
    }

    void update(int k, int l, int r, int x, int y, ll val) {
        if (l > y || x > r) {
            return;
        }
        if (l >= x && r <= y) {
            segmentTree[k].val = max(segmentTree[k].val, val);
            segmentTree[k].lazy = max(segmentTree[k].lazy, val);
            return;
        }
        int mid = l + r >> 1;
        down(k);
        update(k << 1, l, mid, x, y, val);
        update(k << 1 | 1, mid + 1, r, x, y, val);
        segmentTree[k] = segmentTree[k << 1] + segmentTree[k << 1 | 1];
    }

    node get(int k, int l, int r, int x, int y) {
        if (l > y || x > r) {
            node ans;
            return ans;
        }
        if (l >= x && r <= y) {
            return segmentTree[k];
        }
        int mid = l + r >> 1;
        down(k);
        return get(k << 1, l, mid, x, y) + get(k << 1 | 1, mid + 1, r, x, y);
    }
    */

    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]);
        }
        for (int i = maxx; 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:222:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  222 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:223:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  223 |         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...