Submission #1033785

# Submission time Handle Problem Language Result Execution time Memory
1033785 2024-07-25T06:15:56 Z 정민찬(#10972) Fire (BOI24_fire) C++17
0 / 100
1 ms 452 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;

struct SegmentTree{
    vector<int> tree;
    void init(int n) {
        int sz = 1 << __lg(n-1) + 2;
        tree.assign(sz, -1);
    }
    void update(int node, int s, int e, int tar, int val) {
        if (s > tar || tar > e) return;
        if (s == e) {
            tree[node] = val;
            return;
        }
        int mid = s + e >> 1;
        update(node*2, s, mid, tar, val);
        update(node*2+1, mid+1, e, tar, val);
        tree[node] = max(tree[node*2], tree[node*2+1]);
    }
    int query(int node, int s, int e, int l, int r) {
        if (l > e || s > r) return -1;
        if (l <= s && e <= r) return tree[node];
        int mid = s + e >> 1;
        return max(query(node*2, s, mid, l, r), query(node*2+1, mid+1, e, l, r));
    }
};

SegmentTree seg;

int main() {
    ios_base :: sync_with_stdio(false); cin.tie(NULL);
    int n, m;
    cin >> n >> m;
    vector<pair<int,int>> a(n);
    for (int i=0; i<n; i++) {
        cin >> a[i].first >> a[i].second;
        if (a[i].first > a[i].second) a[i].second += m;
    }
    sort(a.begin(), a.end(), [&] (pair<int,int> &u, pair<int,int> &v) {
        if (u.first == v.first) return u.second > v.second;
        return u.first < v.first;
    });
    vector<pair<int,int>> t = {a[0]};
    int mx = a[0].second;
    for (int i=1; i<n; i++) {
        if (a[i].second > mx) {
            t.push_back(a[i]);
            mx = a[i].second;
        }
    }
    a = t;
    n = a.size();
    vector<int> d;
    for (int i=0; i<n; i++) {
        d.push_back(a[i].first);
        d.push_back(a[i].second);
    }
    sort(d.begin(), d.end());
    d.erase(unique(d.begin(), d.end()), d.end());
    for (int i=0; i<n; i++) {
        a[i].first = lower_bound(d.begin(), d.end(), a[i].first) - d.begin() + 1;
        a[i].second = lower_bound(d.begin(), d.end(), a[i].second) - d.begin() + 1;
    }
    int M = d.size();
    seg.init(M);
    for (int i=0; i<n; i++) {
        seg.update(1, 1, M, a[i].first, i);
    }
    vector<vector<int>> r(n, vector<int>(20)), cnt(n, vector<int>(20));
    for (int i=0; i<n; i++) {
        if (d[a[i].second-1] >= m) {
            int idx = upper_bound(d.begin(), d.end(), d[a[i].second-1] - m) - d.begin();
            r[i][0] = seg.query(1, 1, M, 1, idx);
            if (r[i][0] == -1) {
                r[i][0] = seg.query(1, 1, M, a[i].first, M);
            }
        }
        else
            r[i][0] = seg.query(1, 1, M, a[i].first, a[i].second);
        if (r[i][0] == -1) r[i][0] = i;
        cnt[i][0] = (r[i][0] - i + n) % n;
    }
    for (int lv=1; lv<20; lv++) {
        for (int i=0; i<n; i++) {
            r[i][lv] = r[r[i][lv-1]][lv-1];
            cnt[i][lv] = cnt[i][lv-1] + cnt[r[i][lv-1]][lv-1];
        }
    }
    int ans = 1e9;
    for (int i=0; i<n; i++) {
        if (cnt[i][0] == 0) continue;
        int cur = 0;
        int x = i, y = 0;
        for (int lv=19; lv>=0; lv--) {
            if (y + cnt[x][lv] >= n || (a[i].first <= a[r[x][lv]].second && a[r[x][lv]].second <= a[i].second) || (d[a[r[x][lv]].second-1] <= d[a[i].second-1]-m)) {

            }
            else {
                cur += (1 << lv);
                x = r[x][lv];
                y += cnt[x][lv];
            }
        }
        ans = min(ans, cur + 2);
    }
    if (ans > n) cout << "-1\n";
    else cout << ans << '\n';   
}

Compilation message

Main.cpp: In member function 'void SegmentTree::init(int)':
Main.cpp:11:33: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   11 |         int sz = 1 << __lg(n-1) + 2;
      |                       ~~~~~~~~~~^~~
Main.cpp: In member function 'void SegmentTree::update(int, int, int, int, int)':
Main.cpp:20:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   20 |         int mid = s + e >> 1;
      |                   ~~^~~
Main.cpp: In member function 'int SegmentTree::query(int, int, int, int, int)':
Main.cpp:28:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   28 |         int mid = s + e >> 1;
      |                   ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Incorrect 0 ms 348 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Incorrect 0 ms 348 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Incorrect 0 ms 348 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 452 KB Output is correct
10 Incorrect 0 ms 348 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 452 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Incorrect 1 ms 348 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Incorrect 0 ms 348 KB Output isn't correct
12 Halted 0 ms 0 KB -