#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
const ll INF = 1e9 + 177013;
const int MAXN = 1e6 + 123;
int w[MAXN];
int pv[MAXN];
int get_max(int l, int r) {
int mx = -INF;
int ans = 0;
for (int i = l; i < r; ++i) {
if (mx > w[i])
ans = max(ans, mx + w[i]);
else
mx = w[i];
}
return ans;
}
void merg_add(vector<pair<int, int>>& a, pair<int, int> vl) {
while (a.size() && a.back().second <= vl.second) {
a.pop_back();
}
a.push_back(vl);
}
vector<pair<int, int>> merg(vector<pair<int, int>> a, vector<pair<int, int>> b) {
vector<pair<int, int>> ans;
int n = a.size();
int m = b.size();
int j = 0;
for (int i = 0; i < n; ++i) {
while (j < m && b[j] < a[i]) {
merg_add(ans, b[j]);
++j;
}
merg_add(ans, a[i]);
}
while (j < m) {
merg_add(ans, b[j]);
++j;
}
return ans;
}
struct stree {
void resize(int n) {
sz = n;
t.resize(sz * 2 - 1);
init(0, 0, sz);
}
int get(int l, int r) {
return get(0, 0, sz, l, r);
}
private:
int sz;
vector<vector<pair<int, int>>> t;
void init(int ind, int lnd, int rnd) {
if (rnd - lnd == 1) {
if (pv[lnd] >= 0)
t[ind] = {{pv[lnd], w[lnd] + w[pv[lnd]]}};
} else {
int mnd = (lnd + rnd) / 2;
int chd = ind + (mnd - lnd) * 2;
init(ind + 1, lnd, mnd);
init(chd, mnd, rnd);
t[ind] = merg(t[ind + 1], t[chd]);
}
}
int get(int ind, int lnd, int rnd, int l, int r) {
int mnd = (lnd + rnd) / 2;
int chd = ind + (mnd - lnd) * 2;
if (lnd >= l && rnd <= r) {
int i = lower_bound(t[ind].begin(), t[ind].end(), pair<int, int>(l, -1)) - t[ind].begin();
if (i == t[ind].size())
return 0;
return t[ind][i].second;
}
if (lnd >= r || rnd <= l)
return 0;
return max(get(ind + 1, lnd, mnd, l, r), get(chd, mnd, rnd, l, r));
}
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i)
cin >> w[i];
vector<pair<int, int>> previnit = {{INF, -1}};
for (int i = 0; i < n; ++i) {
while (previnit.back().first <= w[i]) {
previnit.pop_back();
}
pv[i] = previnit.back().second;
previnit.push_back({w[i], i});
}
stree tr;
tr.resize(n);
for (int i = 0; i < m; ++i) {
int l, r, k;
cin >> l >> r >> k;
--l;
if (tr.get(l, r) > k) {
cout << 0 << '\n';
} else {
cout << 1 << '\n';
}
}
}
Compilation message
sortbooks.cpp: In member function 'int stree::get(int, int, int, int, int)':
sortbooks.cpp:82:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | if (i == t[ind].size())
| ~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
5 ms |
980 KB |
Output is correct |
13 |
Correct |
8 ms |
980 KB |
Output is correct |
14 |
Correct |
7 ms |
1108 KB |
Output is correct |
15 |
Correct |
6 ms |
1136 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
3 ms |
724 KB |
Output is correct |
18 |
Correct |
4 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2267 ms |
138744 KB |
Output is correct |
2 |
Correct |
2355 ms |
163728 KB |
Output is correct |
3 |
Correct |
2321 ms |
163524 KB |
Output is correct |
4 |
Correct |
2326 ms |
163580 KB |
Output is correct |
5 |
Correct |
1226 ms |
89956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
192 ms |
13804 KB |
Output is correct |
2 |
Correct |
158 ms |
13868 KB |
Output is correct |
3 |
Correct |
96 ms |
6476 KB |
Output is correct |
4 |
Correct |
99 ms |
6472 KB |
Output is correct |
5 |
Correct |
96 ms |
6504 KB |
Output is correct |
6 |
Correct |
84 ms |
7120 KB |
Output is correct |
7 |
Correct |
91 ms |
7268 KB |
Output is correct |
8 |
Correct |
107 ms |
11112 KB |
Output is correct |
9 |
Correct |
41 ms |
1100 KB |
Output is correct |
10 |
Correct |
99 ms |
10212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
5 ms |
980 KB |
Output is correct |
13 |
Correct |
8 ms |
980 KB |
Output is correct |
14 |
Correct |
7 ms |
1108 KB |
Output is correct |
15 |
Correct |
6 ms |
1136 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
3 ms |
724 KB |
Output is correct |
18 |
Correct |
4 ms |
852 KB |
Output is correct |
19 |
Correct |
432 ms |
27064 KB |
Output is correct |
20 |
Correct |
425 ms |
27212 KB |
Output is correct |
21 |
Correct |
349 ms |
27128 KB |
Output is correct |
22 |
Correct |
331 ms |
27048 KB |
Output is correct |
23 |
Correct |
323 ms |
27044 KB |
Output is correct |
24 |
Correct |
219 ms |
12240 KB |
Output is correct |
25 |
Correct |
192 ms |
12304 KB |
Output is correct |
26 |
Correct |
209 ms |
12320 KB |
Output is correct |
27 |
Correct |
233 ms |
12360 KB |
Output is correct |
28 |
Correct |
234 ms |
12292 KB |
Output is correct |
29 |
Correct |
243 ms |
12324 KB |
Output is correct |
30 |
Correct |
260 ms |
12304 KB |
Output is correct |
31 |
Correct |
222 ms |
12280 KB |
Output is correct |
32 |
Correct |
253 ms |
12248 KB |
Output is correct |
33 |
Correct |
231 ms |
12240 KB |
Output is correct |
34 |
Correct |
208 ms |
12320 KB |
Output is correct |
35 |
Correct |
182 ms |
12236 KB |
Output is correct |
36 |
Correct |
194 ms |
12284 KB |
Output is correct |
37 |
Correct |
167 ms |
12244 KB |
Output is correct |
38 |
Correct |
173 ms |
12268 KB |
Output is correct |
39 |
Correct |
198 ms |
17236 KB |
Output is correct |
40 |
Correct |
151 ms |
10540 KB |
Output is correct |
41 |
Correct |
235 ms |
19400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
5 ms |
980 KB |
Output is correct |
13 |
Correct |
8 ms |
980 KB |
Output is correct |
14 |
Correct |
7 ms |
1108 KB |
Output is correct |
15 |
Correct |
6 ms |
1136 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
3 ms |
724 KB |
Output is correct |
18 |
Correct |
4 ms |
852 KB |
Output is correct |
19 |
Correct |
2267 ms |
138744 KB |
Output is correct |
20 |
Correct |
2355 ms |
163728 KB |
Output is correct |
21 |
Correct |
2321 ms |
163524 KB |
Output is correct |
22 |
Correct |
2326 ms |
163580 KB |
Output is correct |
23 |
Correct |
1226 ms |
89956 KB |
Output is correct |
24 |
Correct |
192 ms |
13804 KB |
Output is correct |
25 |
Correct |
158 ms |
13868 KB |
Output is correct |
26 |
Correct |
96 ms |
6476 KB |
Output is correct |
27 |
Correct |
99 ms |
6472 KB |
Output is correct |
28 |
Correct |
96 ms |
6504 KB |
Output is correct |
29 |
Correct |
84 ms |
7120 KB |
Output is correct |
30 |
Correct |
91 ms |
7268 KB |
Output is correct |
31 |
Correct |
107 ms |
11112 KB |
Output is correct |
32 |
Correct |
41 ms |
1100 KB |
Output is correct |
33 |
Correct |
99 ms |
10212 KB |
Output is correct |
34 |
Correct |
432 ms |
27064 KB |
Output is correct |
35 |
Correct |
425 ms |
27212 KB |
Output is correct |
36 |
Correct |
349 ms |
27128 KB |
Output is correct |
37 |
Correct |
331 ms |
27048 KB |
Output is correct |
38 |
Correct |
323 ms |
27044 KB |
Output is correct |
39 |
Correct |
219 ms |
12240 KB |
Output is correct |
40 |
Correct |
192 ms |
12304 KB |
Output is correct |
41 |
Correct |
209 ms |
12320 KB |
Output is correct |
42 |
Correct |
233 ms |
12360 KB |
Output is correct |
43 |
Correct |
234 ms |
12292 KB |
Output is correct |
44 |
Correct |
243 ms |
12324 KB |
Output is correct |
45 |
Correct |
260 ms |
12304 KB |
Output is correct |
46 |
Correct |
222 ms |
12280 KB |
Output is correct |
47 |
Correct |
253 ms |
12248 KB |
Output is correct |
48 |
Correct |
231 ms |
12240 KB |
Output is correct |
49 |
Correct |
208 ms |
12320 KB |
Output is correct |
50 |
Correct |
182 ms |
12236 KB |
Output is correct |
51 |
Correct |
194 ms |
12284 KB |
Output is correct |
52 |
Correct |
167 ms |
12244 KB |
Output is correct |
53 |
Correct |
173 ms |
12268 KB |
Output is correct |
54 |
Correct |
198 ms |
17236 KB |
Output is correct |
55 |
Correct |
151 ms |
10540 KB |
Output is correct |
56 |
Correct |
235 ms |
19400 KB |
Output is correct |
57 |
Correct |
2673 ms |
164284 KB |
Output is correct |
58 |
Execution timed out |
3042 ms |
164328 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |