#include <bits/stdc++.h>
#define Y8o "Hedgehog Daniyar and Algorithms"
#define maxn (int) 1e6 + 5
#define ll long long
#define pii pair<ll, ll>
#define gb(i, j) ((i >> j) & 1)
#define all(x) x.begin(), x.end()
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
//#define f first
//#define s second
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(Y8o".inp", "r"))
{
freopen(Y8o".inp", "r", stdin);
// freopen(Y8o".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
}
void ctime() {
cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
int n, Q, a[maxn], ans[maxn];
int L[maxn][20], Log[maxn], pre[maxn];
vector<int> cur[maxn];
struct dl { int l, r, k, id; } qr[maxn];
int get(int i, int j) {
int kc = Log[j - i + 1];
return max( L[i][kc], L[j - (1 << kc) + 1][kc] );
}
int st[4 * maxn];
struct SEGMENTREE {
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;
cur[r].push_back(i);
}
ST.build();
for(int i =1 ; i <= Q; i ++) {
int l, r, k; cin >> l >> r >> k;
qr[i] = {l, r, k, i};
}
sort(qr + 1, qr + Q + 1, [](dl x, dl y) {
return x.l < y.l;
});
int j = 1;
for(int i = 1; i <= Q; i ++) {
int l = qr[i].l, r = qr[i].r, k = qr[i].k, id = qr[i].id;
while(j < l) {
for(int pos : cur[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();
}
ctime();
return 0;
}
Compilation message
sortbooks.cpp: In function 'void iof()':
sortbooks.cpp:23:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | freopen(Y8o".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
35164 KB |
Output is correct |
2 |
Correct |
4 ms |
35164 KB |
Output is correct |
3 |
Correct |
5 ms |
35164 KB |
Output is correct |
4 |
Correct |
6 ms |
35164 KB |
Output is correct |
5 |
Correct |
5 ms |
35164 KB |
Output is correct |
6 |
Correct |
4 ms |
35164 KB |
Output is correct |
7 |
Correct |
5 ms |
35164 KB |
Output is correct |
8 |
Correct |
4 ms |
35164 KB |
Output is correct |
9 |
Correct |
5 ms |
35256 KB |
Output is correct |
10 |
Correct |
4 ms |
35164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
35164 KB |
Output is correct |
2 |
Correct |
4 ms |
35164 KB |
Output is correct |
3 |
Correct |
5 ms |
35164 KB |
Output is correct |
4 |
Correct |
6 ms |
35164 KB |
Output is correct |
5 |
Correct |
5 ms |
35164 KB |
Output is correct |
6 |
Correct |
4 ms |
35164 KB |
Output is correct |
7 |
Correct |
5 ms |
35164 KB |
Output is correct |
8 |
Correct |
4 ms |
35164 KB |
Output is correct |
9 |
Correct |
5 ms |
35256 KB |
Output is correct |
10 |
Correct |
4 ms |
35164 KB |
Output is correct |
11 |
Correct |
6 ms |
35164 KB |
Output is correct |
12 |
Correct |
7 ms |
35420 KB |
Output is correct |
13 |
Correct |
10 ms |
35420 KB |
Output is correct |
14 |
Correct |
7 ms |
35336 KB |
Output is correct |
15 |
Correct |
7 ms |
35420 KB |
Output is correct |
16 |
Correct |
6 ms |
35420 KB |
Output is correct |
17 |
Correct |
6 ms |
35420 KB |
Output is correct |
18 |
Correct |
6 ms |
35420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
687 ms |
161132 KB |
Output is correct |
2 |
Correct |
712 ms |
161108 KB |
Output is correct |
3 |
Correct |
732 ms |
161028 KB |
Output is correct |
4 |
Correct |
697 ms |
161080 KB |
Output is correct |
5 |
Correct |
561 ms |
144576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
49492 KB |
Output is correct |
2 |
Correct |
51 ms |
49448 KB |
Output is correct |
3 |
Correct |
48 ms |
46640 KB |
Output is correct |
4 |
Correct |
48 ms |
46652 KB |
Output is correct |
5 |
Correct |
49 ms |
46652 KB |
Output is correct |
6 |
Correct |
42 ms |
46800 KB |
Output is correct |
7 |
Correct |
43 ms |
46740 KB |
Output is correct |
8 |
Correct |
48 ms |
47048 KB |
Output is correct |
9 |
Correct |
23 ms |
37468 KB |
Output is correct |
10 |
Correct |
46 ms |
47104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
35164 KB |
Output is correct |
2 |
Correct |
4 ms |
35164 KB |
Output is correct |
3 |
Correct |
5 ms |
35164 KB |
Output is correct |
4 |
Correct |
6 ms |
35164 KB |
Output is correct |
5 |
Correct |
5 ms |
35164 KB |
Output is correct |
6 |
Correct |
4 ms |
35164 KB |
Output is correct |
7 |
Correct |
5 ms |
35164 KB |
Output is correct |
8 |
Correct |
4 ms |
35164 KB |
Output is correct |
9 |
Correct |
5 ms |
35256 KB |
Output is correct |
10 |
Correct |
4 ms |
35164 KB |
Output is correct |
11 |
Correct |
6 ms |
35164 KB |
Output is correct |
12 |
Correct |
7 ms |
35420 KB |
Output is correct |
13 |
Correct |
10 ms |
35420 KB |
Output is correct |
14 |
Correct |
7 ms |
35336 KB |
Output is correct |
15 |
Correct |
7 ms |
35420 KB |
Output is correct |
16 |
Correct |
6 ms |
35420 KB |
Output is correct |
17 |
Correct |
6 ms |
35420 KB |
Output is correct |
18 |
Correct |
6 ms |
35420 KB |
Output is correct |
19 |
Correct |
134 ms |
63392 KB |
Output is correct |
20 |
Correct |
127 ms |
63568 KB |
Output is correct |
21 |
Correct |
109 ms |
63568 KB |
Output is correct |
22 |
Correct |
109 ms |
63572 KB |
Output is correct |
23 |
Correct |
109 ms |
63448 KB |
Output is correct |
24 |
Correct |
99 ms |
60036 KB |
Output is correct |
25 |
Correct |
96 ms |
60104 KB |
Output is correct |
26 |
Correct |
101 ms |
60068 KB |
Output is correct |
27 |
Correct |
102 ms |
60108 KB |
Output is correct |
28 |
Correct |
114 ms |
60104 KB |
Output is correct |
29 |
Correct |
113 ms |
60112 KB |
Output is correct |
30 |
Correct |
109 ms |
60108 KB |
Output is correct |
31 |
Correct |
112 ms |
60124 KB |
Output is correct |
32 |
Correct |
103 ms |
60112 KB |
Output is correct |
33 |
Correct |
106 ms |
60104 KB |
Output is correct |
34 |
Correct |
92 ms |
60104 KB |
Output is correct |
35 |
Correct |
93 ms |
60108 KB |
Output is correct |
36 |
Correct |
89 ms |
60068 KB |
Output is correct |
37 |
Correct |
92 ms |
60112 KB |
Output is correct |
38 |
Correct |
90 ms |
60108 KB |
Output is correct |
39 |
Correct |
98 ms |
61136 KB |
Output is correct |
40 |
Correct |
74 ms |
51408 KB |
Output is correct |
41 |
Correct |
95 ms |
60760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
35164 KB |
Output is correct |
2 |
Correct |
4 ms |
35164 KB |
Output is correct |
3 |
Correct |
5 ms |
35164 KB |
Output is correct |
4 |
Correct |
6 ms |
35164 KB |
Output is correct |
5 |
Correct |
5 ms |
35164 KB |
Output is correct |
6 |
Correct |
4 ms |
35164 KB |
Output is correct |
7 |
Correct |
5 ms |
35164 KB |
Output is correct |
8 |
Correct |
4 ms |
35164 KB |
Output is correct |
9 |
Correct |
5 ms |
35256 KB |
Output is correct |
10 |
Correct |
4 ms |
35164 KB |
Output is correct |
11 |
Correct |
6 ms |
35164 KB |
Output is correct |
12 |
Correct |
7 ms |
35420 KB |
Output is correct |
13 |
Correct |
10 ms |
35420 KB |
Output is correct |
14 |
Correct |
7 ms |
35336 KB |
Output is correct |
15 |
Correct |
7 ms |
35420 KB |
Output is correct |
16 |
Correct |
6 ms |
35420 KB |
Output is correct |
17 |
Correct |
6 ms |
35420 KB |
Output is correct |
18 |
Correct |
6 ms |
35420 KB |
Output is correct |
19 |
Correct |
687 ms |
161132 KB |
Output is correct |
20 |
Correct |
712 ms |
161108 KB |
Output is correct |
21 |
Correct |
732 ms |
161028 KB |
Output is correct |
22 |
Correct |
697 ms |
161080 KB |
Output is correct |
23 |
Correct |
561 ms |
144576 KB |
Output is correct |
24 |
Correct |
60 ms |
49492 KB |
Output is correct |
25 |
Correct |
51 ms |
49448 KB |
Output is correct |
26 |
Correct |
48 ms |
46640 KB |
Output is correct |
27 |
Correct |
48 ms |
46652 KB |
Output is correct |
28 |
Correct |
49 ms |
46652 KB |
Output is correct |
29 |
Correct |
42 ms |
46800 KB |
Output is correct |
30 |
Correct |
43 ms |
46740 KB |
Output is correct |
31 |
Correct |
48 ms |
47048 KB |
Output is correct |
32 |
Correct |
23 ms |
37468 KB |
Output is correct |
33 |
Correct |
46 ms |
47104 KB |
Output is correct |
34 |
Correct |
134 ms |
63392 KB |
Output is correct |
35 |
Correct |
127 ms |
63568 KB |
Output is correct |
36 |
Correct |
109 ms |
63568 KB |
Output is correct |
37 |
Correct |
109 ms |
63572 KB |
Output is correct |
38 |
Correct |
109 ms |
63448 KB |
Output is correct |
39 |
Correct |
99 ms |
60036 KB |
Output is correct |
40 |
Correct |
96 ms |
60104 KB |
Output is correct |
41 |
Correct |
101 ms |
60068 KB |
Output is correct |
42 |
Correct |
102 ms |
60108 KB |
Output is correct |
43 |
Correct |
114 ms |
60104 KB |
Output is correct |
44 |
Correct |
113 ms |
60112 KB |
Output is correct |
45 |
Correct |
109 ms |
60108 KB |
Output is correct |
46 |
Correct |
112 ms |
60124 KB |
Output is correct |
47 |
Correct |
103 ms |
60112 KB |
Output is correct |
48 |
Correct |
106 ms |
60104 KB |
Output is correct |
49 |
Correct |
92 ms |
60104 KB |
Output is correct |
50 |
Correct |
93 ms |
60108 KB |
Output is correct |
51 |
Correct |
89 ms |
60068 KB |
Output is correct |
52 |
Correct |
92 ms |
60112 KB |
Output is correct |
53 |
Correct |
90 ms |
60108 KB |
Output is correct |
54 |
Correct |
98 ms |
61136 KB |
Output is correct |
55 |
Correct |
74 ms |
51408 KB |
Output is correct |
56 |
Correct |
95 ms |
60760 KB |
Output is correct |
57 |
Correct |
718 ms |
161104 KB |
Output is correct |
58 |
Correct |
690 ms |
161048 KB |
Output is correct |
59 |
Correct |
652 ms |
161080 KB |
Output is correct |
60 |
Correct |
633 ms |
161012 KB |
Output is correct |
61 |
Correct |
640 ms |
161052 KB |
Output is correct |
62 |
Correct |
652 ms |
161108 KB |
Output is correct |
63 |
Correct |
494 ms |
144828 KB |
Output is correct |
64 |
Correct |
497 ms |
144688 KB |
Output is correct |
65 |
Correct |
559 ms |
144572 KB |
Output is correct |
66 |
Correct |
577 ms |
144592 KB |
Output is correct |
67 |
Correct |
562 ms |
144576 KB |
Output is correct |
68 |
Correct |
563 ms |
144820 KB |
Output is correct |
69 |
Correct |
588 ms |
144828 KB |
Output is correct |
70 |
Correct |
598 ms |
144572 KB |
Output is correct |
71 |
Correct |
561 ms |
144684 KB |
Output is correct |
72 |
Correct |
598 ms |
144596 KB |
Output is correct |
73 |
Correct |
514 ms |
144592 KB |
Output is correct |
74 |
Correct |
528 ms |
143580 KB |
Output is correct |
75 |
Correct |
502 ms |
144816 KB |
Output is correct |
76 |
Correct |
488 ms |
143552 KB |
Output is correct |
77 |
Correct |
508 ms |
143556 KB |
Output is correct |
78 |
Correct |
607 ms |
151724 KB |
Output is correct |
79 |
Correct |
393 ms |
105924 KB |
Output is correct |
80 |
Correct |
569 ms |
147424 KB |
Output is correct |