Submission #1028362

# Submission time Handle Problem Language Result Execution time Memory
1028362 2024-07-19T17:25:56 Z Fatonim Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++17
100 / 100
1270 ms 180032 KB
// "We create our own demons"
#include <bits/stdc++.h>

using namespace std;

#ifdef ONPC
#include "debug.h"
#else
#define dbg(...)
#endif

#define int long long
#define ll long long
#define ld long double
#define pi pair<int, int>
// vector
#define sz(a) (int)((a).size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(x) (*max_element(all(x)))
#define minel(x) (*min_element(all(x)))
#define maxpos(x) (max_element(all(x)) - x.begin())
#define minpos(x) (min_element(all(x)) - x.begin())
#define rev(x) (reverse(all(x)))
// bits
#define popcount(n) __builtin_popcountll(n)
#define clz(n) __builtin_clzll(n)
// math
#define sqr(n) ((n) * (n))
#define divup(a, b) (((a) + (b)-1) / (b))
// ostream
#define Fixed(a) cout << fixed << setprecision(12) << a;

template <class T> bool chmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template <class T> bool chmax(T& a, const T& b) { return b > a ? a = b, 1 : 0; }

template <class T> using min_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> using max_queue = priority_queue<T, vector<T>, less<T>>;

template <class T> using V = vector<T>;
using vi = V<int>;
using vd = V<ld>;
using vb = V<bool>;
using vpi = V<pi>;
using vvi = V<vi>;
using vvb = V<vb>;

const int mod = 1e9 + 7; // 998244353 1e9 + 7
const ll inf = (int)(1e18) + 7;
const int inf_s = 1e9 + 7;
const ld eps = 1e-9;

const int B = 32;
const int N = 1000 + 3;
const int logn = 21;
const int maxn = 1e6 + 7;
/////////////////////////solve/////////////////////////

struct segtree {
public:
    struct node {
        int mx = 0;
        void apply(int tl, int tr, int x) {
            mx = x;
        }
    };
private:
    int n;
    V<node> tree;

    node unite(const node &a, const node &b) {
        node res;
        res.mx = max(a.mx, b.mx);
        return res;
    }

    void push(int v, int tl, int tr) {
    }

    template <class T>
    void build(const V<T>& a, int v, int tl, int tr) {
        if (tr - tl == 1) {
            tree[v].apply(tl, tr, a[tl]);
            return;
        }
        int tm = (tl + tr) >> 1;
        build(a, 2 * v, tl, tm);
        build(a, 2 * v + 1, tm, tr);
        tree[v] = unite(tree[2 * v], tree[2 * v + 1]);
    }

    void build(int v, int tl, int tr) {
        if (tr - tl == 1) {
            return;
        }
        int tm = (tl + tr) >> 1;
        build(2 * v, tl, tm);
        build(2 * v + 1, tm, tr);
        tree[v] = unite(tree[2 * v], tree[2 * v + 1]);
    }

    template <class T>
    void update(int l, int r, const T &x, int v, int tl, int tr) {
        if (tl >= r || tr <= l) return;
        if (tl >= l && tr <= r) {
            tree[v].apply(tl, tr, x);
            return;
        }
        int tm = (tl + tr) >> 1;
        push(v, tl, tr);
        update(l, r, x, 2 * v, tl, tm);
        update(l, r, x, 2 * v + 1, tm, tr);
        tree[v] = unite(tree[2 * v], tree[2 * v + 1]);
    }

    node get(int l, int r, int v, int tl, int tr) {
        if (tl >= l && tr <= r) return tree[v];
        int tm = (tl + tr) >> 1;
        push(v, tl, tr);
        if (tm <= l) return get(l, r, 2 * v + 1, tm, tr);
        if (tm >= r) return get(l, r, 2 * v, tl, tm);
        return unite(get(l, r, 2 * v, tl, tm),
            get(l, r, 2 * v + 1, tm, tr));
    }

public:
    segtree(int n) : n(n) {
        tree.resize(4 * n);
        build(1, 0, n);
    }

    template <class T> 
    segtree(const V<T>& a) {
        n = sz(a);
        tree.resize(4 * n);
        build(a, 1, 0, n);
    }

    template <class T>
    void update(int l, int r, const T& x) {
        update(l, r + 1, x, 1, 0, n);
    }

    template <class T>
    void update(int i, const T& x) {
        update(i, i + 1, x, 1, 0, n);
    }

    node get(int l, int r) {
        if (l > r) return {-inf};
        return get(l, r + 1, 1, 0, n);
    }

    node get(int i) {
        return get(i, i + 1, 1, 0, n);
    }
};

struct query {
    int r, k, qi;
};

V<query> L[maxn];
int ans[maxn];

