답안 #1033828

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1033828 2024-07-25T06:46:52 Z 정민찬(#10972) Fire (BOI24_fire) C++17
100 / 100
249 ms 100156 KB
#include <bits/stdc++.h>

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

struct SegmentTree{
    vector<ll> tree;
    void init(ll n) {
        ll sz = 1 << __lg(n-1) + 2;
        tree.assign(sz, -1);
    }
    void update(ll node, ll s, ll e, ll tar, ll val) {
        if (s > tar || tar > e) return;
        if (s == e) {
            tree[node] = val;
            return;
        }
        ll 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]);
    }
    ll query(ll node, ll s, ll e, ll l, ll r) {
        if (l > e || s > r) return -1;
        if (l <= s && e <= r) return tree[node];
        ll 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);
    ll n, m;
    cin >> n >> m;
    vector<pair<ll,ll>> a(n);
    for (ll 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<ll,ll> &u, pair<ll,ll> &v) {
        if (u.first == v.first) return u.second > v.second;
        return u.first < v.first;
    });
    vector<pair<ll,ll>> t = {a[0]};
    ll mx = a[0].second;
    for (ll i=1; i<n; i++) {
        if (a[i].second > mx) {
            t.push_back(a[i]);
            mx = a[i].second;
        }
    }
    a.clear();
    for (ll i=0; i<t.size(); i++) {
        if (t[i].second > mx - m)
            a.push_back(t[i]);
    }
    n = a.size();
    vector<ll> d;
    for (ll 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 (ll 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;
    }
    ll M = d.size();
    seg.init(M);
    for (ll i=0; i<n; i++) {
        seg.update(1, 1, M, a[i].first, i);
    }
    vector<vector<ll>> r(n, vector<ll>(20)), cnt(n, vector<ll>(20));
    for (ll i=0; i<n; i++) {
        if (d[a[i].second-1] >= m) {
            ll 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 (ll lv=1; lv<20; lv++) {
        for (ll 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];
        }
    }
    ll ans = 1e9;
    for (ll i=0; i<n; i++) {
        if (cnt[i][0] == 0) continue;
        ll cur = 0;
        ll x = i, y = 0;
        for (ll lv=19; lv>=0; lv--) {
            if (y + cnt[x][lv] < n) {
                cur += (1 << lv);
                y += cnt[x][lv];
                x = r[x][lv];
            }
        }
        if (y + cnt[x][0] >= n)
            ans = min(ans, cur + 1);
    }
    if (ans > n) cout << "-1\n";
    else cout << ans << '\n';   
}

Compilation message

Main.cpp: In member function 'void SegmentTree::init(ll)':
Main.cpp:11:32: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   11 |         ll sz = 1 << __lg(n-1) + 2;
      |                      ~~~~~~~~~~^~~
Main.cpp: In member function 'void SegmentTree::update(ll, ll, ll, ll, ll)':
Main.cpp:20:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   20 |         ll mid = s + e >> 1;
      |                  ~~^~~
Main.cpp: In member function 'll SegmentTree::query(ll, ll, ll, ll, ll)':
Main.cpp:28:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   28 |         ll mid = s + e >> 1;
      |                  ~~^~~
Main.cpp: In function 'int main()':
Main.cpp:57:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     for (ll i=0; i<t.size(); i++) {
      |                  ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 348 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 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 452 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 348 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 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 452 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 0 ms 604 KB Output is correct
30 Correct 1 ms 460 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 0 ms 604 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 348 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 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 452 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 0 ms 604 KB Output is correct
30 Correct 1 ms 460 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 0 ms 604 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 604 KB Output is correct
39 Correct 2 ms 860 KB Output is correct
40 Correct 4 ms 2652 KB Output is correct
41 Correct 4 ms 2908 KB Output is correct
42 Correct 4 ms 2852 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 5 ms 2652 KB Output is correct
45 Correct 5 ms 2652 KB Output is correct
46 Correct 5 ms 2652 KB Output is correct
47 Correct 1 ms 604 KB Output is correct
48 Correct 1 ms 604 KB Output is correct
49 Correct 3 ms 1744 KB Output is correct
50 Correct 4 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 452 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 1 ms 464 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 5 ms 2776 KB Output is correct
16 Correct 5 ms 2652 KB Output is correct
17 Correct 2 ms 620 KB Output is correct
18 Correct 5 ms 2648 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 20 ms 4184 KB Output is correct
21 Correct 67 ms 18520 KB Output is correct
22 Correct 56 ms 15444 KB Output is correct
23 Correct 229 ms 94556 KB Output is correct
24 Correct 187 ms 80444 KB Output is correct
25 Correct 40 ms 6232 KB Output is correct
26 Correct 123 ms 53056 KB Output is correct
27 Correct 231 ms 95556 KB Output is correct
28 Correct 53 ms 13392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 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 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 5 ms 3160 KB Output is correct
13 Correct 7 ms 2648 KB Output is correct
14 Correct 5 ms 2652 KB Output is correct
15 Correct 22 ms 4188 KB Output is correct
16 Correct 232 ms 94804 KB Output is correct
17 Correct 234 ms 99564 KB Output is correct
18 Correct 233 ms 99556 KB Output is correct
19 Correct 241 ms 100156 KB Output is correct
20 Correct 249 ms 95040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 348 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 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 452 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 0 ms 604 KB Output is correct
30 Correct 1 ms 460 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 0 ms 604 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 604 KB Output is correct
39 Correct 2 ms 860 KB Output is correct
40 Correct 4 ms 2652 KB Output is correct
41 Correct 4 ms 2908 KB Output is correct
42 Correct 4 ms 2852 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 5 ms 2652 KB Output is correct
45 Correct 5 ms 2652 KB Output is correct
46 Correct 5 ms 2652 KB Output is correct
47 Correct 1 ms 604 KB Output is correct
48 Correct 1 ms 604 KB Output is correct
49 Correct 3 ms 1744 KB Output is correct
50 Correct 4 ms 2652 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 1 ms 344 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Correct 0 ms 348 KB Output is correct
56 Correct 0 ms 452 KB Output is correct
57 Correct 0 ms 348 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 1 ms 464 KB Output is correct
61 Correct 0 ms 348 KB Output is correct
62 Correct 0 ms 344 KB Output is correct
63 Correct 0 ms 604 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 5 ms 2776 KB Output is correct
66 Correct 5 ms 2652 KB Output is correct
67 Correct 2 ms 620 KB Output is correct
68 Correct 5 ms 2648 KB Output is correct
69 Correct 1 ms 604 KB Output is correct
70 Correct 20 ms 4184 KB Output is correct
71 Correct 67 ms 18520 KB Output is correct
72 Correct 56 ms 15444 KB Output is correct
73 Correct 229 ms 94556 KB Output is correct
74 Correct 187 ms 80444 KB Output is correct
75 Correct 40 ms 6232 KB Output is correct
76 Correct 123 ms 53056 KB Output is correct
77 Correct 231 ms 95556 KB Output is correct
78 Correct 53 ms 13392 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 0 ms 348 KB Output is correct
81 Correct 0 ms 348 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 1 ms 600 KB Output is correct
85 Correct 0 ms 344 KB Output is correct
86 Correct 0 ms 604 KB Output is correct
87 Correct 1 ms 604 KB Output is correct
88 Correct 1 ms 604 KB Output is correct
89 Correct 0 ms 604 KB Output is correct
90 Correct 5 ms 3160 KB Output is correct
91 Correct 7 ms 2648 KB Output is correct
92 Correct 5 ms 2652 KB Output is correct
93 Correct 22 ms 4188 KB Output is correct
94 Correct 232 ms 94804 KB Output is correct
95 Correct 234 ms 99564 KB Output is correct
96 Correct 233 ms 99556 KB Output is correct
97 Correct 241 ms 100156 KB Output is correct
98 Correct 249 ms 95040 KB Output is correct
99 Correct 81 ms 33108 KB Output is correct
100 Correct 54 ms 15728 KB Output is correct
101 Correct 238 ms 98724 KB Output is correct
102 Correct 193 ms 84032 KB Output is correct
103 Correct 233 ms 96228 KB Output is correct
104 Correct 64 ms 20052 KB Output is correct
105 Correct 104 ms 52556 KB Output is correct