#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define y1 tim
#define endl "\n"
#define sz(x) (long long)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
int w[1000010], mx[4000010], ans[4000010], Max, y, l, r, k, n, m;
vector <int> v[4000010];
void build (int l, int r, int x)
{
if (l == r)
{
mx[x] = w[l];
v[x].push_back(w[l]);
ans[x] = 0;
return;
}
build (l, (l + r) / 2, x * 2);
build ((l + r) / 2 + 1, r, x * 2 + 1);
mx[x] = max (mx[x * 2], mx[x * 2 + 1]);
v[x] = v[x * 2];
ans[x] = max (ans[x * 2], ans[x * 2 + 1]);
int mx1 = -1;
for (int i = 0; i < v[x * 2 + 1].size(); i++)
{
if (v[x * 2 + 1][i] < mx[x * 2]) mx1 = v[x * 2 + 1][i];
v[x].push_back(v[x * 2 + 1][i]);
}
sort (v[x].begin(), v[x].end());
if (mx1 != -1) ans[x] = max (ans[x], mx[x * 2] + mx1);
}
void get (int l, int r, int tl, int tr, int x)
{
tl = max (l, tl);
tr = min (r, tr);
if (l == tl and r == tr)
{
if (Max == 0 and y == 0)
{
y = ans[x];
Max = mx[x];
}
else
{
y = max (y, ans[x]);
int pos = lower_bound(v[x].begin(), v[x].end(), Max) - v[x].begin();
if (pos != 0)
{
pos--;
y = max (y, Max + v[x][pos]);
}
Max = max (Max, mx[x]);
}
return;
}
if (tl <= (l + r) / 2) get (l, (l + r) / 2, tl, tr, x * 2);
if (tr >= (l + r) / 2 + 1) get ((l + r) / 2 + 1, r, tl, tr, x * 2 + 1);
}
int main()
{
srand(time(0));
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
cin >> w[i];
}
build (1, n, 1);
for (int i = 1; i <= m; i++)
{
cin >> l >> r >> k;
y = 0;
Max = 0;
get (1, n, l, r, 1);
if (y <= k) cout << 1 << endl;
else cout << 0 << endl;
}
return 0;
}
Compilation message
sortbooks.cpp: In function 'void build(int, int, int)':
sortbooks.cpp:35:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for (int i = 0; i < v[x * 2 + 1].size(); i++)
| ~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
94212 KB |
Output is correct |
2 |
Correct |
44 ms |
94188 KB |
Output is correct |
3 |
Correct |
44 ms |
94192 KB |
Output is correct |
4 |
Correct |
45 ms |
94164 KB |
Output is correct |
5 |
Correct |
44 ms |
94160 KB |
Output is correct |
6 |
Correct |
57 ms |
94180 KB |
Output is correct |
7 |
Correct |
49 ms |
94276 KB |
Output is correct |
8 |
Correct |
45 ms |
94212 KB |
Output is correct |
9 |
Correct |
45 ms |
94252 KB |
Output is correct |
10 |
Correct |
45 ms |
94200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
94212 KB |
Output is correct |
2 |
Correct |
44 ms |
94188 KB |
Output is correct |
3 |
Correct |
44 ms |
94192 KB |
Output is correct |
4 |
Correct |
45 ms |
94164 KB |
Output is correct |
5 |
Correct |
44 ms |
94160 KB |
Output is correct |
6 |
Correct |
57 ms |
94180 KB |
Output is correct |
7 |
Correct |
49 ms |
94276 KB |
Output is correct |
8 |
Correct |
45 ms |
94212 KB |
Output is correct |
9 |
Correct |
45 ms |
94252 KB |
Output is correct |
10 |
Correct |
45 ms |
94200 KB |
Output is correct |
11 |
Correct |
46 ms |
94404 KB |
Output is correct |
12 |
Correct |
49 ms |
94820 KB |
Output is correct |
13 |
Correct |
49 ms |
94788 KB |
Output is correct |
14 |
Correct |
59 ms |
94852 KB |
Output is correct |
15 |
Correct |
51 ms |
94924 KB |
Output is correct |
16 |
Correct |
48 ms |
94912 KB |
Output is correct |
17 |
Correct |
48 ms |
94668 KB |
Output is correct |
18 |
Correct |
48 ms |
94804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3082 ms |
247536 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
265 ms |
108636 KB |
Output is correct |
2 |
Correct |
216 ms |
108640 KB |
Output is correct |
3 |
Correct |
196 ms |
108720 KB |
Output is correct |
4 |
Correct |
205 ms |
108936 KB |
Output is correct |
5 |
Correct |
194 ms |
108680 KB |
Output is correct |
6 |
Correct |
168 ms |
108668 KB |
Output is correct |
7 |
Correct |
176 ms |
108800 KB |
Output is correct |
8 |
Correct |
192 ms |
108712 KB |
Output is correct |
9 |
Correct |
78 ms |
94612 KB |
Output is correct |
10 |
Correct |
170 ms |
108708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
94212 KB |
Output is correct |
2 |
Correct |
44 ms |
94188 KB |
Output is correct |
3 |
Correct |
44 ms |
94192 KB |
Output is correct |
4 |
Correct |
45 ms |
94164 KB |
Output is correct |
5 |
Correct |
44 ms |
94160 KB |
Output is correct |
6 |
Correct |
57 ms |
94180 KB |
Output is correct |
7 |
Correct |
49 ms |
94276 KB |
Output is correct |
8 |
Correct |
45 ms |
94212 KB |
Output is correct |
9 |
Correct |
45 ms |
94252 KB |
Output is correct |
10 |
Correct |
45 ms |
94200 KB |
Output is correct |
11 |
Correct |
46 ms |
94404 KB |
Output is correct |
12 |
Correct |
49 ms |
94820 KB |
Output is correct |
13 |
Correct |
49 ms |
94788 KB |
Output is correct |
14 |
Correct |
59 ms |
94852 KB |
Output is correct |
15 |
Correct |
51 ms |
94924 KB |
Output is correct |
16 |
Correct |
48 ms |
94912 KB |
Output is correct |
17 |
Correct |
48 ms |
94668 KB |
Output is correct |
18 |
Correct |
48 ms |
94804 KB |
Output is correct |
19 |
Correct |
576 ms |
123800 KB |
Output is correct |
20 |
Correct |
573 ms |
123704 KB |
Output is correct |
21 |
Correct |
478 ms |
123792 KB |
Output is correct |
22 |
Correct |
490 ms |
123728 KB |
Output is correct |
23 |
Correct |
501 ms |
123900 KB |
Output is correct |
24 |
Correct |
348 ms |
123788 KB |
Output is correct |
25 |
Correct |
362 ms |
123892 KB |
Output is correct |
26 |
Correct |
439 ms |
123940 KB |
Output is correct |
27 |
Correct |
437 ms |
123768 KB |
Output is correct |
28 |
Correct |
468 ms |
123796 KB |
Output is correct |
29 |
Correct |
454 ms |
124120 KB |
Output is correct |
30 |
Correct |
482 ms |
124136 KB |
Output is correct |
31 |
Correct |
492 ms |
124152 KB |
Output is correct |
32 |
Correct |
444 ms |
124216 KB |
Output is correct |
33 |
Correct |
426 ms |
124156 KB |
Output is correct |
34 |
Correct |
340 ms |
124308 KB |
Output is correct |
35 |
Correct |
342 ms |
124128 KB |
Output is correct |
36 |
Correct |
344 ms |
124136 KB |
Output is correct |
37 |
Correct |
329 ms |
124156 KB |
Output is correct |
38 |
Correct |
349 ms |
124280 KB |
Output is correct |
39 |
Correct |
397 ms |
124204 KB |
Output is correct |
40 |
Correct |
270 ms |
113296 KB |
Output is correct |
41 |
Correct |
344 ms |
124160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
94212 KB |
Output is correct |
2 |
Correct |
44 ms |
94188 KB |
Output is correct |
3 |
Correct |
44 ms |
94192 KB |
Output is correct |
4 |
Correct |
45 ms |
94164 KB |
Output is correct |
5 |
Correct |
44 ms |
94160 KB |
Output is correct |
6 |
Correct |
57 ms |
94180 KB |
Output is correct |
7 |
Correct |
49 ms |
94276 KB |
Output is correct |
8 |
Correct |
45 ms |
94212 KB |
Output is correct |
9 |
Correct |
45 ms |
94252 KB |
Output is correct |
10 |
Correct |
45 ms |
94200 KB |
Output is correct |
11 |
Correct |
46 ms |
94404 KB |
Output is correct |
12 |
Correct |
49 ms |
94820 KB |
Output is correct |
13 |
Correct |
49 ms |
94788 KB |
Output is correct |
14 |
Correct |
59 ms |
94852 KB |
Output is correct |
15 |
Correct |
51 ms |
94924 KB |
Output is correct |
16 |
Correct |
48 ms |
94912 KB |
Output is correct |
17 |
Correct |
48 ms |
94668 KB |
Output is correct |
18 |
Correct |
48 ms |
94804 KB |
Output is correct |
19 |
Execution timed out |
3082 ms |
247536 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |