Submission #993368

# Submission time Handle Problem Language Result Execution time Memory
993368 2024-06-05T14:13:43 Z vladilius Fire (BOI24_fire) C++17
40 / 100
27 ms 4088 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define ins insert

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n, m; cin>>n>>m;
    vector<int> l(n + 1), r(n + 1);
    for (int i = 1; i <= n; i++){
        cin>>l[i]>>r[i];
        if (!r[i]) r[i] = m;
        else r[i]--;
        if (l[i] > r[i]){
            r[i] += m;
        }
    }
    
    vector<pii> all;
    for (int i = 1; i <= n; i++){
        all.pb({l[i], r[i]});
    }
    sort(all.begin(), all.end());
    vector<pii> tmp;
    int i = 0;
    while (i < n){
        int j = i;
        while (j < n && all[i].ff == all[j].ff){
            j++;
        }
        tmp.pb({all[j - 1].ss, all[j - 1].ff});
        i = j;
    }
    all = tmp;
    sort(all.begin(), all.end());
    tmp.clear();
    i = 0;
    while (i < all.size()){
        int j = i;
        while (j < all.size() && all[i].ff == all[j].ff){
            j++;
        }
        tmp.pb({all[i].ss, all[i].ff});
        i = j;
    }
    all = tmp;
    
    
    sort(all.begin(), all.end());
    n = (int) all.size();
    vector<int> log(n + 1);
    for (int i = 2; i <= n; i++){
        log[i] = log[i / 2] + 1;
    }
    const int lg = log[n];
    vector<vector<pii>> sp(n, vector<pii>(lg + 1));
    for (int i = 0; i < n; i++){
        sp[i][0] = {all[i].ss, i};
    }
    for (int k = 1; k <= lg; k++){
        for (int i = 0; i + (1 << k) <= n; i++){
            sp[i][k] = max(sp[i][k - 1], sp[i + (1 << (k - 1))][k - 1]);
        }
    }
    
    auto get = [&](int l, int r){
        int k = log[r - l + 1];
        return max(sp[l][k], sp[r - (1 << k) + 1][k]);
    };
    
    vector<int> ls;
    for (int i = 0; i < n; i++){
        ls.pb(all[i].ff);
    }
    vector<int> :: iterator it1, it2;
    
    vector<int> g(n);
    for (int i = 0; i < n; i++){
        auto &[l, r] = all[i];
        it1 = lower_bound(ls.begin(), ls.end(), l);
        it2 = upper_bound(ls.begin(), ls.end(), r + 1); it2--;
        int lb = max(i + 1, (int) (it1 - ls.begin())), rb = (int) (it2 - ls.begin());
        if (lb > rb){
            g[i] = -1;
            continue;
        }
        g[i] = get(lb, rb).ss;
    }
    
    int out = n + 1;
    for (int i = 0; i < n; i++){
        int j = i, cnt = 1, rr = all[i].ff + m - 1;
        while (j != -1 && all[j].ss < rr){
            j = g[j];
            cnt++;
        }
        if (j != -1) out = min(out, cnt);
    }
    
    cout<<((out > n) ? -1 : out)<<"\n";
    
    /*

    vector<vector<int>> pw(n, vector<int>(lg + 1, -1));
    for (int i = 0; i < n; i++){
        pw[i][0] = g[i];
    }
    for (int k = 1; k <= lg; k++){
        for (int i = 0; i + (1 << k) <= n; i++){
            if (pw[i][k - 1] == -1) continue;
            pw[i][k] = pw[pw[i][k - 1]][k - 1];
        }
    }
    
    int ans = n + 1;
    for (int i = 0; i < n; i++){
        int rr = all[i].ff + m - 1, j = i, out = 1;
        for (int k = lg; k >= 0; k--){
            int v = pw[j][k];
            if (v == -1) continue;
            if (all[v].ss < rr){
                j = v;
                out += (1 << k);
            }
        }
        
        if (all[j].ss >= rr) ans = min(ans, out);
        else {
            j = g[j];
            if (j != -1 && all[j].ss >= rr){
                ans = min(ans, out + 1);
            }
        }
    }
    cout<<((ans > n) ? -1 : ans)<<"\n";
     */
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:45:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     while (i < all.size()){
      |            ~~^~~~~~~~~~~~
Main.cpp:47:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |         while (j < all.size() && all[i].ff == all[j].ff){
      |                ~~^~~~~~~~~~~~
# 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 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 348 KB Output is correct
8 Correct 0 ms 352 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 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 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 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
# 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 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 348 KB Output is correct
8 Correct 0 ms 352 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 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 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 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 344 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
# 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 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 348 KB Output is correct
8 Correct 0 ms 352 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 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 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 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 344 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 4 ms 1116 KB Output is correct
40 Correct 23 ms 1328 KB Output is correct
41 Correct 3 ms 1116 KB Output is correct
42 Correct 27 ms 1260 KB Output is correct
43 Correct 3 ms 856 KB Output is correct
44 Correct 27 ms 1116 KB Output is correct
45 Correct 27 ms 1328 KB Output is correct
46 Correct 27 ms 1116 KB Output is correct
47 Correct 2 ms 1116 KB Output is correct
48 Correct 3 ms 1116 KB Output is correct
49 Correct 8 ms 1116 KB Output is correct
50 Correct 6 ms 1112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 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 1 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 1 ms 344 KB Output is correct
15 Correct 3 ms 1368 KB Output is correct
16 Correct 27 ms 1116 KB Output is correct
17 Correct 2 ms 856 KB Output is correct
18 Correct 27 ms 1116 KB Output is correct
19 Correct 3 ms 1116 KB Output is correct
20 Incorrect 21 ms 4088 KB Output isn't correct
21 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 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 1 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 3 ms 1116 KB Output is correct
13 Correct 27 ms 1116 KB Output is correct
14 Correct 6 ms 1112 KB Output is correct
15 Incorrect 23 ms 4052 KB Output isn't correct
16 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 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 348 KB Output is correct
8 Correct 0 ms 352 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 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 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 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 344 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 4 ms 1116 KB Output is correct
40 Correct 23 ms 1328 KB Output is correct
41 Correct 3 ms 1116 KB Output is correct
42 Correct 27 ms 1260 KB Output is correct
43 Correct 3 ms 856 KB Output is correct
44 Correct 27 ms 1116 KB Output is correct
45 Correct 27 ms 1328 KB Output is correct
46 Correct 27 ms 1116 KB Output is correct
47 Correct 2 ms 1116 KB Output is correct
48 Correct 3 ms 1116 KB Output is correct
49 Correct 8 ms 1116 KB Output is correct
50 Correct 6 ms 1112 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 348 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 348 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 0 ms 348 KB Output is correct
61 Correct 1 ms 348 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 1 ms 344 KB Output is correct
65 Correct 3 ms 1368 KB Output is correct
66 Correct 27 ms 1116 KB Output is correct
67 Correct 2 ms 856 KB Output is correct
68 Correct 27 ms 1116 KB Output is correct
69 Correct 3 ms 1116 KB Output is correct
70 Incorrect 21 ms 4088 KB Output isn't correct
71 Halted 0 ms 0 KB -