#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().first == vl.first) {
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;
}
int s = ans.size();
for (int i = s - 2; i >= 0; --i) {
ans[i].second = max(ans[i].second, ans[i + 1].second);
}
return ans;
}
struct stree {
void resize(int n) {
sz = n;
t.resize(sz * 2 - 1);
go_down.resize(sz * 2 - 1);
init(0, 0, sz);
}
int get(int l, int r) {
return get(0, 0, sz, l, r, lower_bound(t[0].begin(), t[0].end(), pair<int, int>(l, -1)) - t[0].begin());
}
private:
int sz;
vector<vector<pair<int, int>>> t;
vector<vector<pair<int, int>>> go_down;
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 s = t[ind].size();
int n = t[ind + 1].size();
int m = t[chd].size();
go_down[ind].resize(s);
int upi = s - 1;
if (t[ind + 1].size()) {
while (upi >= 0 && t[ind][upi].first > t[ind + 1].back().first) {
go_down[ind][upi].first = n;
--upi;
}
for (int i = n - 1; i >= 0; --i) {
while (upi >= 0 && (i == 0 || t[ind][upi].first > t[ind + 1][i - 1].first)) {
go_down[ind][upi].first = i;
--upi;
}
}
}
upi = s - 1;
if (t[chd].size()) {
while (upi >= 0 && t[ind][upi].first > t[chd].back().first) {
go_down[ind][upi].second = m;
--upi;
}
for (int j = m - 1; j >= 0; --j) {
while (upi >= 0 && (j == 0 || t[ind][upi].first > t[chd][j - 1].first)) {
go_down[ind][upi].second = j;
--upi;
}
}
}
}
}
int get(int ind, int lnd, int rnd, int l, int r, int i) {
if (i == t[ind].size())
return 0;
int mnd = (lnd + rnd) / 2;
int chd = ind + (mnd - lnd) * 2;
if (lnd >= l && rnd <= r) {
return t[ind][i].second;
}
if (lnd >= r || rnd <= l)
return 0;
return max(get(ind + 1, lnd, mnd, l, r, go_down[ind][i].first), get(chd, mnd, rnd, l, r, go_down[ind][i].second));
}
};
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, int)':
sortbooks.cpp:114:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | if (i == t[ind].size())
| ~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
4 ms |
724 KB |
Output is correct |
12 |
Correct |
7 ms |
1944 KB |
Output is correct |
13 |
Correct |
7 ms |
1956 KB |
Output is correct |
14 |
Correct |
9 ms |
2004 KB |
Output is correct |
15 |
Correct |
9 ms |
2004 KB |
Output is correct |
16 |
Correct |
5 ms |
1236 KB |
Output is correct |
17 |
Correct |
3 ms |
852 KB |
Output is correct |
18 |
Correct |
4 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
508 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
359 ms |
33228 KB |
Output is correct |
2 |
Correct |
223 ms |
34084 KB |
Output is correct |
3 |
Correct |
44 ms |
11464 KB |
Output is correct |
4 |
Correct |
55 ms |
11400 KB |
Output is correct |
5 |
Correct |
49 ms |
11388 KB |
Output is correct |
6 |
Correct |
63 ms |
12376 KB |
Output is correct |
7 |
Correct |
47 ms |
12168 KB |
Output is correct |
8 |
Correct |
89 ms |
17916 KB |
Output is correct |
9 |
Correct |
33 ms |
1028 KB |
Output is correct |
10 |
Correct |
81 ms |
16588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
4 ms |
724 KB |
Output is correct |
12 |
Correct |
7 ms |
1944 KB |
Output is correct |
13 |
Correct |
7 ms |
1956 KB |
Output is correct |
14 |
Correct |
9 ms |
2004 KB |
Output is correct |
15 |
Correct |
9 ms |
2004 KB |
Output is correct |
16 |
Correct |
5 ms |
1236 KB |
Output is correct |
17 |
Correct |
3 ms |
852 KB |
Output is correct |
18 |
Correct |
4 ms |
1236 KB |
Output is correct |
19 |
Correct |
803 ms |
68488 KB |
Output is correct |
20 |
Correct |
847 ms |
68472 KB |
Output is correct |
21 |
Correct |
480 ms |
68420 KB |
Output is correct |
22 |
Correct |
533 ms |
68484 KB |
Output is correct |
23 |
Correct |
524 ms |
68592 KB |
Output is correct |
24 |
Correct |
163 ms |
21544 KB |
Output is correct |
25 |
Correct |
111 ms |
21548 KB |
Output is correct |
26 |
Correct |
112 ms |
21596 KB |
Output is correct |
27 |
Correct |
117 ms |
21476 KB |
Output is correct |
28 |
Correct |
117 ms |
21592 KB |
Output is correct |
29 |
Correct |
106 ms |
21600 KB |
Output is correct |
30 |
Correct |
131 ms |
21560 KB |
Output is correct |
31 |
Correct |
148 ms |
21524 KB |
Output is correct |
32 |
Correct |
121 ms |
21472 KB |
Output is correct |
33 |
Correct |
115 ms |
21608 KB |
Output is correct |
34 |
Correct |
142 ms |
21552 KB |
Output is correct |
35 |
Correct |
134 ms |
21620 KB |
Output is correct |
36 |
Correct |
132 ms |
21600 KB |
Output is correct |
37 |
Correct |
125 ms |
21500 KB |
Output is correct |
38 |
Correct |
139 ms |
21568 KB |
Output is correct |
39 |
Correct |
223 ms |
37184 KB |
Output is correct |
40 |
Correct |
103 ms |
17592 KB |
Output is correct |
41 |
Correct |
215 ms |
32280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
4 ms |
724 KB |
Output is correct |
12 |
Correct |
7 ms |
1944 KB |
Output is correct |
13 |
Correct |
7 ms |
1956 KB |
Output is correct |
14 |
Correct |
9 ms |
2004 KB |
Output is correct |
15 |
Correct |
9 ms |
2004 KB |
Output is correct |
16 |
Correct |
5 ms |
1236 KB |
Output is correct |
17 |
Correct |
3 ms |
852 KB |
Output is correct |
18 |
Correct |
4 ms |
1236 KB |
Output is correct |
19 |
Runtime error |
508 ms |
262144 KB |
Execution killed with signal 9 |
20 |
Halted |
0 ms |
0 KB |
- |