Submission #373140

# Submission time Handle Problem Language Result Execution time Memory
373140 2021-03-03T13:58:58 Z maksim1744 Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++17
100 / 100
1213 ms 72132 KB
/*
    author:  Maksim1744
    created: 03.03.2021 16:47:45
*/

#include "bits/stdc++.h"

using namespace std;

#define ll   long long
#define ld   long double

#define mp   make_pair
#define pb   push_back
#define eb   emplace_back

#define sum(a)     ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a)    (*min_element((a).begin(), (a).end()))
#define maxe(a)    (*max_element((a).begin(), (a).end()))
#define mini(a)    ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a)    ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

template<typename T>             vector<T>& operator--            (vector<T>& v){for (auto& i : v) --i;            return  v;}
template<typename T>             vector<T>& operator++            (vector<T>& v){for (auto& i : v) ++i;            return  v;}
template<typename T>             istream& operator>>(istream& is,  vector<T>& v){for (auto& i : v) is >> i;        return is;}
template<typename T>             ostream& operator<<(ostream& os,  vector<T>& v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator--           (pair<T, U> &p){--p.first; --p.second;            return  p;}
template<typename T, typename U> pair<T,U>& operator++           (pair<T, U> &p){++p.first; ++p.second;            return  p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p){is >> p.first >> p.second;        return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>& p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}

#ifdef HOME
#define SHOW_COLORS
#include "C:/C++ libs/print.cpp"
#else
#define show(...)     42
#define mclock        42
#define shows         42
#define debug if (false)
#endif

struct item {
    ll mx;

    template<typename T>
    void init(const T &t, int l, int r) {
        mx = t;
    }

    void update(const item &first, const item &second, int l, int r) {
        mx = max(first.mx, second.mx);
    }

    static item merge(const item &first, const item &second, int l, int r) {
        item res;
        res.update(first, second, l, r);  // careful with different lengths
        return res;
    }
};

string to_string(const item &i) {
    stringstream ss;
    ss << "[" << "]";
    return ss.str();
}
ostream& operator << (ostream &o, const item &i) {
    return o << to_string(i);
}

struct segtree {
    vector<item> tree;
    int n = 1;

    segtree(int n = 1) : n(n) {
        tree.resize(1 << (__lg(n - 1) + 2));
    }

    template<typename T>
    void build(const vector<T> &v, int i, int l, int r) {
        if (l == r) {
            tree[i].init(v[l], l, r);
            return;
        }
        int m = (l + r) >> 1;
        build(v, i * 2 + 1, l, m);
        build(v, i * 2 + 2, m + 1, r);
        tree[i].update(tree[i * 2 + 1], tree[i * 2 + 2], l, r);
    }

    template<typename T>
    void build(const vector<T> &v) {
        n = v.size();
        tree.resize(1 << (__lg(n - 1) + 2));
        build(v, 0, 0, n - 1);
    }

    item ask(int l, int r, int i, int vl, int vr) {
        if (l == vl && r == vr) {
            return tree[i];
        }
        int m = (vl + vr) >> 1;
        if (r <= m) {
            return ask(l, r, i * 2 + 1, vl, m);
        } else if (m < l) {
            return ask(l, r, i * 2 + 2, m + 1, vr);
        } else {
            return item::merge(ask(l, m, i * 2 + 1, vl, m), ask(m + 1, r, i * 2 + 2, m + 1, vr), l, r);
        }
    }

    item ask(int l, int r) {
        l = max(l, 0); r = min(r, n - 1);
        if (l > r) return item();
        return ask(l, r, 0, 0, n - 1);
    }

    template<typename T>
    void set(int ind, const T &t) {
        static array<pair<int, int>, 30> st;
        int l = 0, r = n - 1, i = 0;
        int ptr = -1;
        while (l != r) {
            st[++ptr] = {l, r};
            int m = (l + r) >> 1;
            if (ind <= m) {
                i = i * 2 + 1;
                r = m;
            } else {
                i = i * 2 + 2;
                l = m + 1;
            }
        }
        tree[i].init(t, l, r);
        while (i != 0) {
            i = (i - 1) / 2;
            tree[i].update(tree[i * 2 + 1], tree[i * 2 + 2], st[ptr].first, st[ptr].second);
            --ptr;
        }
    }
};

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

    int n, q;
    cin >> n >> q;
    vector<ll> w(n);
    cin >> w;
    struct str {
        int l, r;
        ll k;
        int ind;
    };
    vector<str> que(q);
    for (int i = 0; i < q; ++i) {
        cin >> que[i].l >> que[i].r >> que[i].k;
        que[i].ind = i;
        --que[i].l;
        --que[i].r;
    }
    vector<int> ans(q, -1);
    sort(que.begin(), que.end(), [&](const auto &a, const auto &b) {
        return a.r < b.r;
    });

    vector<int> st;
    segtree tree(n);

    int ind = 0;
    for (auto q : que) {
        while (ind <= q.r) {
            while (!st.empty() && w[st.back()] <= w[ind])
                st.pop_back();
            if (!st.empty()) {
                tree.set(st.back(), w[ind] + w[st.back()]);
            }
            st.pb(ind);

            ++ind;
        }

        ll val = tree.ask(q.l, q.r).mx;
        ans[q.ind] = (val <= q.k);
    }
    for (int k : ans)
        cout << k << '\n';


    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 620 KB Output is correct
13 Correct 4 ms 748 KB Output is correct
14 Correct 7 ms 876 KB Output is correct
15 Correct 6 ms 876 KB Output is correct
16 Correct 4 ms 748 KB Output is correct
17 Correct 4 ms 748 KB Output is correct
18 Correct 4 ms 876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1189 ms 63596 KB Output is correct
2 Correct 1206 ms 65260 KB Output is correct
3 Correct 1195 ms 65132 KB Output is correct
4 Correct 1190 ms 65260 KB Output is correct
5 Correct 1109 ms 65132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 8044 KB Output is correct
2 Correct 88 ms 8044 KB Output is correct
3 Correct 80 ms 8172 KB Output is correct
4 Correct 79 ms 8172 KB Output is correct
5 Correct 79 ms 8172 KB Output is correct
6 Correct 76 ms 7916 KB Output is correct
7 Correct 73 ms 7916 KB Output is correct
8 Correct 74 ms 7788 KB Output is correct
9 Correct 44 ms 4588 KB Output is correct
10 Correct 73 ms 7788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 620 KB Output is correct
13 Correct 4 ms 748 KB Output is correct
14 Correct 7 ms 876 KB Output is correct
15 Correct 6 ms 876 KB Output is correct
16 Correct 4 ms 748 KB Output is correct
17 Correct 4 ms 748 KB Output is correct
18 Correct 4 ms 876 KB Output is correct
19 Correct 214 ms 18540 KB Output is correct
20 Correct 218 ms 18560 KB Output is correct
21 Correct 203 ms 18412 KB Output is correct
22 Correct 253 ms 18284 KB Output is correct
23 Correct 195 ms 18284 KB Output is correct
24 Correct 172 ms 18156 KB Output is correct
25 Correct 172 ms 18412 KB Output is correct
26 Correct 182 ms 18456 KB Output is correct
27 Correct 184 ms 18540 KB Output is correct
28 Correct 186 ms 18412 KB Output is correct
29 Correct 193 ms 18664 KB Output is correct
30 Correct 187 ms 18540 KB Output is correct
31 Correct 190 ms 18540 KB Output is correct
32 Correct 187 ms 18544 KB Output is correct
33 Correct 191 ms 18540 KB Output is correct
34 Correct 168 ms 18156 KB Output is correct
35 Correct 167 ms 18156 KB Output is correct
36 Correct 199 ms 17900 KB Output is correct
37 Correct 170 ms 18028 KB Output is correct
38 Correct 166 ms 18156 KB Output is correct
39 Correct 176 ms 17216 KB Output is correct
40 Correct 134 ms 13804 KB Output is correct
41 Correct 162 ms 16648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 620 KB Output is correct
13 Correct 4 ms 748 KB Output is correct
14 Correct 7 ms 876 KB Output is correct
15 Correct 6 ms 876 KB Output is correct
16 Correct 4 ms 748 KB Output is correct
17 Correct 4 ms 748 KB Output is correct
18 Correct 4 ms 876 KB Output is correct
19 Correct 1189 ms 63596 KB Output is correct
20 Correct 1206 ms 65260 KB Output is correct
21 Correct 1195 ms 65132 KB Output is correct
22 Correct 1190 ms 65260 KB Output is correct
23 Correct 1109 ms 65132 KB Output is correct
24 Correct 89 ms 8044 KB Output is correct
25 Correct 88 ms 8044 KB Output is correct
26 Correct 80 ms 8172 KB Output is correct
27 Correct 79 ms 8172 KB Output is correct
28 Correct 79 ms 8172 KB Output is correct
29 Correct 76 ms 7916 KB Output is correct
30 Correct 73 ms 7916 KB Output is correct
31 Correct 74 ms 7788 KB Output is correct
32 Correct 44 ms 4588 KB Output is correct
33 Correct 73 ms 7788 KB Output is correct
34 Correct 214 ms 18540 KB Output is correct
35 Correct 218 ms 18560 KB Output is correct
36 Correct 203 ms 18412 KB Output is correct
37 Correct 253 ms 18284 KB Output is correct
38 Correct 195 ms 18284 KB Output is correct
39 Correct 172 ms 18156 KB Output is correct
40 Correct 172 ms 18412 KB Output is correct
41 Correct 182 ms 18456 KB Output is correct
42 Correct 184 ms 18540 KB Output is correct
43 Correct 186 ms 18412 KB Output is correct
44 Correct 193 ms 18664 KB Output is correct
45 Correct 187 ms 18540 KB Output is correct
46 Correct 190 ms 18540 KB Output is correct
47 Correct 187 ms 18544 KB Output is correct
48 Correct 191 ms 18540 KB Output is correct
49 Correct 168 ms 18156 KB Output is correct
50 Correct 167 ms 18156 KB Output is correct
51 Correct 199 ms 17900 KB Output is correct
52 Correct 170 ms 18028 KB Output is correct
53 Correct 166 ms 18156 KB Output is correct
54 Correct 176 ms 17216 KB Output is correct
55 Correct 134 ms 13804 KB Output is correct
56 Correct 162 ms 16648 KB Output is correct
57 Correct 1196 ms 65720 KB Output is correct
58 Correct 1213 ms 65644 KB Output is correct
59 Correct 1184 ms 65644 KB Output is correct
60 Correct 1204 ms 65536 KB Output is correct
61 Correct 1187 ms 65516 KB Output is correct
62 Correct 1180 ms 65388 KB Output is correct
63 Correct 924 ms 65020 KB Output is correct
64 Correct 931 ms 65076 KB Output is correct
65 Correct 1022 ms 65516 KB Output is correct
66 Correct 1043 ms 65388 KB Output is correct
67 Correct 1052 ms 65388 KB Output is correct
68 Correct 1081 ms 65404 KB Output is correct
69 Correct 1068 ms 65488 KB Output is correct
70 Correct 1066 ms 65552 KB Output is correct
71 Correct 1058 ms 65464 KB Output is correct
72 Correct 1065 ms 65484 KB Output is correct
73 Correct 895 ms 64548 KB Output is correct
74 Correct 901 ms 64660 KB Output is correct
75 Correct 910 ms 64512 KB Output is correct
76 Correct 888 ms 64364 KB Output is correct
77 Correct 935 ms 64548 KB Output is correct
78 Correct 971 ms 63596 KB Output is correct
79 Correct 712 ms 64876 KB Output is correct
80 Correct 932 ms 72132 KB Output is correct