#include<bits/stdc++.h>
#define el '\n'
using namespace std ;
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
const int MN = 1e6 + 5;
const int N = 2e5 + 5;
const int inf = 2e9;
int n , m , a[MN];
namespace sub_trau{
bool valid_input(){
return n <= 500 && m <= 500;
}
void solve(){
while(m--){
int l , r , k;
cin >> l >> r >> k;
int ans = 0;
for ( int i = l ; i <= r ; i++ ){
for ( int j = i ; j <= r ; j++ ){
if (a[j] < a[i]) ans = max(ans , a[j] + a[i]);
}
}
cout << (ans <= k ? 1 : 0) << el;
}
}
}
namespace sub2{
bool valid_input(){
return n <= 5000 && m <= 5000;
}
void solve(){
while(m--){
int l , r , k;
cin >> l >> r >> k;
int ans = 0;
vector<int> pref(r + 3);
pref[l - 1] = INT_MIN;
pref[l] = a[l];
for ( int i = l + 1 ; i <= r ; i++ ) pref[i] = max(pref[i - 1] , a[i]);
for ( int i = l ; i <= r ; i++ ){
if (pref[i - 1] > a[i]) ans = max(ans , a[i] + pref[i - 1]);
}
cout << (ans <= k ? 1 : 0) << el;
}
}
}
namespace memory_dak{
bool valid_input(){
return n <= (int)2e5 && m <= (int)2e5;
}
struct merge_sort_tree{
#define lt(x) (x << 1)
#define rt(x) (x << 1 | 1)
vector<int> st[4 * MN];
void build(int id , int l , int r){
if (l == r){
st[id].push_back(a[l]);
return;
}
int mid = l + r >> 1;
build(id << 1 , l , mid);
build(id << 1 | 1 , mid + 1 , r);
merge(st[lt(id)].begin() , st[lt(id)].end() , st[rt(id)].begin() , st[rt(id)].end() , back_inserter(st[id]));
}
int get(int id , int l , int r , int tl , int tr , int x){
if (l > tr || r < tl) return -inf;
else if (l >= tl && r <= tr){
auto it = lower_bound(st[id].begin() , st[id].end() , x) - st[id].begin() - 1;
if (it >= 0) return st[id][it];
else return -inf;
}
int mid = l + r >> 1;
return max(get(id << 1 , l , mid , tl , tr , x) , get(id << 1 | 1 , mid + 1 , r , tl , tr , x));
}
} mt;
struct Segtri{
struct Node{
int l , r , best , max_val;
};
Node merge(const Node &x , const Node &y){
Node c;
c.l = x.l;
c.r = y.r;
c.max_val = max(x.max_val , y.max_val);
c.best = max(x.best , y.best);
int cur = mt.get(1 , 1 , n , y.l , y.r , x.max_val);
c.best = max(c.best , x.max_val + cur);
return c;
}
Node st[4 * N];
void build(int id , int l , int r){
if (l == r){
st[id] = {l , l , 0 , a[l]};
return;
}
int mid = l + r >> 1;
build(id << 1 , l , mid);
build(id << 1 | 1 , mid + 1 , r);
st[id] = merge(st[id << 1] , st[id << 1 | 1]);
}
Node get(int id , int l , int r , int tl , int tr){
if (l >= tl && r <= tr) return st[id];
else {
int mid = l + r >> 1;
if (tl > mid) return get(id << 1 | 1 , mid + 1 , r , tl , tr);
else if (tr <= mid) return get(id << 1 , l , mid , tl , tr);
else return merge(get(id << 1 , l , mid , tl , tr) , get(id << 1 | 1 , mid + 1 , r , tl , tr));
}
}
};
void solve(){
mt.build(1 , 1 , n);
Segtri it;
it.build(1 , 1 , n);
while(m--){
int l , r , k;
cin >> l >> r >> k;
cout << (it.get(1 , 1 , n , l , r).best <= k ? 1 : 0) << el;
}
}
}
namespace sub_final{
vector<tuple<int , int , int>> queries[MN];
struct fenwick{
int bit[MN];
void update(int p , int v){
for ( ; p ; p -= p & -p ){
bit[p] = max(bit[p] , v);
}
}
int get(int x){
int res = 0;
for ( ; x <= n ; x += x & -x ){
res = max(res , bit[x]);
}
return res;
}
} fw;
void solve(){
stack<int> st;
for ( int i = 1 ; i <= m ; i++ ){
int l , r , k;
cin >> l >> r >> k;
queries[r].push_back(make_tuple(l , k , i));
}
a[0] = inf;
st.push(0);
vector<int> ans(m + 5);
for ( int i = 1 ; i <= n ; i++ ){
while (!st.empty() && a[st.top()] <= a[i]) st.pop();
if (st.top()) fw.update(st.top() , a[st.top()] + a[i]);
st.push(i);
for ( auto [l , k , id] : queries[i] ){
ans[id] = fw.get(l) <= k;
}
}
for ( int i = 1 ; i <= m ; i++ ) cout << ans[i] << el;
}
}
int32_t main (){
// freopen("sortbook.inp" , "r" , stdin);
// freopen("sortbook.out" , "w" , stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for ( int i = 1 ; i <= n ; i++ ) cin >> a[i];
if (sub_trau::valid_input()){
sub_trau::solve();
return 0;
}
if (sub2::valid_input()){
sub2::solve();
return 0;
}
if (memory_dak::valid_input()){
memory_dak::solve();
return 0;
}
sub_final::solve();
}
Compilation message
sortbooks.cpp: In member function 'void memory_dak::merge_sort_tree::build(int, int, int)':
sortbooks.cpp:71:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
71 | int mid = l + r >> 1;
| ~~^~~
sortbooks.cpp: In member function 'int memory_dak::merge_sort_tree::get(int, int, int, int, int, int)':
sortbooks.cpp:84:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
84 | int mid = l + r >> 1;
| ~~^~~
sortbooks.cpp: In member function 'void memory_dak::Segtri::build(int, int, int)':
sortbooks.cpp:112:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
112 | int mid = l + r >> 1;
| ~~^~~
sortbooks.cpp: In member function 'memory_dak::Segtri::Node memory_dak::Segtri::get(int, int, int, int, int)':
sortbooks.cpp:121:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
121 | int mid = l + r >> 1;
| ~~^~~
sortbooks.cpp: In function 'void sub_final::solve()':
sortbooks.cpp:176:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
176 | for ( auto [l , k , id] : queries[i] ){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
121724 KB |
Output is correct |
2 |
Correct |
30 ms |
121436 KB |
Output is correct |
3 |
Correct |
32 ms |
121436 KB |
Output is correct |
4 |
Correct |
30 ms |
121340 KB |
Output is correct |
5 |
Correct |
30 ms |
121436 KB |
Output is correct |
6 |
Correct |
51 ms |
121528 KB |
Output is correct |
7 |
Correct |
51 ms |
121688 KB |
Output is correct |
8 |
Correct |
56 ms |
121432 KB |
Output is correct |
9 |
Correct |
36 ms |
121432 KB |
Output is correct |
10 |
Correct |
52 ms |
121436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
121724 KB |
Output is correct |
2 |
Correct |
30 ms |
121436 KB |
Output is correct |
3 |
Correct |
32 ms |
121436 KB |
Output is correct |
4 |
Correct |
30 ms |
121340 KB |
Output is correct |
5 |
Correct |
30 ms |
121436 KB |
Output is correct |
6 |
Correct |
51 ms |
121528 KB |
Output is correct |
7 |
Correct |
51 ms |
121688 KB |
Output is correct |
8 |
Correct |
56 ms |
121432 KB |
Output is correct |
9 |
Correct |
36 ms |
121432 KB |
Output is correct |
10 |
Correct |
52 ms |
121436 KB |
Output is correct |
11 |
Correct |
36 ms |
121436 KB |
Output is correct |
12 |
Correct |
48 ms |
121648 KB |
Output is correct |
13 |
Correct |
50 ms |
121544 KB |
Output is correct |
14 |
Correct |
65 ms |
121700 KB |
Output is correct |
15 |
Correct |
65 ms |
121688 KB |
Output is correct |
16 |
Correct |
93 ms |
121640 KB |
Output is correct |
17 |
Correct |
80 ms |
121432 KB |
Output is correct |
18 |
Correct |
96 ms |
121436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
683 ms |
185160 KB |
Output is correct |
2 |
Correct |
718 ms |
186192 KB |
Output is correct |
3 |
Correct |
711 ms |
185868 KB |
Output is correct |
4 |
Correct |
718 ms |
186072 KB |
Output is correct |
5 |
Correct |
660 ms |
183888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
373 ms |
149344 KB |
Output is correct |
2 |
Correct |
395 ms |
149676 KB |
Output is correct |
3 |
Correct |
376 ms |
149196 KB |
Output is correct |
4 |
Correct |
405 ms |
149564 KB |
Output is correct |
5 |
Correct |
342 ms |
149192 KB |
Output is correct |
6 |
Correct |
362 ms |
149296 KB |
Output is correct |
7 |
Correct |
352 ms |
149280 KB |
Output is correct |
8 |
Correct |
275 ms |
148976 KB |
Output is correct |
9 |
Correct |
82 ms |
135608 KB |
Output is correct |
10 |
Correct |
275 ms |
148952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
121724 KB |
Output is correct |
2 |
Correct |
30 ms |
121436 KB |
Output is correct |
3 |
Correct |
32 ms |
121436 KB |
Output is correct |
4 |
Correct |
30 ms |
121340 KB |
Output is correct |
5 |
Correct |
30 ms |
121436 KB |
Output is correct |
6 |
Correct |
51 ms |
121528 KB |
Output is correct |
7 |
Correct |
51 ms |
121688 KB |
Output is correct |
8 |
Correct |
56 ms |
121432 KB |
Output is correct |
9 |
Correct |
36 ms |
121432 KB |
Output is correct |
10 |
Correct |
52 ms |
121436 KB |
Output is correct |
11 |
Correct |
36 ms |
121436 KB |
Output is correct |
12 |
Correct |
48 ms |
121648 KB |
Output is correct |
13 |
Correct |
50 ms |
121544 KB |
Output is correct |
14 |
Correct |
65 ms |
121700 KB |
Output is correct |
15 |
Correct |
65 ms |
121688 KB |
Output is correct |
16 |
Correct |
93 ms |
121640 KB |
Output is correct |
17 |
Correct |
80 ms |
121432 KB |
Output is correct |
18 |
Correct |
96 ms |
121436 KB |
Output is correct |
19 |
Correct |
914 ms |
168108 KB |
Output is correct |
20 |
Correct |
974 ms |
168064 KB |
Output is correct |
21 |
Correct |
880 ms |
167980 KB |
Output is correct |
22 |
Correct |
906 ms |
167992 KB |
Output is correct |
23 |
Correct |
867 ms |
167880 KB |
Output is correct |
24 |
Correct |
753 ms |
167884 KB |
Output is correct |
25 |
Correct |
765 ms |
167836 KB |
Output is correct |
26 |
Correct |
851 ms |
168248 KB |
Output is correct |
27 |
Correct |
838 ms |
168504 KB |
Output is correct |
28 |
Correct |
890 ms |
168112 KB |
Output is correct |
29 |
Correct |
838 ms |
168156 KB |
Output is correct |
30 |
Correct |
840 ms |
168000 KB |
Output is correct |
31 |
Correct |
849 ms |
168100 KB |
Output is correct |
32 |
Correct |
867 ms |
168132 KB |
Output is correct |
33 |
Correct |
866 ms |
168160 KB |
Output is correct |
34 |
Correct |
753 ms |
167792 KB |
Output is correct |
35 |
Correct |
754 ms |
167712 KB |
Output is correct |
36 |
Correct |
758 ms |
167616 KB |
Output is correct |
37 |
Correct |
755 ms |
167488 KB |
Output is correct |
38 |
Correct |
776 ms |
167744 KB |
Output is correct |
39 |
Correct |
699 ms |
166596 KB |
Output is correct |
40 |
Correct |
425 ms |
154860 KB |
Output is correct |
41 |
Correct |
645 ms |
166340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
121724 KB |
Output is correct |
2 |
Correct |
30 ms |
121436 KB |
Output is correct |
3 |
Correct |
32 ms |
121436 KB |
Output is correct |
4 |
Correct |
30 ms |
121340 KB |
Output is correct |
5 |
Correct |
30 ms |
121436 KB |
Output is correct |
6 |
Correct |
51 ms |
121528 KB |
Output is correct |
7 |
Correct |
51 ms |
121688 KB |
Output is correct |
8 |
Correct |
56 ms |
121432 KB |
Output is correct |
9 |
Correct |
36 ms |
121432 KB |
Output is correct |
10 |
Correct |
52 ms |
121436 KB |
Output is correct |
11 |
Correct |
36 ms |
121436 KB |
Output is correct |
12 |
Correct |
48 ms |
121648 KB |
Output is correct |
13 |
Correct |
50 ms |
121544 KB |
Output is correct |
14 |
Correct |
65 ms |
121700 KB |
Output is correct |
15 |
Correct |
65 ms |
121688 KB |
Output is correct |
16 |
Correct |
93 ms |
121640 KB |
Output is correct |
17 |
Correct |
80 ms |
121432 KB |
Output is correct |
18 |
Correct |
96 ms |
121436 KB |
Output is correct |
19 |
Correct |
683 ms |
185160 KB |
Output is correct |
20 |
Correct |
718 ms |
186192 KB |
Output is correct |
21 |
Correct |
711 ms |
185868 KB |
Output is correct |
22 |
Correct |
718 ms |
186072 KB |
Output is correct |
23 |
Correct |
660 ms |
183888 KB |
Output is correct |
24 |
Correct |
373 ms |
149344 KB |
Output is correct |
25 |
Correct |
395 ms |
149676 KB |
Output is correct |
26 |
Correct |
376 ms |
149196 KB |
Output is correct |
27 |
Correct |
405 ms |
149564 KB |
Output is correct |
28 |
Correct |
342 ms |
149192 KB |
Output is correct |
29 |
Correct |
362 ms |
149296 KB |
Output is correct |
30 |
Correct |
352 ms |
149280 KB |
Output is correct |
31 |
Correct |
275 ms |
148976 KB |
Output is correct |
32 |
Correct |
82 ms |
135608 KB |
Output is correct |
33 |
Correct |
275 ms |
148952 KB |
Output is correct |
34 |
Correct |
914 ms |
168108 KB |
Output is correct |
35 |
Correct |
974 ms |
168064 KB |
Output is correct |
36 |
Correct |
880 ms |
167980 KB |
Output is correct |
37 |
Correct |
906 ms |
167992 KB |
Output is correct |
38 |
Correct |
867 ms |
167880 KB |
Output is correct |
39 |
Correct |
753 ms |
167884 KB |
Output is correct |
40 |
Correct |
765 ms |
167836 KB |
Output is correct |
41 |
Correct |
851 ms |
168248 KB |
Output is correct |
42 |
Correct |
838 ms |
168504 KB |
Output is correct |
43 |
Correct |
890 ms |
168112 KB |
Output is correct |
44 |
Correct |
838 ms |
168156 KB |
Output is correct |
45 |
Correct |
840 ms |
168000 KB |
Output is correct |
46 |
Correct |
849 ms |
168100 KB |
Output is correct |
47 |
Correct |
867 ms |
168132 KB |
Output is correct |
48 |
Correct |
866 ms |
168160 KB |
Output is correct |
49 |
Correct |
753 ms |
167792 KB |
Output is correct |
50 |
Correct |
754 ms |
167712 KB |
Output is correct |
51 |
Correct |
758 ms |
167616 KB |
Output is correct |
52 |
Correct |
755 ms |
167488 KB |
Output is correct |
53 |
Correct |
776 ms |
167744 KB |
Output is correct |
54 |
Correct |
699 ms |
166596 KB |
Output is correct |
55 |
Correct |
425 ms |
154860 KB |
Output is correct |
56 |
Correct |
645 ms |
166340 KB |
Output is correct |
57 |
Correct |
667 ms |
186196 KB |
Output is correct |
58 |
Correct |
642 ms |
186196 KB |
Output is correct |
59 |
Correct |
626 ms |
184992 KB |
Output is correct |
60 |
Correct |
638 ms |
185108 KB |
Output is correct |
61 |
Correct |
645 ms |
184776 KB |
Output is correct |
62 |
Correct |
638 ms |
184368 KB |
Output is correct |
63 |
Correct |
432 ms |
179440 KB |
Output is correct |
64 |
Correct |
411 ms |
179284 KB |
Output is correct |
65 |
Correct |
547 ms |
182352 KB |
Output is correct |
66 |
Correct |
553 ms |
182256 KB |
Output is correct |
67 |
Correct |
597 ms |
182364 KB |
Output is correct |
68 |
Correct |
630 ms |
184132 KB |
Output is correct |
69 |
Correct |
644 ms |
184004 KB |
Output is correct |
70 |
Correct |
695 ms |
183460 KB |
Output is correct |
71 |
Correct |
704 ms |
183840 KB |
Output is correct |
72 |
Correct |
659 ms |
183680 KB |
Output is correct |
73 |
Correct |
389 ms |
175184 KB |
Output is correct |
74 |
Correct |
386 ms |
176504 KB |
Output is correct |
75 |
Correct |
386 ms |
175488 KB |
Output is correct |
76 |
Correct |
378 ms |
175324 KB |
Output is correct |
77 |
Correct |
375 ms |
175136 KB |
Output is correct |
78 |
Correct |
555 ms |
177068 KB |
Output is correct |
79 |
Correct |
466 ms |
167148 KB |
Output is correct |
80 |
Correct |
522 ms |
173304 KB |
Output is correct |