#include <bits/stdc++.h>
#ifdef ONPC
#include "t_debug.cpp"
#else
#define debug(...) 42
#endif
using namespace std;
//namespace pbds = __gnu_pbds;
using ll = long long;
const int inf = 1e9;
const ll infl = 1e18;
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rng(RANDOM);
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p) { return is >> p.first >> p.second; }
template<typename Cont> int sz(const Cont& cont) { return int(cont.size()); }
const string fileio = "";
constexpr int tests = 0, nmax = 2e5, nlog = __lg(nmax), mod = 1e9+7;
struct SegmentTree {
using T = int;
using S = int;
const T id = {};
inline T single(S v) { return v; }
T merge(const T& l, const T& r) {
return max(l, r);
}
int n;
vector<T> tree;
SegmentTree(int n) : n(n) {
tree.resize(n * 2, id);
build();
}
SegmentTree(vector<S> arr) : n(arr.size()) {
tree.resize(n * 2, id);
for (int i = 0; i < n; i++) {
tree[i + n] = single(arr[i]);
}
build();
}
void build() {
for (int i = n-1; i >= 1; i--) {
tree[i] = merge(tree[i*2], tree[i*2 + 1]);
}
}
void update(int i, S v) {
tree[i+=n] = single(v);
for (i /= 2; i >= 1; i/= 2)
tree[i] = merge(tree[i*2], tree[i*2+1]);
}
T queryit(int l, int r) {
T left = id, right = id;
l += n;
r += n;
while (l <= r) {
if (l % 2 == 1) left = merge(left, tree[l++]);
if (r % 2 == 0) right = merge(right, tree[r--]);
l /= 2; r /= 2;
}
return merge(left, right);
}
int find_first(function<bool(T&)> predicate, int last = 0) {
int v = 1;
while (v < n) {
if (predicate(tree[v*2 + last])) v = v*2 + last;
else if (predicate(tree[v*2 + !last])) v = v*2 + !last;
else return -1;
}
return v;
}
};
int solve() {
int n, m;
cin >> n >> m;
if (!cin) return 1;
vector<int> w(n);
for (auto& i : w) cin >> i;
vector<int> ix(n);
iota(ix.begin(), ix.end(), 0);
sort(ix.begin(), ix.end(), [&](int a, int b) {
return w[a] > w[b];
});
vector<tuple<int,int,int,int>> q(m);
vector<int> ans(m);
for (int i = 0; i < m; i++) {
int l, r, k;
cin >> l >> r >> k;
q[i] = {r-1, l-1, k, i};
}
sort(q.begin(), q.end());
SegmentTree tree(1<<20);
vector<pair<int,int>> st;
int j = 0;
for (auto [r, l, k, i] : q) {
while (j <= r && j < n) {
while (st.size() && st.back().first <= w[j]) {
st.pop_back();
};
if (st.size()) {
int k = st.back().second;
tree.update(k, w[j] + w[k]);
}
st.emplace_back(w[j], j);
j++;
}
ans[i] = tree.queryit(l, r) <= k;
}
for (int i : ans) cout << i << '\n';
return 0;
}
signed main() {
int t = 1;
#ifdef ONPC
t = 10000;
#else
if (fileio.size()) {freopen((fileio+".in").c_str(),"r",stdin);freopen((fileio+".out").c_str(),"w",stdout);}
#endif
cin.tie(0)->sync_with_stdio(0);
if (tests) cin >> t;
while (t-- && cin) {
if (solve()) break;
#ifdef ONPC
cout << "____________________" << endl;
#endif
}
return 0;
}
/*
█████ █████ ███ ████
▒▒███ ▒▒███ ▒▒▒ ▒▒███
███████ ████████ ███████ ████ ▒███ █████ ████ ██████ ████████
███▒▒███ ▒▒███▒▒███ ███▒▒███ ▒▒███ ▒███ ▒▒███ ▒███ ███▒▒███▒▒███▒▒███
▒███ ▒███ ▒███ ▒▒▒ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒▒▒
▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███
▒▒████████ █████ ▒▒████████ █████ █████ ▒▒███████ ▒▒██████ █████
▒▒▒▒▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒███ ▒▒▒▒▒▒ ▒▒▒▒▒
███ ▒███
▒▒██████
▒▒▒▒▒▒
*/
Compilation message
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:129:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
129 | if (fileio.size()) {freopen((fileio+".in").c_str(),"r",stdin);freopen((fileio+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:129:74: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
129 | if (fileio.size()) {freopen((fileio+".in").c_str(),"r",stdin);freopen((fileio+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
5 ms |
8532 KB |
Output is correct |
3 |
Correct |
5 ms |
8532 KB |
Output is correct |
4 |
Correct |
7 ms |
8532 KB |
Output is correct |
5 |
Correct |
7 ms |
8520 KB |
Output is correct |
6 |
Correct |
6 ms |
8532 KB |
Output is correct |
7 |
Correct |
5 ms |
8532 KB |
Output is correct |
8 |
Correct |
5 ms |
8532 KB |
Output is correct |
9 |
Correct |
5 ms |
8532 KB |
Output is correct |
10 |
Correct |
7 ms |
8524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
5 ms |
8532 KB |
Output is correct |
3 |
Correct |
5 ms |
8532 KB |
Output is correct |
4 |
Correct |
7 ms |
8532 KB |
Output is correct |
5 |
Correct |
7 ms |
8520 KB |
Output is correct |
6 |
Correct |
6 ms |
8532 KB |
Output is correct |
7 |
Correct |
5 ms |
8532 KB |
Output is correct |
8 |
Correct |
5 ms |
8532 KB |
Output is correct |
9 |
Correct |
5 ms |
8532 KB |
Output is correct |
10 |
Correct |
7 ms |
8524 KB |
Output is correct |
11 |
Correct |
6 ms |
8564 KB |
Output is correct |
12 |
Correct |
7 ms |
8660 KB |
Output is correct |
13 |
Correct |
7 ms |
8748 KB |
Output is correct |
14 |
Correct |
9 ms |
8788 KB |
Output is correct |
15 |
Correct |
10 ms |
8788 KB |
Output is correct |
16 |
Correct |
8 ms |
8764 KB |
Output is correct |
17 |
Correct |
8 ms |
8656 KB |
Output is correct |
18 |
Correct |
9 ms |
8724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
884 ms |
37852 KB |
Output is correct |
2 |
Correct |
887 ms |
52496 KB |
Output is correct |
3 |
Correct |
882 ms |
64732 KB |
Output is correct |
4 |
Correct |
879 ms |
53820 KB |
Output is correct |
5 |
Correct |
808 ms |
53060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
11400 KB |
Output is correct |
2 |
Correct |
75 ms |
13344 KB |
Output is correct |
3 |
Correct |
71 ms |
13376 KB |
Output is correct |
4 |
Correct |
71 ms |
13388 KB |
Output is correct |
5 |
Correct |
64 ms |
13376 KB |
Output is correct |
6 |
Correct |
63 ms |
13216 KB |
Output is correct |
7 |
Correct |
64 ms |
13236 KB |
Output is correct |
8 |
Correct |
63 ms |
13040 KB |
Output is correct |
9 |
Correct |
45 ms |
12004 KB |
Output is correct |
10 |
Correct |
61 ms |
13056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
5 ms |
8532 KB |
Output is correct |
3 |
Correct |
5 ms |
8532 KB |
Output is correct |
4 |
Correct |
7 ms |
8532 KB |
Output is correct |
5 |
Correct |
7 ms |
8520 KB |
Output is correct |
6 |
Correct |
6 ms |
8532 KB |
Output is correct |
7 |
Correct |
5 ms |
8532 KB |
Output is correct |
8 |
Correct |
5 ms |
8532 KB |
Output is correct |
9 |
Correct |
5 ms |
8532 KB |
Output is correct |
10 |
Correct |
7 ms |
8524 KB |
Output is correct |
11 |
Correct |
6 ms |
8564 KB |
Output is correct |
12 |
Correct |
7 ms |
8660 KB |
Output is correct |
13 |
Correct |
7 ms |
8748 KB |
Output is correct |
14 |
Correct |
9 ms |
8788 KB |
Output is correct |
15 |
Correct |
10 ms |
8788 KB |
Output is correct |
16 |
Correct |
8 ms |
8764 KB |
Output is correct |
17 |
Correct |
8 ms |
8656 KB |
Output is correct |
18 |
Correct |
9 ms |
8724 KB |
Output is correct |
19 |
Correct |
173 ms |
20940 KB |
Output is correct |
20 |
Correct |
177 ms |
20892 KB |
Output is correct |
21 |
Correct |
163 ms |
20824 KB |
Output is correct |
22 |
Correct |
163 ms |
20816 KB |
Output is correct |
23 |
Correct |
190 ms |
20840 KB |
Output is correct |
24 |
Correct |
163 ms |
20832 KB |
Output is correct |
25 |
Correct |
141 ms |
20680 KB |
Output is correct |
26 |
Correct |
165 ms |
20888 KB |
Output is correct |
27 |
Correct |
161 ms |
20908 KB |
Output is correct |
28 |
Correct |
157 ms |
20812 KB |
Output is correct |
29 |
Correct |
156 ms |
21028 KB |
Output is correct |
30 |
Correct |
143 ms |
20900 KB |
Output is correct |
31 |
Correct |
150 ms |
20936 KB |
Output is correct |
32 |
Correct |
142 ms |
20940 KB |
Output is correct |
33 |
Correct |
146 ms |
20896 KB |
Output is correct |
34 |
Correct |
137 ms |
20608 KB |
Output is correct |
35 |
Correct |
151 ms |
20592 KB |
Output is correct |
36 |
Correct |
128 ms |
20396 KB |
Output is correct |
37 |
Correct |
132 ms |
20408 KB |
Output is correct |
38 |
Correct |
166 ms |
20564 KB |
Output is correct |
39 |
Correct |
141 ms |
19532 KB |
Output is correct |
40 |
Correct |
139 ms |
18380 KB |
Output is correct |
41 |
Correct |
160 ms |
19096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
5 ms |
8532 KB |
Output is correct |
3 |
Correct |
5 ms |
8532 KB |
Output is correct |
4 |
Correct |
7 ms |
8532 KB |
Output is correct |
5 |
Correct |
7 ms |
8520 KB |
Output is correct |
6 |
Correct |
6 ms |
8532 KB |
Output is correct |
7 |
Correct |
5 ms |
8532 KB |
Output is correct |
8 |
Correct |
5 ms |
8532 KB |
Output is correct |
9 |
Correct |
5 ms |
8532 KB |
Output is correct |
10 |
Correct |
7 ms |
8524 KB |
Output is correct |
11 |
Correct |
6 ms |
8564 KB |
Output is correct |
12 |
Correct |
7 ms |
8660 KB |
Output is correct |
13 |
Correct |
7 ms |
8748 KB |
Output is correct |
14 |
Correct |
9 ms |
8788 KB |
Output is correct |
15 |
Correct |
10 ms |
8788 KB |
Output is correct |
16 |
Correct |
8 ms |
8764 KB |
Output is correct |
17 |
Correct |
8 ms |
8656 KB |
Output is correct |
18 |
Correct |
9 ms |
8724 KB |
Output is correct |
19 |
Correct |
884 ms |
37852 KB |
Output is correct |
20 |
Correct |
887 ms |
52496 KB |
Output is correct |
21 |
Correct |
882 ms |
64732 KB |
Output is correct |
22 |
Correct |
879 ms |
53820 KB |
Output is correct |
23 |
Correct |
808 ms |
53060 KB |
Output is correct |
24 |
Correct |
70 ms |
11400 KB |
Output is correct |
25 |
Correct |
75 ms |
13344 KB |
Output is correct |
26 |
Correct |
71 ms |
13376 KB |
Output is correct |
27 |
Correct |
71 ms |
13388 KB |
Output is correct |
28 |
Correct |
64 ms |
13376 KB |
Output is correct |
29 |
Correct |
63 ms |
13216 KB |
Output is correct |
30 |
Correct |
64 ms |
13236 KB |
Output is correct |
31 |
Correct |
63 ms |
13040 KB |
Output is correct |
32 |
Correct |
45 ms |
12004 KB |
Output is correct |
33 |
Correct |
61 ms |
13056 KB |
Output is correct |
34 |
Correct |
173 ms |
20940 KB |
Output is correct |
35 |
Correct |
177 ms |
20892 KB |
Output is correct |
36 |
Correct |
163 ms |
20824 KB |
Output is correct |
37 |
Correct |
163 ms |
20816 KB |
Output is correct |
38 |
Correct |
190 ms |
20840 KB |
Output is correct |
39 |
Correct |
163 ms |
20832 KB |
Output is correct |
40 |
Correct |
141 ms |
20680 KB |
Output is correct |
41 |
Correct |
165 ms |
20888 KB |
Output is correct |
42 |
Correct |
161 ms |
20908 KB |
Output is correct |
43 |
Correct |
157 ms |
20812 KB |
Output is correct |
44 |
Correct |
156 ms |
21028 KB |
Output is correct |
45 |
Correct |
143 ms |
20900 KB |
Output is correct |
46 |
Correct |
150 ms |
20936 KB |
Output is correct |
47 |
Correct |
142 ms |
20940 KB |
Output is correct |
48 |
Correct |
146 ms |
20896 KB |
Output is correct |
49 |
Correct |
137 ms |
20608 KB |
Output is correct |
50 |
Correct |
151 ms |
20592 KB |
Output is correct |
51 |
Correct |
128 ms |
20396 KB |
Output is correct |
52 |
Correct |
132 ms |
20408 KB |
Output is correct |
53 |
Correct |
166 ms |
20564 KB |
Output is correct |
54 |
Correct |
141 ms |
19532 KB |
Output is correct |
55 |
Correct |
139 ms |
18380 KB |
Output is correct |
56 |
Correct |
160 ms |
19096 KB |
Output is correct |
57 |
Correct |
932 ms |
71376 KB |
Output is correct |
58 |
Correct |
940 ms |
71460 KB |
Output is correct |
59 |
Correct |
915 ms |
71288 KB |
Output is correct |
60 |
Correct |
943 ms |
71260 KB |
Output is correct |
61 |
Correct |
935 ms |
71352 KB |
Output is correct |
62 |
Correct |
950 ms |
71364 KB |
Output is correct |
63 |
Correct |
747 ms |
69552 KB |
Output is correct |
64 |
Correct |
707 ms |
69584 KB |
Output is correct |
65 |
Correct |
760 ms |
71284 KB |
Output is correct |
66 |
Correct |
786 ms |
71300 KB |
Output is correct |
67 |
Correct |
763 ms |
71364 KB |
Output is correct |
68 |
Correct |
780 ms |
71544 KB |
Output is correct |
69 |
Correct |
813 ms |
71620 KB |
Output is correct |
70 |
Correct |
760 ms |
71352 KB |
Output is correct |
71 |
Correct |
759 ms |
71476 KB |
Output is correct |
72 |
Correct |
779 ms |
71372 KB |
Output is correct |
73 |
Correct |
668 ms |
67916 KB |
Output is correct |
74 |
Correct |
692 ms |
69032 KB |
Output is correct |
75 |
Correct |
652 ms |
67900 KB |
Output is correct |
76 |
Correct |
653 ms |
68008 KB |
Output is correct |
77 |
Correct |
689 ms |
67896 KB |
Output is correct |
78 |
Correct |
742 ms |
65204 KB |
Output is correct |
79 |
Correct |
610 ms |
56664 KB |
Output is correct |
80 |
Correct |
688 ms |
62536 KB |
Output is correct |