void solve() {
    int n, m;
    cin >> n >> m;

    vi a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    for (int qi = 0; qi < m; ++qi) {
        int l, r, k;
        cin >> l >> r >> k;
        --l; --r;

        L[l].push_back({r, k, qi});
    }

    segtree st1(a), st2(n);

    vi st;
    st.push_back(n);
    for (int i = n - 1; i >= 0; --i) {
        while (sz(st) > 1 && a[st.back()] < a[i]) {
            int j = st.back();
            st.pop_back();
            st2.update(j, 0);
        }
        int mx = st1.get(i + 1, st.back() - 1).mx;
        dbg(i, a[i], mx, st.back());
        st2.update(i, a[i] + mx);
        st.push_back(i);
        for (auto& [r, k, qi] : L[i]) {
            dbg(st);
            auto it = upper_bound(rall(st), r);
            dbg(*it, r);
            int res = 0;
            --it;
            dbg(i, *it);
            chmax(res, st2.get(i, *it - 1).mx);
            dbg(res);
            if (*it != n) {
                chmax(res, a[*it] + st1.get(*it + 1, r).mx);
            }
            dbg(res);
            ans[qi] = (res <= k);
        }
    }

    for (int qi = 0; qi < m; ++qi) {
        cout << ans[qi] << "\n";
    }
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

#ifdef ONPC
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
#endif

    int t = 1;
    //cin >> t;
    for (int i = 1; i <= t; ++i) {
#ifdef ONPC
        cerr << "===========" << i << "===========" << '\n';
#endif
        solve();
    }

#ifdef ONPC
    cerr << endl << "Time " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23900 KB Output is correct
2 Correct 10 ms 23900 KB Output is correct
3 Correct 10 ms 23900 KB Output is correct
4 Correct 10 ms 23936 KB Output is correct
5 Correct 10 ms 23748 KB Output is correct
6 Correct 10 ms 24012 KB Output is correct
7 Correct 10 ms 23896 KB Output is correct
8 Correct 11 ms 23900 KB Output is correct
9 Correct 10 ms 23900 KB Output is correct
10 Correct 10 ms 23896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23900 KB Output is correct
2 Correct 10 ms 23900 KB Output is correct
3 Correct 10 ms 23900 KB Output is correct
4 Correct 10 ms 23936 KB Output is correct
5 Correct 10 ms 23748 KB Output is correct
6 Correct 10 ms 24012 KB Output is correct
7 Correct 10 ms 23896 KB Output is correct
8 Correct 11 ms 23900 KB Output is correct
9 Correct 10 ms 23900 KB Output is correct
10 Correct 10 ms 23896 KB Output is correct
11 Correct 12 ms 24216 KB Output is correct
12 Correct 14 ms 24408 KB Output is correct
13 Correct 13 ms 24412 KB Output is correct
14 Correct 14 ms 24504 KB Output is correct
15 Correct 14 ms 24664 KB Output is correct
16 Correct 13 ms 24664 KB Output is correct
17 Correct 15 ms 24488 KB Output is correct
18 Correct 14 ms 24412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1128 ms 162988 KB Output is correct
2 Correct 1075 ms 169368 KB Output is correct
3 Correct 1029 ms 169212 KB Output is correct
4 Correct 1125 ms 169556 KB Output is correct
5 Correct 1075 ms 177532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 36284 KB Output is correct
2 Correct 89 ms 37580 KB Output is correct
3 Correct 76 ms 38228 KB Output is correct
4 Correct 77 ms 38096 KB Output is correct
5 Correct 84 ms 38092 KB Output is correct
6 Correct 57 ms 37840 KB Output is correct
7 Correct 60 ms 37784 KB Output is correct
8 Correct 70 ms 37388 KB Output is correct
9 Correct 35 ms 29168 KB Output is correct
10 Correct 69 ms 37304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23900 KB Output is correct
2 Correct 10 ms 23900 KB Output is correct
3 Correct 10 ms 23900 KB Output is correct
4 Correct 10 ms 23936 KB Output is correct
5 Correct 10 ms 23748 KB Output is correct
6 Correct 10 ms 24012 KB Output is correct
7 Correct 10 ms 23896 KB Output is correct
8 Correct 11 ms 23900 KB Output is correct
9 Correct 10 ms 23900 KB Output is correct
10 Correct 10 ms 23896 KB Output is correct
11 Correct 12 ms 24216 KB Output is correct
12 Correct 14 ms 24408 KB Output is correct
13 Correct 13 ms 24412 KB Output is correct
14 Correct 14 ms 24504 KB Output is correct
15 Correct 14 ms 24664 KB Output is correct
16 Correct 13 ms 24664 KB Output is correct
17 Correct 15 ms 24488 KB Output is correct
18 Correct 14 ms 24412 KB Output is correct
19 Correct 202 ms 52884 KB Output is correct
20 Correct 191 ms 52892 KB Output is correct
21 Correct 160 ms 53972 KB Output is correct
22 Correct 180 ms 53844 KB Output is correct
23 Correct 162 ms 53724 KB Output is correct
24 Correct 133 ms 55576 KB Output is correct
25 Correct 143 ms 55492 KB Output is correct
26 Correct 159 ms 55244 KB Output is correct
27 Correct 154 ms 55224 KB Output is correct
28 Correct 178 ms 55024 KB Output is correct
29 Correct 227 ms 54724 KB Output is correct
30 Correct 219 ms 54728 KB Output is correct
31 Correct 188 ms 54692 KB Output is correct
32 Correct 223 ms 54732 KB Output is correct
33 Correct 191 ms 54736 KB Output is correct
34 Correct 125 ms 54488 KB Output is correct
35 Correct 136 ms 54436 KB Output is correct
36 Correct 137 ms 54272 KB Output is correct
37 Correct 131 ms 54396 KB Output is correct
38 Correct 133 ms 54424 KB Output is correct
39 Correct 192 ms 53992 KB Output is correct
40 Correct 121 ms 46896 KB Output is correct
41 Correct 168 ms 52096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23900 KB Output is correct
2 Correct 10 ms 23900 KB Output is correct
3 Correct 10 ms 23900 KB Output is correct
4 Correct 10 ms 23936 KB Output is correct
5 Correct 10 ms 23748 KB Output is correct
6 Correct 10 ms 24012 KB Output is correct
7 Correct 10 ms 23896 KB Output is correct
8 Correct 11 ms 23900 KB Output is correct
9 Correct 10 ms 23900 KB Output is correct
10 Correct 10 ms 23896 KB Output is correct
11 Correct 12 ms 24216 KB Output is correct
12 Correct 14 ms 24408 KB Output is correct
13 Correct 13 ms 24412 KB Output is correct
14 Correct 14 ms 24504 KB Output is correct
15 Correct 14 ms 24664 KB Output is correct
16 Correct 13 ms 24664 KB Output is correct
17 Correct 15 ms 24488 KB Output is correct
18 Correct 14 ms 24412 KB Output is correct
19 Correct 1128 ms 162988 KB Output is correct
20 Correct 1075 ms 169368 KB Output is correct
21 Correct 1029 ms 169212 KB Output is correct
22 Correct 1125 ms 169556 KB Output is correct
23 Correct 1075 ms 177532 KB Output is correct
24 Correct 90 ms 36284 KB Output is correct
25 Correct 89 ms 37580 KB Output is correct
26 Correct 76 ms 38228 KB Output is correct
27 Correct 77 ms 38096 KB Output is correct
28 Correct 84 ms 38092 KB Output is correct
29 Correct 57 ms 37840 KB Output is correct
30 Correct 60 ms 37784 KB Output is correct
31 Correct 70 ms 37388 KB Output is correct
32 Correct 35 ms 29168 KB Output is correct
33 Correct 69 ms 37304 KB Output is correct
34 Correct 202 ms 52884 KB Output is correct
35 Correct 191 ms 52892 KB Output is correct
36 Correct 160 ms 53972 KB Output is correct
37 Correct 180 ms 53844 KB Output is correct
38 Correct 162 ms 53724 KB Output is correct
39 Correct 133 ms 55576 KB Output is correct
40 Correct 143 ms 55492 KB Output is correct
41 Correct 159 ms 55244 KB Output is correct
42 Correct 154 ms 55224 KB Output is correct
43 Correct 178 ms 55024 KB Output is correct
44 Correct 227 ms 54724 KB Output is correct
45 Correct 219 ms 54728 KB Output is correct
46 Correct 188 ms 54692 KB Output is correct
47 Correct 223 ms 54732 KB Output is correct
48 Correct 191 ms 54736 KB Output is correct
49 Correct 125 ms 54488 KB Output is correct
50 Correct 136 ms 54436 KB Output is correct
51 Correct 137 ms 54272 KB Output is correct
52 Correct 131 ms 54396 KB Output is correct
53 Correct 133 ms 54424 KB Output is correct
54 Correct 192 ms 53992 KB Output is correct
55 Correct 121 ms 46896 KB Output is correct
56 Correct 168 ms 52096 KB Output is correct
57 Correct 1270 ms 170004 KB Output is correct
58 Correct 1159 ms 169812 KB Output is correct
59 Correct 1006 ms 171684 KB Output is correct
60 Correct 982 ms 171420 KB Output is correct
61 Correct 985 ms 171460 KB Output is correct
62 Correct 982 ms 171604 KB Output is correct
63 Correct 610 ms 176816 KB Output is correct
64 Correct 650 ms 176740 KB Output is correct
65 Correct 888 ms 180032 KB Output is correct
66 Correct 958 ms 179828 KB Output is correct
67 Correct 950 ms 179632 KB Output is correct
68 Correct 1111 ms 177920 KB Output is correct
69 Correct 1082 ms 177880 KB Output is correct
70 Correct 1000 ms 177928 KB Output is correct
71 Correct 1018 ms 178132 KB Output is correct
72 Correct 991 ms 177916 KB Output is correct
73 Correct 529 ms 173324 KB Output is correct
74 Correct 569 ms 174068 KB Output is correct
75 Correct 518 ms 173124 KB Output is correct
76 Correct 523 ms 172976 KB Output is correct
77 Correct 525 ms 173036 KB Output is correct
78 Correct 890 ms 172536 KB Output is correct
79 Correct 584 ms 128488 KB Output is correct
80 Correct 791 ms 166376 KB Output is correct