#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
const int def = 1e6+1;
const int maxk = 18;
const ll inf = 1e18;
struct Node{
vector<int> v;
int val;
Node(){}
Node(int x){
v.push_back(x);
val = 0;
}
};
Node st[def];
void build(int l, int r, int crr, vector<int> &a){
if (l == r){
st[crr] = Node(a[l]);
return;
}
int mid = (l + r) / 2;
build(l, mid, crr * 2 + 1, a);
build(mid + 1, r, crr * 2 + 2, a);
st[crr].v = st[crr * 2 + 1].v;
st[crr].val = max(st[crr * 2 + 1].val, st[crr * 2 + 2].val);
for (int x : st[crr * 2 + 2].v)
st[crr].v.push_back(x);
sort(st[crr].v.begin(), st[crr].v.end());
if (st[crr * 2 + 1].v.back() > st[crr * 2 + 2].v.front()){
int pos = lower_bound(st[crr * 2 + 2].v.begin(), st[crr * 2 + 2].v.end(), st[crr * 2 + 1].v.back()) - st[crr * 2 + 2].v.begin();
int val = st[crr * 2 + 1].v.back() + st[crr * 2 + 2].v[pos - 1];
st[crr].val = max(st[crr].val, val);
}
}
vector<int> nodes;
void get(int l, int r, int ql, int qr, int crr){
if (l > qr || r < ql)
return;
if (l >= ql && r <= qr){
nodes.push_back(crr);
return;
}
int mid = (l + r) / 2;
get(l, mid, ql, qr, crr * 2 + 1);
get(mid + 1, r , ql, qr, crr * 2 + 2);
}
void solve(){
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
build(0, n - 1, 0, a);
while (q--){
int l, r, k;
cin >> l >> r >> k;
l--; r--;
nodes.clear();
get(0, n - 1, l, r, 0);
int val = 0, crr = 0;
for (int i = 0; i < nodes.size(); i++){
val = max(val, st[nodes[i]].val);
crr = max(crr, st[nodes[i]].v.back());
if (i + 1 < nodes.size()){
if (st[nodes[i + 1]].v.front() >= crr)
continue;
int p = nodes[i + 1];
int pos = lower_bound(st[p].v.begin(), st[p].v.end(), crr) - st[p].v.begin();
val = max(val, crr + st[p].v[pos - 1]);
}
}
if (val <= k)
cout << 1;
else
cout << 0;
cout << endl;
}
}
/*
*/
main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (ifstream("input.txt").good()){
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
int t = 1;
while (t--){
solve();
cout << "\n";
}
}
Compilation message (stderr)
sortbooks.cpp:105:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
105 | main(){
| ^~~~
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:110:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:111:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |