#include <bits/stdc++.h>
#define file "main"
#define maxn (int) 1e6 + 5
#define pii pair<int, int>
#define ll long long
#define gb(i, j) ((i >> j) & 1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
return uniform_int_distribution<ll> (l, r) (rng);
}
void iof() {
if(fopen(file".inp", "r")) {
freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
}
int n, Q, ans[maxn];
int a[maxn], pre[maxn];
int L[maxn][20], Log[maxn];
struct dl { int f, s, k, id; } qr[maxn];
vector<int> o[maxn];
int get(int i, int j) {
int kc = Log[j - i + 1];
return max( L[i][kc], L[j - (1 << kc) + 1][kc] );
}
//struct SEGMT {
// int st[4 * maxn];
//
// void build(int id = 1, int l = 1, int r = n) {
// if(l == r) return st[id] = pre[l] != 0 ? a[l] + a[ pre[l] ] : 0, void();
// int mid = (l + r) >> 1;
// build(_left), build(_right);
// st[id] = max(st[id * 2], st[id * 2 + 1]);
// }
// void update(int i, int id = 1, int l = 1, int r = n) {
// if(i < l || r < i) return ;
// if(l == r) return st[id] = 0, void();
// int mid = (l + r) >> 1;
// st[id] = max(st[id * 2], st[id * 2 + 1]);
// }
// int get(int u, int v, int id = 1, int l = 1, int r = n) {
// if(v < l || r < u) return 0;
// if(u <= l && r <= v) return st[id];
// int mid = (l + r) >> 1;
// return max( get(u, v, _left), get(u, v, _right) );
// }
//} ST;
struct SEGMENTREE {
int st[4 * maxn];
void build(int id = 1, int l = 1, int r = n) {
if(l == r) {
if(pre[l] != 0) st[id] = a[pre[l]] + a[l];
return ;
}
int mid = (l + r) >> 1;
build(_left), build(_right);
st[id] = max(st[id * 2], st[id * 2 + 1]);
}
void update(int i, int id = 1, int l = 1, int r = n) {
if(i < l || r < i) return ;
if(l == r) return st[id] = 0, void();
int mid = (l + r) >> 1;
update(i, _left), update(i, _right);
st[id] = max(st[id * 2], st[id * 2 + 1]);
}
int get(int u, int v, int id = 1, int l = 1, int r = n) {
if(v < l || r < u) return 0;
if(u <= l && r <= v) return st[id];
int mid = (l + r) >> 1;
return max( get(u, v, _left), get(u, v, _right) );
}
} ST;
void solve() {
cin >> n >> Q ;
for(int i = 1; i <= n; i ++) {
cin >> a[i], L[i][0] = a[i], Log[i] = __lg(i);
}
for(int i = 1; i <= Log[n]; i ++) {
for(int j = 1; j + (1 << i) - 1 <= n; j ++) {
L[j][i] = max(L[j][i - 1], L[j + (1 << (i - 1))][i - 1]);
}
}
for(int i = 1; i <= n; i ++) {
int l = 1, r = i;
while(l <= r) {
int mid = (l + r) >> 1;
if( get(mid, i) > a[i] ) l = mid + 1;
else r = mid - 1;
}
pre[i] = r;
o[r].push_back(i);
}
ST.build();
for(int i = 1; i <= Q; i ++) cin >> qr[i].f >> qr[i].s >> qr[i].k, qr[i].id = i;
sort(qr + 1, qr + Q + 1, [](dl x, dl y) {
return x.f < y.f;
});
int j = 1;
for(int i = 1; i <= Q; i ++) {
int l = qr[i].f, r = qr[i].s, k = qr[i].k, id = qr[i].id;
while(j < l) {
for(int pos : o[j]) ST.update(pos);
j ++;
}
ans[id] = ST.get(l, r) <= k;
}
for(int i = 1; i <= Q; i ++) cout << ans[i] << '\n';
}
int main()
{
iof();
int nTest = 1;
// cin >> nTest;
while(nTest --) {
solve();
}
return 0;
}
Compilation message
sortbooks.cpp: In function 'void iof()':
sortbooks.cpp:19:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen(file".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
35164 KB |
Output is correct |
2 |
Correct |
4 ms |
35164 KB |
Output is correct |
3 |
Correct |
4 ms |
35164 KB |
Output is correct |
4 |
Correct |
5 ms |
35344 KB |
Output is correct |
5 |
Correct |
5 ms |
35416 KB |
Output is correct |
6 |
Correct |
4 ms |
35164 KB |
Output is correct |
7 |
Correct |
5 ms |
35340 KB |
Output is correct |
8 |
Correct |
5 ms |
35252 KB |
Output is correct |
9 |
Correct |
4 ms |
35160 KB |
Output is correct |
10 |
Correct |
5 ms |
35164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
35164 KB |
Output is correct |
2 |
Correct |
4 ms |
35164 KB |
Output is correct |
3 |
Correct |
4 ms |
35164 KB |
Output is correct |
4 |
Correct |
5 ms |
35344 KB |
Output is correct |
5 |
Correct |
5 ms |
35416 KB |
Output is correct |
6 |
Correct |
4 ms |
35164 KB |
Output is correct |
7 |
Correct |
5 ms |
35340 KB |
Output is correct |
8 |
Correct |
5 ms |
35252 KB |
Output is correct |
9 |
Correct |
4 ms |
35160 KB |
Output is correct |
10 |
Correct |
5 ms |
35164 KB |
Output is correct |
11 |
Correct |
6 ms |
37464 KB |
Output is correct |
12 |
Correct |
6 ms |
37468 KB |
Output is correct |
13 |
Correct |
7 ms |
37652 KB |
Output is correct |
14 |
Correct |
7 ms |
37468 KB |
Output is correct |
15 |
Correct |
7 ms |
37468 KB |
Output is correct |
16 |
Correct |
7 ms |
37600 KB |
Output is correct |
17 |
Correct |
7 ms |
37468 KB |
Output is correct |
18 |
Correct |
6 ms |
37468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
743 ms |
160844 KB |
Output is correct |
2 |
Correct |
736 ms |
193872 KB |
Output is correct |
3 |
Correct |
750 ms |
193872 KB |
Output is correct |
4 |
Correct |
734 ms |
193912 KB |
Output is correct |
5 |
Correct |
589 ms |
177604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
49496 KB |
Output is correct |
2 |
Correct |
61 ms |
51376 KB |
Output is correct |
3 |
Correct |
48 ms |
48780 KB |
Output is correct |
4 |
Correct |
49 ms |
48592 KB |
Output is correct |
5 |
Correct |
49 ms |
48680 KB |
Output is correct |
6 |
Correct |
43 ms |
48512 KB |
Output is correct |
7 |
Correct |
44 ms |
48596 KB |
Output is correct |
8 |
Correct |
49 ms |
48832 KB |
Output is correct |
9 |
Correct |
24 ms |
38748 KB |
Output is correct |
10 |
Correct |
49 ms |
48720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
35164 KB |
Output is correct |
2 |
Correct |
4 ms |
35164 KB |
Output is correct |
3 |
Correct |
4 ms |
35164 KB |
Output is correct |
4 |
Correct |
5 ms |
35344 KB |
Output is correct |
5 |
Correct |
5 ms |
35416 KB |
Output is correct |
6 |
Correct |
4 ms |
35164 KB |
Output is correct |
7 |
Correct |
5 ms |
35340 KB |
Output is correct |
8 |
Correct |
5 ms |
35252 KB |
Output is correct |
9 |
Correct |
4 ms |
35160 KB |
Output is correct |
10 |
Correct |
5 ms |
35164 KB |
Output is correct |
11 |
Correct |
6 ms |
37464 KB |
Output is correct |
12 |
Correct |
6 ms |
37468 KB |
Output is correct |
13 |
Correct |
7 ms |
37652 KB |
Output is correct |
14 |
Correct |
7 ms |
37468 KB |
Output is correct |
15 |
Correct |
7 ms |
37468 KB |
Output is correct |
16 |
Correct |
7 ms |
37600 KB |
Output is correct |
17 |
Correct |
7 ms |
37468 KB |
Output is correct |
18 |
Correct |
6 ms |
37468 KB |
Output is correct |
19 |
Correct |
132 ms |
72112 KB |
Output is correct |
20 |
Correct |
136 ms |
72276 KB |
Output is correct |
21 |
Correct |
110 ms |
72016 KB |
Output is correct |
22 |
Correct |
110 ms |
72060 KB |
Output is correct |
23 |
Correct |
107 ms |
72020 KB |
Output is correct |
24 |
Correct |
102 ms |
68472 KB |
Output is correct |
25 |
Correct |
101 ms |
68556 KB |
Output is correct |
26 |
Correct |
113 ms |
68812 KB |
Output is correct |
27 |
Correct |
106 ms |
68808 KB |
Output is correct |
28 |
Correct |
110 ms |
68808 KB |
Output is correct |
29 |
Correct |
107 ms |
68688 KB |
Output is correct |
30 |
Correct |
108 ms |
68808 KB |
Output is correct |
31 |
Correct |
113 ms |
68732 KB |
Output is correct |
32 |
Correct |
108 ms |
68816 KB |
Output is correct |
33 |
Correct |
107 ms |
68744 KB |
Output is correct |
34 |
Correct |
91 ms |
68296 KB |
Output is correct |
35 |
Correct |
94 ms |
68312 KB |
Output is correct |
36 |
Correct |
91 ms |
68224 KB |
Output is correct |
37 |
Correct |
92 ms |
68304 KB |
Output is correct |
38 |
Correct |
95 ms |
68300 KB |
Output is correct |
39 |
Correct |
105 ms |
68304 KB |
Output is correct |
40 |
Correct |
76 ms |
55760 KB |
Output is correct |
41 |
Correct |
100 ms |
67668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
35164 KB |
Output is correct |
2 |
Correct |
4 ms |
35164 KB |
Output is correct |
3 |
Correct |
4 ms |
35164 KB |
Output is correct |
4 |
Correct |
5 ms |
35344 KB |
Output is correct |
5 |
Correct |
5 ms |
35416 KB |
Output is correct |
6 |
Correct |
4 ms |
35164 KB |
Output is correct |
7 |
Correct |
5 ms |
35340 KB |
Output is correct |
8 |
Correct |
5 ms |
35252 KB |
Output is correct |
9 |
Correct |
4 ms |
35160 KB |
Output is correct |
10 |
Correct |
5 ms |
35164 KB |
Output is correct |
11 |
Correct |
6 ms |
37464 KB |
Output is correct |
12 |
Correct |
6 ms |
37468 KB |
Output is correct |
13 |
Correct |
7 ms |
37652 KB |
Output is correct |
14 |
Correct |
7 ms |
37468 KB |
Output is correct |
15 |
Correct |
7 ms |
37468 KB |
Output is correct |
16 |
Correct |
7 ms |
37600 KB |
Output is correct |
17 |
Correct |
7 ms |
37468 KB |
Output is correct |
18 |
Correct |
6 ms |
37468 KB |
Output is correct |
19 |
Correct |
743 ms |
160844 KB |
Output is correct |
20 |
Correct |
736 ms |
193872 KB |
Output is correct |
21 |
Correct |
750 ms |
193872 KB |
Output is correct |
22 |
Correct |
734 ms |
193912 KB |
Output is correct |
23 |
Correct |
589 ms |
177604 KB |
Output is correct |
24 |
Correct |
78 ms |
49496 KB |
Output is correct |
25 |
Correct |
61 ms |
51376 KB |
Output is correct |
26 |
Correct |
48 ms |
48780 KB |
Output is correct |
27 |
Correct |
49 ms |
48592 KB |
Output is correct |
28 |
Correct |
49 ms |
48680 KB |
Output is correct |
29 |
Correct |
43 ms |
48512 KB |
Output is correct |
30 |
Correct |
44 ms |
48596 KB |
Output is correct |
31 |
Correct |
49 ms |
48832 KB |
Output is correct |
32 |
Correct |
24 ms |
38748 KB |
Output is correct |
33 |
Correct |
49 ms |
48720 KB |
Output is correct |
34 |
Correct |
132 ms |
72112 KB |
Output is correct |
35 |
Correct |
136 ms |
72276 KB |
Output is correct |
36 |
Correct |
110 ms |
72016 KB |
Output is correct |
37 |
Correct |
110 ms |
72060 KB |
Output is correct |
38 |
Correct |
107 ms |
72020 KB |
Output is correct |
39 |
Correct |
102 ms |
68472 KB |
Output is correct |
40 |
Correct |
101 ms |
68556 KB |
Output is correct |
41 |
Correct |
113 ms |
68812 KB |
Output is correct |
42 |
Correct |
106 ms |
68808 KB |
Output is correct |
43 |
Correct |
110 ms |
68808 KB |
Output is correct |
44 |
Correct |
107 ms |
68688 KB |
Output is correct |
45 |
Correct |
108 ms |
68808 KB |
Output is correct |
46 |
Correct |
113 ms |
68732 KB |
Output is correct |
47 |
Correct |
108 ms |
68816 KB |
Output is correct |
48 |
Correct |
107 ms |
68744 KB |
Output is correct |
49 |
Correct |
91 ms |
68296 KB |
Output is correct |
50 |
Correct |
94 ms |
68312 KB |
Output is correct |
51 |
Correct |
91 ms |
68224 KB |
Output is correct |
52 |
Correct |
92 ms |
68304 KB |
Output is correct |
53 |
Correct |
95 ms |
68300 KB |
Output is correct |
54 |
Correct |
105 ms |
68304 KB |
Output is correct |
55 |
Correct |
76 ms |
55760 KB |
Output is correct |
56 |
Correct |
100 ms |
67668 KB |
Output is correct |
57 |
Correct |
764 ms |
194428 KB |
Output is correct |
58 |
Correct |
736 ms |
194644 KB |
Output is correct |
59 |
Correct |
682 ms |
194384 KB |
Output is correct |
60 |
Correct |
683 ms |
194548 KB |
Output is correct |
61 |
Correct |
696 ms |
194628 KB |
Output is correct |
62 |
Correct |
742 ms |
194644 KB |
Output is correct |
63 |
Correct |
566 ms |
176316 KB |
Output is correct |
64 |
Correct |
535 ms |
176320 KB |
Output is correct |
65 |
Correct |
585 ms |
178108 KB |
Output is correct |
66 |
Correct |
573 ms |
178116 KB |
Output is correct |
67 |
Correct |
581 ms |
178112 KB |
Output is correct |
68 |
Correct |
645 ms |
178368 KB |
Output is correct |
69 |
Correct |
585 ms |
178300 KB |
Output is correct |
70 |
Correct |
632 ms |
178416 KB |
Output is correct |
71 |
Correct |
592 ms |
178364 KB |
Output is correct |
72 |
Correct |
609 ms |
178368 KB |
Output is correct |
73 |
Correct |
518 ms |
174728 KB |
Output is correct |
74 |
Correct |
522 ms |
175744 KB |
Output is correct |
75 |
Correct |
503 ms |
174788 KB |
Output is correct |
76 |
Correct |
495 ms |
174924 KB |
Output is correct |
77 |
Correct |
539 ms |
174740 KB |
Output is correct |
78 |
Correct |
573 ms |
177600 KB |
Output is correct |
79 |
Correct |
402 ms |
128456 KB |
Output is correct |
80 |
Correct |
598 ms |
173124 KB |
Output is correct |