Submission #344162

# Submission time Handle Problem Language Result Execution time Memory
344162 2021-01-05T08:01:14 Z Nurlykhan Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++17
100 / 100
1694 ms 73712 KB
#include <bits/stdc++.h>

using namespace std;

const int N = (int)1e6 + 10;
const int K = 450;

int n, m;
int w[N];

int ans[N];
int L[N], R[N], k[N];
vector<int> query[N];

int suff_w[N], suff_mood[N];

void solve(int l = 0, int r = n - 1) {
    if (l == r)
        return;
    int mid = (l + r) / 2;

    // 5 4 100
    vector<int> s; // TODO: replace set with fenwick
    suff_w[mid + 1] = suff_mood[mid + 1] = 0;
    for (int i = mid; i >= l; i--) {
        suff_w[i] = max(suff_w[i + 1], w[i]);
        suff_mood[i] = suff_mood[i + 1];

        while (!s.empty() && s.back() < w[i]) {
            suff_mood[i] = max(suff_mood[i], s.back() + w[i]);
            s.pop_back();
        }

        if (s.empty() || s.back() > w[i])
            s.push_back(w[i]);
    }

    s.clear();
    int mx = 0, mxx = 0;
    for (int i = mid + 1; i <= r; i++) {
        if (mxx <= w[i]) {
            mxx = w[i];
            s.push_back(w[i]);
        } else {
            mx = max(mx, mxx + w[i]);
        }


        // TODO: optimize iteration over queries
        for (auto j : query[i]) {
            if (L[j] >= l && L[j] <= mid) {
                int cur_mx = max(suff_mood[L[j]], mx);

                int tmp = suff_w[L[j]];
                int it = lower_bound(s.begin(), s.end(), tmp) - s.begin() - 1;
                if (it >= 0) {
                    cur_mx = max(cur_mx, s[it] + tmp);
                }

                if (k[j] >= cur_mx) {
                    ans[j] = 1;
                } else {
                    ans[j] = 0;
                }
            }
        }
    }

    solve(l, mid);
    solve(mid + 1, r);
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++) {
        scanf("%d", &w[i]);
    }

    for (int i = 0; i < m; i++) {
        scanf("%d%d%d", &L[i], &R[i], &k[i]);
        --L[i], --R[i];
        query[R[i]].push_back(i);
    }

    solve();

    for (int i = 0; i < m; i++) {
        if (L[i] == R[i]) ans[i] = 1;
        printf("%d\n", ans[i]);
    }
    return 0;
}

Compilation message

sortbooks.cpp: In function 'int main()':
sortbooks.cpp:74:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   74 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
sortbooks.cpp:76:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   76 |         scanf("%d", &w[i]);
      |         ~~~~~^~~~~~~~~~~~~
