Submission #1029099

# Submission time Handle Problem Language Result Execution time Memory
1029099 2024-07-20T12:24:38 Z atom Osumnjičeni (COCI21_osumnjiceni) C++17
110 / 110
495 ms 38336 KB
#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;

#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif

struct LazySegmentTree {
    vector <int> lzy, f;
    int n;
    LazySegmentTree(int _n) {
        n = _n;
        lzy.resize(n * 4 + 5, 0);
        f.resize(n * 4 + 5, 0);
    }

    int merge(int x, int y) { return max(x, y); }

    void push(int x, int l, int r) {
        if (lzy[x] == 0) return;
        int val = lzy[x];

        f[x << 1] += val;
        lzy[x << 1] += val;

        f[x << 1 | 1] += val;
        lzy[x << 1 | 1] += val;

        lzy[x] = 0;
        return;
    }

    void upd(int x, int l, int r, int u, int v, int val) {
        if (l > v || r < u) return;
        if (u <= l && r <= v) {
            f[x] += val;
            lzy[x] += val;
            return;
        }
        int m = (l + r) / 2;
        push(x, l, r);
        upd(x << 1, l, m, u, v, val);
        upd(x << 1 | 1, m + 1, r, u, v, val);
        f[x] = merge(f[x << 1], f[x << 1 | 1]);
    }

    void upd(int l, int r, int val) { upd(1, 1, n, l, r, val); }

    int qry(int x, int l, int r, int u, int v) {
        if (l > v || r < u) return 0;
        if (u <= l && r <= v) return f[x];
        int m = (l + r) / 2;
        push(x, l, r);
        int ql = qry(x << 1, l, m, u, v);
        int qr = qry(x << 1 | 1, m + 1, r, u, v);
        return merge(ql, qr); 
    }

    int qry(int l, int r) { return qry(1, 1, n, l, r); }
};

const int LG = 18;

