#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;
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 |
35160 KB |
Output is correct |
2 |
Correct |
4 ms |
35164 KB |
Output is correct |
3 |
Correct |
4 ms |
35164 KB |
Output is correct |
4 |
Correct |
4 ms |
35164 KB |
Output is correct |
5 |
Correct |
5 ms |
35160 KB |
Output is correct |
6 |
Correct |
4 ms |
35164 KB |
Output is correct |
7 |
Correct |
5 ms |
35160 KB |
Output is correct |
8 |
Correct |
5 ms |
35360 KB |
Output is correct |
9 |
Correct |
5 ms |
35160 KB |
Output is correct |
10 |
Correct |
5 ms |
35160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
35160 KB |
Output is correct |
2 |
Correct |
4 ms |
35164 KB |
Output is correct |
3 |
Correct |
4 ms |
35164 KB |
Output is correct |
4 |
Correct |
4 ms |
35164 KB |
Output is correct |
5 |
Correct |
5 ms |
35160 KB |
Output is correct |
6 |
Correct |
4 ms |
35164 KB |
Output is correct |
7 |
Correct |
5 ms |
35160 KB |
Output is correct |
8 |
Correct |
5 ms |
35360 KB |
Output is correct |
9 |
Correct |
5 ms |
35160 KB |
Output is correct |
10 |
Correct |
5 ms |
35160 KB |
Output is correct |
11 |
Correct |
6 ms |
37212 KB |
Output is correct |
12 |
Correct |
8 ms |
37548 KB |
Output is correct |
13 |
Correct |
7 ms |
37468 KB |
Output is correct |
14 |
Correct |
10 ms |
37544 KB |
Output is correct |
15 |
Correct |
7 ms |
37468 KB |
Output is correct |
16 |
Correct |
7 ms |
37348 KB |
Output is correct |
17 |
Correct |
6 ms |
37392 KB |
Output is correct |
18 |
Correct |
6 ms |
37468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
729 ms |
160972 KB |
Output is correct |
2 |
Correct |
732 ms |
161084 KB |
Output is correct |
3 |
Correct |
736 ms |
161104 KB |
Output is correct |
4 |
Correct |
724 ms |
161004 KB |
Output is correct |
5 |
Correct |
594 ms |
148856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
49432 KB |
Output is correct |
2 |
Correct |
51 ms |
49444 KB |
Output is correct |
3 |
Correct |
49 ms |
48336 KB |
Output is correct |
4 |
Correct |
52 ms |
48336 KB |
Output is correct |
5 |
Correct |
48 ms |
48336 KB |
Output is correct |
6 |
Correct |
43 ms |
48332 KB |
Output is correct |
7 |
Correct |
52 ms |
48336 KB |
Output is correct |
8 |
Correct |
47 ms |
48344 KB |
Output is correct |
9 |
Correct |
24 ms |
37468 KB |
Output is correct |
10 |
Correct |
45 ms |
48468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
35160 KB |
Output is correct |
2 |
Correct |
4 ms |
35164 KB |
Output is correct |
3 |
Correct |
4 ms |
35164 KB |
Output is correct |
4 |
Correct |
4 ms |
35164 KB |
Output is correct |
5 |
Correct |
5 ms |
35160 KB |
Output is correct |
6 |
Correct |
4 ms |
35164 KB |
Output is correct |
7 |
Correct |
5 ms |
35160 KB |
Output is correct |
8 |
Correct |
5 ms |
35360 KB |
Output is correct |
9 |
Correct |
5 ms |
35160 KB |
Output is correct |
10 |
Correct |
5 ms |
35160 KB |
Output is correct |
11 |
Correct |
6 ms |
37212 KB |
Output is correct |
12 |
Correct |
8 ms |
37548 KB |
Output is correct |
13 |
Correct |
7 ms |
37468 KB |
Output is correct |
14 |
Correct |
10 ms |
37544 KB |
Output is correct |
15 |
Correct |
7 ms |
37468 KB |
Output is correct |
16 |
Correct |
7 ms |
37348 KB |
Output is correct |
17 |
Correct |
6 ms |
37392 KB |
Output is correct |
18 |
Correct |
6 ms |
37468 KB |
Output is correct |
19 |
Correct |
128 ms |
65656 KB |
Output is correct |
20 |
Correct |
152 ms |
65656 KB |
Output is correct |
21 |
Correct |
110 ms |
65616 KB |
Output is correct |
22 |
Correct |
121 ms |
65660 KB |
Output is correct |
23 |
Correct |
113 ms |
65616 KB |
Output is correct |
24 |
Correct |
101 ms |
63344 KB |
Output is correct |
25 |
Correct |
94 ms |
63436 KB |
Output is correct |
26 |
Correct |
113 ms |
63436 KB |
Output is correct |
27 |
Correct |
104 ms |
63352 KB |
Output is correct |
28 |
Correct |
105 ms |
63384 KB |
Output is correct |
29 |
Correct |
108 ms |
63432 KB |
Output is correct |
30 |
Correct |
110 ms |
63436 KB |
Output is correct |
31 |
Correct |
104 ms |
63352 KB |
Output is correct |
32 |
Correct |
108 ms |
63436 KB |
Output is correct |
33 |
Correct |
105 ms |
63432 KB |
Output is correct |
34 |
Correct |
92 ms |
63356 KB |
Output is correct |
35 |
Correct |
91 ms |
63400 KB |
Output is correct |
36 |
Correct |
94 ms |
63436 KB |
Output is correct |
37 |
Correct |
90 ms |
63436 KB |
Output is correct |
38 |
Correct |
90 ms |
63352 KB |
Output is correct |
39 |
Correct |
97 ms |
63948 KB |
Output is correct |
40 |
Correct |
75 ms |
52944 KB |
Output is correct |
41 |
Correct |
97 ms |
63572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
35160 KB |
Output is correct |
2 |
Correct |
4 ms |
35164 KB |
Output is correct |
3 |
Correct |
4 ms |
35164 KB |
Output is correct |
4 |
Correct |
4 ms |
35164 KB |
Output is correct |
5 |
Correct |
5 ms |
35160 KB |
Output is correct |
6 |
Correct |
4 ms |
35164 KB |
Output is correct |
7 |
Correct |
5 ms |
35160 KB |
Output is correct |
8 |
Correct |
5 ms |
35360 KB |
Output is correct |
9 |
Correct |
5 ms |
35160 KB |
Output is correct |
10 |
Correct |
5 ms |
35160 KB |
Output is correct |
11 |
Correct |
6 ms |
37212 KB |
Output is correct |
12 |
Correct |
8 ms |
37548 KB |
Output is correct |
13 |
Correct |
7 ms |
37468 KB |
Output is correct |
14 |
Correct |
10 ms |
37544 KB |
Output is correct |
15 |
Correct |
7 ms |
37468 KB |
Output is correct |
16 |
Correct |
7 ms |
37348 KB |
Output is correct |
17 |
Correct |
6 ms |
37392 KB |
Output is correct |
18 |
Correct |
6 ms |
37468 KB |
Output is correct |
19 |
Correct |
729 ms |
160972 KB |
Output is correct |
20 |
Correct |
732 ms |
161084 KB |
Output is correct |
21 |
Correct |
736 ms |
161104 KB |
Output is correct |
22 |
Correct |
724 ms |
161004 KB |
Output is correct |
23 |
Correct |
594 ms |
148856 KB |
Output is correct |
24 |
Correct |
59 ms |
49432 KB |
Output is correct |
25 |
Correct |
51 ms |
49444 KB |
Output is correct |
26 |
Correct |
49 ms |
48336 KB |
Output is correct |
27 |
Correct |
52 ms |
48336 KB |
Output is correct |
28 |
Correct |
48 ms |
48336 KB |
Output is correct |
29 |
Correct |
43 ms |
48332 KB |
Output is correct |
30 |
Correct |
52 ms |
48336 KB |
Output is correct |
31 |
Correct |
47 ms |
48344 KB |
Output is correct |
32 |
Correct |
24 ms |
37468 KB |
Output is correct |
33 |
Correct |
45 ms |
48468 KB |
Output is correct |
34 |
Correct |
128 ms |
65656 KB |
Output is correct |
35 |
Correct |
152 ms |
65656 KB |
Output is correct |
36 |
Correct |
110 ms |
65616 KB |
Output is correct |
37 |
Correct |
121 ms |
65660 KB |
Output is correct |
38 |
Correct |
113 ms |
65616 KB |
Output is correct |
39 |
Correct |
101 ms |
63344 KB |
Output is correct |
40 |
Correct |
94 ms |
63436 KB |
Output is correct |
41 |
Correct |
113 ms |
63436 KB |
Output is correct |
42 |
Correct |
104 ms |
63352 KB |
Output is correct |
43 |
Correct |
105 ms |
63384 KB |
Output is correct |
44 |
Correct |
108 ms |
63432 KB |
Output is correct |
45 |
Correct |
110 ms |
63436 KB |
Output is correct |
46 |
Correct |
104 ms |
63352 KB |
Output is correct |
47 |
Correct |
108 ms |
63436 KB |
Output is correct |
48 |
Correct |
105 ms |
63432 KB |
Output is correct |
49 |
Correct |
92 ms |
63356 KB |
Output is correct |
50 |
Correct |
91 ms |
63400 KB |
Output is correct |
51 |
Correct |
94 ms |
63436 KB |
Output is correct |
52 |
Correct |
90 ms |
63436 KB |
Output is correct |
53 |
Correct |
90 ms |
63352 KB |
Output is correct |
54 |
Correct |
97 ms |
63948 KB |
Output is correct |
55 |
Correct |
75 ms |
52944 KB |
Output is correct |
56 |
Correct |
97 ms |
63572 KB |
Output is correct |
57 |
Correct |
742 ms |
160848 KB |
Output is correct |
58 |
Correct |
727 ms |
160848 KB |
Output is correct |
59 |
Correct |
696 ms |
160852 KB |
Output is correct |
60 |
Correct |
676 ms |
160852 KB |
Output is correct |
61 |
Correct |
712 ms |
160860 KB |
Output is correct |
62 |
Correct |
742 ms |
161024 KB |
Output is correct |
63 |
Correct |
569 ms |
148676 KB |
Output is correct |
64 |
Correct |
517 ms |
148932 KB |
Output is correct |
65 |
Correct |
587 ms |
148904 KB |
Output is correct |
66 |
Correct |
575 ms |
148668 KB |
Output is correct |
67 |
Correct |
588 ms |
148872 KB |
Output is correct |
68 |
Correct |
602 ms |
148684 KB |
Output is correct |
69 |
Correct |
638 ms |
148692 KB |
Output is correct |
70 |
Correct |
604 ms |
148920 KB |
Output is correct |
71 |
Correct |
583 ms |
148896 KB |
Output is correct |
72 |
Correct |
587 ms |
148668 KB |
Output is correct |
73 |
Correct |
512 ms |
148832 KB |
Output is correct |
74 |
Correct |
515 ms |
148672 KB |
Output is correct |
75 |
Correct |
512 ms |
148668 KB |
Output is correct |
76 |
Correct |
488 ms |
148668 KB |
Output is correct |
77 |
Correct |
538 ms |
148672 KB |
Output is correct |
78 |
Correct |
562 ms |
152768 KB |
Output is correct |
79 |
Correct |
370 ms |
107400 KB |
Output is correct |
80 |
Correct |
566 ms |
149572 KB |
Output is correct |