sortbooks.cpp:80:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   80 |         scanf("%d%d%d", &L[i], &R[i], &k[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23916 KB Output is correct
2 Correct 16 ms 23788 KB Output is correct
3 Correct 19 ms 23916 KB Output is correct
4 Correct 18 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Correct 16 ms 23916 KB Output is correct
7 Correct 16 ms 23916 KB Output is correct
8 Correct 17 ms 23916 KB Output is correct
9 Correct 19 ms 23916 KB Output is correct
10 Correct 17 ms 23916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23916 KB Output is correct
2 Correct 16 ms 23788 KB Output is correct
3 Correct 19 ms 23916 KB Output is correct
4 Correct 18 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Correct 16 ms 23916 KB Output is correct
7 Correct 16 ms 23916 KB Output is correct
8 Correct 17 ms 23916 KB Output is correct
9 Correct 19 ms 23916 KB Output is correct
10 Correct 17 ms 23916 KB Output is correct
11 Correct 22 ms 23916 KB Output is correct
12 Correct 19 ms 24044 KB Output is correct
13 Correct 20 ms 24044 KB Output is correct
14 Correct 21 ms 24044 KB Output is correct
15 Correct 22 ms 24044 KB Output is correct
16 Correct 20 ms 24044 KB Output is correct
17 Correct 20 ms 24044 KB Output is correct
18 Correct 20 ms 24044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1524 ms 71164 KB Output is correct
2 Correct 1535 ms 71392 KB Output is correct
3 Correct 1528 ms 72044 KB Output is correct
4 Correct 1514 ms 71472 KB Output is correct
5 Correct 1694 ms 73712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 112 ms 28396 KB Output is correct
2 Correct 98 ms 27756 KB Output is correct
3 Correct 110 ms 28424 KB Output is correct
4 Correct 128 ms 28612 KB Output is correct
5 Correct 117 ms 28708 KB Output is correct
6 Correct 84 ms 27652 KB Output is correct
7 Correct 86 ms 27652 KB Output is correct
8 Correct 109 ms 28600 KB Output is correct
9 Correct 57 ms 26088 KB Output is correct
10 Correct 107 ms 28652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23916 KB Output is correct
2 Correct 16 ms 23788 KB Output is correct
3 Correct 19 ms 23916 KB Output is correct
4 Correct 18 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Correct 16 ms 23916 KB Output is correct
7 Correct 16 ms 23916 KB Output is correct
8 Correct 17 ms 23916 KB Output is correct
9 Correct 19 ms 23916 KB Output is correct
10 Correct 17 ms 23916 KB Output is correct
11 Correct 22 ms 23916 KB Output is correct
12 Correct 19 ms 24044 KB Output is correct
13 Correct 20 ms 24044 KB Output is correct
14 Correct 21 ms 24044 KB Output is correct
15 Correct 22 ms 24044 KB Output is correct
16 Correct 20 ms 24044 KB Output is correct
17 Correct 20 ms 24044 KB Output is correct
18 Correct 20 ms 24044 KB Output is correct
19 Correct 272 ms 33004 KB Output is correct
20 Correct 260 ms 33132 KB Output is correct
21 Correct 220 ms 31340 KB Output is correct
22 Correct 220 ms 31340 KB Output is correct
23 Correct 217 ms 31340 KB Output is correct
24 Correct 187 ms 31648 KB Output is correct
25 Correct 234 ms 31704 KB Output is correct
26 Correct 221 ms 32372 KB Output is correct
27 Correct 227 ms 32296 KB Output is correct
28 Correct 251 ms 32540 KB Output is correct
29 Correct 254 ms 33508 KB Output is correct
30 Correct 267 ms 33500 KB Output is correct
31 Correct 253 ms 33388 KB Output is correct
32 Correct 285 ms 33380 KB Output is correct
33 Correct 256 ms 33512 KB Output is correct
34 Correct 184 ms 31396 KB Output is correct
35 Correct 189 ms 31400 KB Output is correct
36 Correct 207 ms 31392 KB Output is correct
37 Correct 183 ms 31272 KB Output is correct
38 Correct 180 ms 31272 KB Output is correct
39 Correct 233 ms 33236 KB Output is correct
40 Correct 198 ms 32168 KB Output is correct
41 Correct 224 ms 33748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23916 KB Output is correct
2 Correct 16 ms 23788 KB Output is correct
3 Correct 19 ms 23916 KB Output is correct
4 Correct 18 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Correct 16 ms 23916 KB Output is correct
7 Correct 16 ms 23916 KB Output is correct
8 Correct 17 ms 23916 KB Output is correct
9 Correct 19 ms 23916 KB Output is correct
10 Correct 17 ms 23916 KB Output is correct
11 Correct 22 ms 23916 KB Output is correct
12 Correct 19 ms 24044 KB Output is correct
13 Correct 20 ms 24044 KB Output is correct
14 Correct 21 ms 24044 KB Output is correct
15 Correct 22 ms 24044 KB Output is correct
16 Correct 20 ms 24044 KB Output is correct
17 Correct 20 ms 24044 KB Output is correct
18 Correct 20 ms 24044 KB Output is correct
19 Correct 1524 ms 71164 KB Output is correct
20 Correct 1535 ms 71392 KB Output is correct
21 Correct 1528 ms 72044 KB Output is correct
22 Correct 1514 ms 71472 KB Output is correct
23 Correct 1694 ms 73712 KB Output is correct
24 Correct 112 ms 28396 KB Output is correct
25 Correct 98 ms 27756 KB Output is correct
26 Correct 110 ms 28424 KB Output is correct
27 Correct 128 ms 28612 KB Output is correct
28 Correct 117 ms 28708 KB Output is correct
29 Correct 84 ms 27652 KB Output is correct
30 Correct 86 ms 27652 KB Output is correct
31 Correct 109 ms 28600 KB Output is correct
32 Correct 57 ms 26088 KB Output is correct
33 Correct 107 ms 28652 KB Output is correct
34 Correct 272 ms 33004 KB Output is correct
35 Correct 260 ms 33132 KB Output is correct
36 Correct 220 ms 31340 KB Output is correct
37 Correct 220 ms 31340 KB Output is correct
38 Correct 217 ms 31340 KB Output is correct
39 Correct 187 ms 31648 KB Output is correct
40 Correct 234 ms 31704 KB Output is correct
41 Correct 221 ms 32372 KB Output is correct
42 Correct 227 ms 32296 KB Output is correct
43 Correct 251 ms 32540 KB Output is correct
44 Correct 254 ms 33508 KB Output is correct
45 Correct 267 ms 33500 KB Output is correct
46 Correct 253 ms 33388 KB Output is correct
47 Correct 285 ms 33380 KB Output is correct
48 Correct 256 ms 33512 KB Output is correct
49 Correct 184 ms 31396 KB Output is correct
50 Correct 189 ms 31400 KB Output is correct
51 Correct 207 ms 31392 KB Output is correct
52 Correct 183 ms 31272 KB Output is correct
53 Correct 180 ms 31272 KB Output is correct
54 Correct 233 ms 33236 KB Output is correct
55 Correct 198 ms 32168 KB Output is correct
56 Correct 224 ms 33748 KB Output is correct
57 Correct 1486 ms 71536 KB Output is correct
58 Correct 1498 ms 71056 KB Output is correct
59 Correct 1364 ms 66924 KB Output is correct
60 Correct 1374 ms 66764 KB Output is correct
61 Correct 1373 ms 66720 KB Output is correct
62 Correct 1365 ms 66668 KB Output is correct
63 Correct 1000 ms 63504 KB Output is correct
64 Correct 996 ms 63500 KB Output is correct
65 Correct 1427 ms 68432 KB Output is correct
66 Correct 1417 ms 68400 KB Output is correct
67 Correct 1446 ms 68584 KB Output is correct
68 Correct 1556 ms 72988 KB Output is correct
69 Correct 1545 ms 72872 KB Output is correct
70 Correct 1532 ms 72324 KB Output is correct
71 Correct 1539 ms 72368 KB Output is correct
72 Correct 1538 ms 72240 KB Output is correct
73 Correct 1020 ms 62872 KB Output is correct
74 Correct 982 ms 62868 KB Output is correct
75 Correct 990 ms 63228 KB Output is correct
76 Correct 984 ms 63284 KB Output is correct
77 Correct 1002 ms 63136 KB Output is correct
78 Correct 1420 ms 72800 KB Output is correct
79 Correct 1043 ms 62256 KB Output is correct
80 Correct 1382 ms 73076 KB Output is correct