signed main() {
    cin.tie(0) -> sync_with_stdio(0);
    
    #ifdef JASPER
        freopen("in1", "r", stdin);
    #endif

    vector <int> vals;
    int n, q;
    cin >> n;
    vector <pair <int, int>> segs(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> segs[i].first >> segs[i].second;
        vals.push_back(segs[i].first);
        vals.push_back(segs[i].second);
    }

    sort(vals.begin(), vals.end());
    vals.resize(unique(vals.begin(), vals.end()) - vals.begin());

    for (int i = 1; i <= n; ++i) {
        segs[i].first = (int) (lower_bound(vals.begin(), vals.end(), segs[i].first) - vals.begin() + 1);
        segs[i].second = (int) (lower_bound(vals.begin(), vals.end(), segs[i].second) - vals.begin() + 1);
    }

    LazySegmentTree st((int) vals.size());

    vector <int> nxt(n + 1, 0);
    int j = 1;
    for (int i = 1; i <= n; ++i) {
        while (j <= n && st.qry(segs[j].first, segs[j].second) == 0) {
            st.upd(segs[j].first, segs[j].second, 1);
            ++j;
        }
        nxt[i] = j;
        st.upd(segs[i].first, segs[i].second, -1);
    }

    debug(nxt);
    vector <vector <int>> up(LG, vector <int> (n + 5, 0));
    // up(i, j) = nxt(..nxt(i)), use nxt() j times
    for (int i = 1; i <= n; ++i)
        up[0][i] = nxt[i];
    up[0][n + 1] = n + 1;

    for (int _j = 1; _j < LG; ++_j)
        for (int i = 1; i <= n + 1; ++i)
            up[_j][i] = up[_j - 1][up[_j - 1][i]];

    cin >> q;
    for (int _q = 1; _q <= q; ++_q) {
        int l, r;
        cin >> l >> r;
        
        int res = 1;
        for (int i = LG - 1; i >= 0; --i) {
            if (up[i][l] <= r) {
                l = up[i][l];
                res += (1 << i);
            }
        }
        cout << res << "\n";
    } 

    
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:9:20: warning: statement has no effect [-Wunused-value]
    9 | #define debug(...) 166
      |                    ^~~
Main.cpp:106:5: note: in expansion of macro 'debug'
  106 |     debug(nxt);
      |     ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 375 ms 33840 KB Output is correct
2 Correct 357 ms 33480 KB Output is correct
3 Correct 391 ms 33496 KB Output is correct
4 Correct 413 ms 33996 KB Output is correct
5 Correct 441 ms 35472 KB Output is correct
6 Correct 50 ms 20800 KB Output is correct
7 Correct 72 ms 21200 KB Output is correct
8 Correct 79 ms 21264 KB Output is correct
9 Correct 71 ms 21812 KB Output is correct
10 Correct 68 ms 21012 KB Output is correct
11 Correct 261 ms 34076 KB Output is correct
12 Correct 267 ms 32132 KB Output is correct
13 Correct 260 ms 32048 KB Output is correct
14 Correct 257 ms 34156 KB Output is correct
15 Correct 267 ms 33224 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 45 ms 22292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 1372 KB Output is correct
2 Correct 7 ms 1116 KB Output is correct
3 Correct 8 ms 1348 KB Output is correct
4 Correct 12 ms 1312 KB Output is correct
5 Correct 10 ms 1288 KB Output is correct
6 Correct 3 ms 860 KB Output is correct
7 Correct 3 ms 880 KB Output is correct
8 Correct 3 ms 860 KB Output is correct
9 Correct 5 ms 1088 KB Output is correct
10 Correct 4 ms 860 KB Output is correct
11 Correct 7 ms 1116 KB Output is correct
12 Correct 5 ms 1116 KB Output is correct
13 Correct 5 ms 1232 KB Output is correct
14 Correct 5 ms 1240 KB Output is correct
15 Correct 6 ms 1296 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 2 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 1372 KB Output is correct
2 Correct 7 ms 1116 KB Output is correct
3 Correct 8 ms 1348 KB Output is correct
4 Correct 12 ms 1312 KB Output is correct
5 Correct 10 ms 1288 KB Output is correct
6 Correct 3 ms 860 KB Output is correct
7 Correct 3 ms 880 KB Output is correct
8 Correct 3 ms 860 KB Output is correct
9 Correct 5 ms 1088 KB Output is correct
10 Correct 4 ms 860 KB Output is correct
11 Correct 7 ms 1116 KB Output is correct
12 Correct 5 ms 1116 KB Output is correct
13 Correct 5 ms 1232 KB Output is correct
14 Correct 5 ms 1240 KB Output is correct
15 Correct 6 ms 1296 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 2 ms 860 KB Output is correct
18 Correct 58 ms 3924 KB Output is correct
19 Correct 51 ms 3876 KB Output is correct
20 Correct 85 ms 3844 KB Output is correct
21 Correct 52 ms 3736 KB Output is correct
22 Correct 74 ms 3788 KB Output is correct
23 Correct 56 ms 3288 KB Output is correct
24 Correct 50 ms 3448 KB Output is correct
25 Correct 69 ms 3664 KB Output is correct
26 Correct 67 ms 3356 KB Output is correct
27 Correct 56 ms 3308 KB Output is correct
28 Correct 33 ms 3144 KB Output is correct
29 Correct 40 ms 3412 KB Output is correct
30 Correct 33 ms 3272 KB Output is correct
31 Correct 41 ms 3228 KB Output is correct
32 Correct 34 ms 3408 KB Output is correct
33 Correct 0 ms 344 KB Output is correct
34 Correct 61 ms 3292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 395 ms 35528 KB Output is correct
2 Correct 410 ms 34660 KB Output is correct
3 Correct 366 ms 32300 KB Output is correct
4 Correct 342 ms 31944 KB Output is correct
5 Correct 386 ms 32968 KB Output is correct
6 Correct 44 ms 21064 KB Output is correct
7 Correct 51 ms 20940 KB Output is correct
8 Correct 49 ms 21056 KB Output is correct
9 Correct 53 ms 20764 KB Output is correct
10 Correct 65 ms 22460 KB Output is correct
11 Correct 221 ms 31776 KB Output is correct
12 Correct 244 ms 34932 KB Output is correct
13 Correct 215 ms 31692 KB Output is correct
14 Correct 291 ms 32696 KB Output is correct
15 Correct 297 ms 34792 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 43 ms 21448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 375 ms 33840 KB Output is correct
2 Correct 357 ms 33480 KB Output is correct
3 Correct 391 ms 33496 KB Output is correct
4 Correct 413 ms 33996 KB Output is correct
5 Correct 441 ms 35472 KB Output is correct
6 Correct 50 ms 20800 KB Output is correct
7 Correct 72 ms 21200 KB Output is correct
8 Correct 79 ms 21264 KB Output is correct
9 Correct 71 ms 21812 KB Output is correct
10 Correct 68 ms 21012 KB Output is correct
11 Correct 261 ms 34076 KB Output is correct
12 Correct 267 ms 32132 KB Output is correct
13 Correct 260 ms 32048 KB Output is correct
14 Correct 257 ms 34156 KB Output is correct
15 Correct 267 ms 33224 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 45 ms 22292 KB Output is correct
18 Correct 8 ms 1372 KB Output is correct
19 Correct 7 ms 1116 KB Output is correct
20 Correct 8 ms 1348 KB Output is correct
21 Correct 12 ms 1312 KB Output is correct
22 Correct 10 ms 1288 KB Output is correct
23 Correct 3 ms 860 KB Output is correct
24 Correct 3 ms 880 KB Output is correct
25 Correct 3 ms 860 KB Output is correct
26 Correct 5 ms 1088 KB Output is correct
27 Correct 4 ms 860 KB Output is correct
28 Correct 7 ms 1116 KB Output is correct
29 Correct 5 ms 1116 KB Output is correct
30 Correct 5 ms 1232 KB Output is correct
31 Correct 5 ms 1240 KB Output is correct
32 Correct 6 ms 1296 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 2 ms 860 KB Output is correct
35 Correct 58 ms 3924 KB Output is correct
36 Correct 51 ms 3876 KB Output is correct
37 Correct 85 ms 3844 KB Output is correct
38 Correct 52 ms 3736 KB Output is correct
39 Correct 74 ms 3788 KB Output is correct
40 Correct 56 ms 3288 KB Output is correct
41 Correct 50 ms 3448 KB Output is correct
42 Correct 69 ms 3664 KB Output is correct
43 Correct 67 ms 3356 KB Output is correct
44 Correct 56 ms 3308 KB Output is correct
45 Correct 33 ms 3144 KB Output is correct
46 Correct 40 ms 3412 KB Output is correct
47 Correct 33 ms 3272 KB Output is correct
48 Correct 41 ms 3228 KB Output is correct
49 Correct 34 ms 3408 KB Output is correct
50 Correct 0 ms 344 KB Output is correct
51 Correct 61 ms 3292 KB Output is correct
52 Correct 395 ms 35528 KB Output is correct
53 Correct 410 ms 34660 KB Output is correct
54 Correct 366 ms 32300 KB Output is correct
55 Correct 342 ms 31944 KB Output is correct
56 Correct 386 ms 32968 KB Output is correct
57 Correct 44 ms 21064 KB Output is correct
58 Correct 51 ms 20940 KB Output is correct
59 Correct 49 ms 21056 KB Output is correct
60 Correct 53 ms 20764 KB Output is correct
61 Correct 65 ms 22460 KB Output is correct
62 Correct 221 ms 31776 KB Output is correct
63 Correct 244 ms 34932 KB Output is correct
64 Correct 215 ms 31692 KB Output is correct
65 Correct 291 ms 32696 KB Output is correct
66 Correct 297 ms 34792 KB Output is correct
67 Correct 0 ms 348 KB Output is correct
68 Correct 43 ms 21448 KB Output is correct
69 Correct 459 ms 36484 KB Output is correct
70 Correct 495 ms 38336 KB Output is correct
71 Correct 414 ms 35540 KB Output is correct
72 Correct 493 ms 37216 KB Output is correct
73 Correct 493 ms 36812 KB Output is correct
74 Correct 153 ms 26444 KB Output is correct
75 Correct 174 ms 24740 KB Output is correct
76 Correct 161 ms 26340 KB Output is correct
77 Correct 161 ms 24524 KB Output is correct
78 Correct 154 ms 24780 KB Output is correct
79 Correct 307 ms 37632 KB Output is correct
80 Correct 304 ms 34760 KB Output is correct
81 Correct 278 ms 34496 KB Output is correct
82 Correct 287 ms 36408 KB Output is correct
83 Correct 289 ms 34760 KB Output is correct
84 Correct 0 ms 348 KB Output is correct
85 Correct 114 ms 24496 KB Output is correct