#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Rand(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int MAX = 1000010;
int nArr;
int nQuery;
int arr[MAX];
int ans[MAX];
stack<int> s;
int st[(1 << 21) + 5];
vector<tuple<int, int, int>> queries[MAX];
void init(void) {
cin >> nArr >> nQuery;
for (int i = 1; i <= nArr; ++i) {
cin >> arr[i];
}
for (int i = 1; i <= nQuery; ++i) {
int l, r, k; cin >> l >> r >> k;
queries[r].emplace_back(l, k, i);
}
}
void update(int pos, int value) {
int id = 1, l = 1, r = nArr;
while (l < r) {
int mid = l + r >> 1;
if (pos <= mid) id <<= 1, r = mid;
else id = id << 1 | 1, l = mid + 1;
}
st[id] = max(st[id], value);
while (id > 1) {
id >>= 1;
st[id] = max(st[id << 1], st[id << 1 | 1]);
}
}
int get(int id, int l, int r, int u, int v) {
if (l > v || r < u) return 0;
if (l >= u && r <= v) return st[id];
int mid = l + r >> 1; return max(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
}
#define get(l, r) (get(1, 1, nArr, l, r))
void process(void) {
for (int i = 1; i <= nArr; ++i) {
while (s.size() && arr[s.top()] <= arr[i]) s.pop();
if (s.size()) update(s.top(), arr[s.top()] + arr[i]);
s.push(i);
for (tuple<int, int, int> &qry: queries[i]) {
int j, k, id; tie(j, k, id) = qry;
ans[id] = get(j, i) <= k;
}
}
for (int i = 1; i <= nQuery; ++i) {
cout << ans[i] << '\n';
}
}
int main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
// freopen("C.INP", "r", stdin);
// freopen("C.OUT", "w", stdout);
init();
process();
return 0;
}
Compilation message
sortbooks.cpp: In function 'void update(int, int)':
sortbooks.cpp:36:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
36 | int mid = l + r >> 1;
| ~~^~~
sortbooks.cpp: In function 'int get(int, int, int, int, int)':
sortbooks.cpp:50:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | int mid = l + r >> 1; return max(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
29276 KB |
Output is correct |
2 |
Correct |
6 ms |
29376 KB |
Output is correct |
3 |
Correct |
6 ms |
29300 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
6 ms |
29368 KB |
Output is correct |
6 |
Correct |
6 ms |
29272 KB |
Output is correct |
7 |
Correct |
6 ms |
29272 KB |
Output is correct |
8 |
Correct |
6 ms |
29276 KB |
Output is correct |
9 |
Correct |
6 ms |
29272 KB |
Output is correct |
10 |
Correct |
6 ms |
29276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
29276 KB |
Output is correct |
2 |
Correct |
6 ms |
29376 KB |
Output is correct |
3 |
Correct |
6 ms |
29300 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
6 ms |
29368 KB |
Output is correct |
6 |
Correct |
6 ms |
29272 KB |
Output is correct |
7 |
Correct |
6 ms |
29272 KB |
Output is correct |
8 |
Correct |
6 ms |
29276 KB |
Output is correct |
9 |
Correct |
6 ms |
29272 KB |
Output is correct |
10 |
Correct |
6 ms |
29276 KB |
Output is correct |
11 |
Correct |
8 ms |
29272 KB |
Output is correct |
12 |
Correct |
8 ms |
29532 KB |
Output is correct |
13 |
Correct |
8 ms |
29532 KB |
Output is correct |
14 |
Correct |
10 ms |
29532 KB |
Output is correct |
15 |
Correct |
9 ms |
29532 KB |
Output is correct |
16 |
Correct |
8 ms |
29532 KB |
Output is correct |
17 |
Correct |
8 ms |
29532 KB |
Output is correct |
18 |
Correct |
9 ms |
29376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1012 ms |
95584 KB |
Output is correct |
2 |
Correct |
1050 ms |
96552 KB |
Output is correct |
3 |
Correct |
1028 ms |
96368 KB |
Output is correct |
4 |
Correct |
1008 ms |
96592 KB |
Output is correct |
5 |
Correct |
911 ms |
90452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
33460 KB |
Output is correct |
2 |
Correct |
76 ms |
33320 KB |
Output is correct |
3 |
Correct |
63 ms |
33380 KB |
Output is correct |
4 |
Correct |
65 ms |
33620 KB |
Output is correct |
5 |
Correct |
63 ms |
33620 KB |
Output is correct |
6 |
Correct |
53 ms |
33104 KB |
Output is correct |
7 |
Correct |
57 ms |
32904 KB |
Output is correct |
8 |
Correct |
58 ms |
33216 KB |
Output is correct |
9 |
Correct |
33 ms |
32120 KB |
Output is correct |
10 |
Correct |
58 ms |
33168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
29276 KB |
Output is correct |
2 |
Correct |
6 ms |
29376 KB |
Output is correct |
3 |
Correct |
6 ms |
29300 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
6 ms |
29368 KB |
Output is correct |
6 |
Correct |
6 ms |
29272 KB |
Output is correct |
7 |
Correct |
6 ms |
29272 KB |
Output is correct |
8 |
Correct |
6 ms |
29276 KB |
Output is correct |
9 |
Correct |
6 ms |
29272 KB |
Output is correct |
10 |
Correct |
6 ms |
29276 KB |
Output is correct |
11 |
Correct |
8 ms |
29272 KB |
Output is correct |
12 |
Correct |
8 ms |
29532 KB |
Output is correct |
13 |
Correct |
8 ms |
29532 KB |
Output is correct |
14 |
Correct |
10 ms |
29532 KB |
Output is correct |
15 |
Correct |
9 ms |
29532 KB |
Output is correct |
16 |
Correct |
8 ms |
29532 KB |
Output is correct |
17 |
Correct |
8 ms |
29532 KB |
Output is correct |
18 |
Correct |
9 ms |
29376 KB |
Output is correct |
19 |
Correct |
166 ms |
41116 KB |
Output is correct |
20 |
Correct |
159 ms |
41356 KB |
Output is correct |
21 |
Correct |
143 ms |
40556 KB |
Output is correct |
22 |
Correct |
146 ms |
40592 KB |
Output is correct |
23 |
Correct |
154 ms |
40768 KB |
Output is correct |
24 |
Correct |
129 ms |
39700 KB |
Output is correct |
25 |
Correct |
120 ms |
39716 KB |
Output is correct |
26 |
Correct |
139 ms |
40016 KB |
Output is correct |
27 |
Correct |
135 ms |
39988 KB |
Output is correct |
28 |
Correct |
137 ms |
40016 KB |
Output is correct |
29 |
Correct |
149 ms |
40528 KB |
Output is correct |
30 |
Correct |
148 ms |
40528 KB |
Output is correct |
31 |
Correct |
148 ms |
40532 KB |
Output is correct |
32 |
Correct |
142 ms |
40356 KB |
Output is correct |
33 |
Correct |
147 ms |
40484 KB |
Output is correct |
34 |
Correct |
114 ms |
39248 KB |
Output is correct |
35 |
Correct |
113 ms |
39204 KB |
Output is correct |
36 |
Correct |
120 ms |
39228 KB |
Output is correct |
37 |
Correct |
114 ms |
38944 KB |
Output is correct |
38 |
Correct |
113 ms |
39252 KB |
Output is correct |
39 |
Correct |
126 ms |
38924 KB |
Output is correct |
40 |
Correct |
96 ms |
37564 KB |
Output is correct |
41 |
Correct |
128 ms |
38668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
29276 KB |
Output is correct |
2 |
Correct |
6 ms |
29376 KB |
Output is correct |
3 |
Correct |
6 ms |
29300 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
6 ms |
29368 KB |
Output is correct |
6 |
Correct |
6 ms |
29272 KB |
Output is correct |
7 |
Correct |
6 ms |
29272 KB |
Output is correct |
8 |
Correct |
6 ms |
29276 KB |
Output is correct |
9 |
Correct |
6 ms |
29272 KB |
Output is correct |
10 |
Correct |
6 ms |
29276 KB |
Output is correct |
11 |
Correct |
8 ms |
29272 KB |
Output is correct |
12 |
Correct |
8 ms |
29532 KB |
Output is correct |
13 |
Correct |
8 ms |
29532 KB |
Output is correct |
14 |
Correct |
10 ms |
29532 KB |
Output is correct |
15 |
Correct |
9 ms |
29532 KB |
Output is correct |
16 |
Correct |
8 ms |
29532 KB |
Output is correct |
17 |
Correct |
8 ms |
29532 KB |
Output is correct |
18 |
Correct |
9 ms |
29376 KB |
Output is correct |
19 |
Correct |
1012 ms |
95584 KB |
Output is correct |
20 |
Correct |
1050 ms |
96552 KB |
Output is correct |
21 |
Correct |
1028 ms |
96368 KB |
Output is correct |
22 |
Correct |
1008 ms |
96592 KB |
Output is correct |
23 |
Correct |
911 ms |
90452 KB |
Output is correct |
24 |
Correct |
74 ms |
33460 KB |
Output is correct |
25 |
Correct |
76 ms |
33320 KB |
Output is correct |
26 |
Correct |
63 ms |
33380 KB |
Output is correct |
27 |
Correct |
65 ms |
33620 KB |
Output is correct |
28 |
Correct |
63 ms |
33620 KB |
Output is correct |
29 |
Correct |
53 ms |
33104 KB |
Output is correct |
30 |
Correct |
57 ms |
32904 KB |
Output is correct |
31 |
Correct |
58 ms |
33216 KB |
Output is correct |
32 |
Correct |
33 ms |
32120 KB |
Output is correct |
33 |
Correct |
58 ms |
33168 KB |
Output is correct |
34 |
Correct |
166 ms |
41116 KB |
Output is correct |
35 |
Correct |
159 ms |
41356 KB |
Output is correct |
36 |
Correct |
143 ms |
40556 KB |
Output is correct |
37 |
Correct |
146 ms |
40592 KB |
Output is correct |
38 |
Correct |
154 ms |
40768 KB |
Output is correct |
39 |
Correct |
129 ms |
39700 KB |
Output is correct |
40 |
Correct |
120 ms |
39716 KB |
Output is correct |
41 |
Correct |
139 ms |
40016 KB |
Output is correct |
42 |
Correct |
135 ms |
39988 KB |
Output is correct |
43 |
Correct |
137 ms |
40016 KB |
Output is correct |
44 |
Correct |
149 ms |
40528 KB |
Output is correct |
45 |
Correct |
148 ms |
40528 KB |
Output is correct |
46 |
Correct |
148 ms |
40532 KB |
Output is correct |
47 |
Correct |
142 ms |
40356 KB |
Output is correct |
48 |
Correct |
147 ms |
40484 KB |
Output is correct |
49 |
Correct |
114 ms |
39248 KB |
Output is correct |
50 |
Correct |
113 ms |
39204 KB |
Output is correct |
51 |
Correct |
120 ms |
39228 KB |
Output is correct |
52 |
Correct |
114 ms |
38944 KB |
Output is correct |
53 |
Correct |
113 ms |
39252 KB |
Output is correct |
54 |
Correct |
126 ms |
38924 KB |
Output is correct |
55 |
Correct |
96 ms |
37564 KB |
Output is correct |
56 |
Correct |
128 ms |
38668 KB |
Output is correct |
57 |
Correct |
1024 ms |
96820 KB |
Output is correct |
58 |
Correct |
1012 ms |
96700 KB |
Output is correct |
59 |
Correct |
935 ms |
95056 KB |
Output is correct |
60 |
Correct |
937 ms |
95208 KB |
Output is correct |
61 |
Correct |
947 ms |
95052 KB |
Output is correct |
62 |
Correct |
930 ms |
95056 KB |
Output is correct |
63 |
Correct |
621 ms |
85664 KB |
Output is correct |
64 |
Correct |
656 ms |
85844 KB |
Output is correct |
65 |
Correct |
815 ms |
88916 KB |
Output is correct |
66 |
Correct |
811 ms |
88708 KB |
Output is correct |
67 |
Correct |
991 ms |
88756 KB |
Output is correct |
68 |
Correct |
968 ms |
90656 KB |
Output is correct |
69 |
Correct |
1012 ms |
90428 KB |
Output is correct |
70 |
Correct |
996 ms |
90120 KB |
Output is correct |
71 |
Correct |
983 ms |
90116 KB |
Output is correct |
72 |
Correct |
964 ms |
90332 KB |
Output is correct |
73 |
Correct |
604 ms |
81564 KB |
Output is correct |
74 |
Correct |
645 ms |
82512 KB |
Output is correct |
75 |
Correct |
626 ms |
81740 KB |
Output is correct |
76 |
Correct |
608 ms |
81652 KB |
Output is correct |
77 |
Correct |
592 ms |
81628 KB |
Output is correct |
78 |
Correct |
813 ms |
85044 KB |
Output is correct |
79 |
Correct |
604 ms |
75200 KB |
Output is correct |
80 |
Correct |
760 ms |
80048 KB |
Output is correct |