This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#define N 1000010
#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mod 1000000007
#define pii pair <ll, ll>
using namespace std;
int n, q;
ll v[N];
pii T[N*4];
ll l, r, x;
vector <ll> E[N*4];
void uni(ll CEP, ll SAG, ll node)
{
	pii ans = T[node];
	vector <ll> res;
	
	ll pos = 0, pos2 = 0;
	while(pos < E[CEP].size() && pos2 < E[SAG].size()) {
		if(E[CEP][pos] < E[SAG][pos2]) res.pb(E[CEP][pos++]);
		
		else res.pb(E[SAG][pos2++]);
	}
	
	ll a = pos2-1;
	
	while(pos < E[CEP].size()) res.pb(E[CEP][pos++]);
	while(pos2 < E[SAG].size()) res.pb(E[SAG][pos2++]);
	
	if(T[CEP].ss == -1 || T[SAG].ss == -1) { ans = (T[CEP].ss == -1 ? T[SAG] : T[CEP]); }
	else {
		if(T[CEP].ff + T[CEP].ss >= T[SAG].ff + T[SAG].ss) ans = T[CEP];
		else ans = T[SAG];
	}
	
	if(a != -1) {
		ll x = E[SAG][a];
		
		if(ans.ss == -1 || ans.ff + ans.ss < E[CEP].back() + x) ans = {E[CEP].back(), x};
	}
	
	T[node] = ans;
	E[node].clear();
	
	for(auto i: res) E[node].pb(i);
}
void build(ll l, ll r, ll node)
{
	if(l == r) {
		T[node] = {v[l], -1};
		E[node].pb(v[l]);
		return;
	}
	
	build(l, (l+r)/2, node*2);
	build((l+r)/2+1, r, node*2+1);
	
	uni(node*2, node*2+1, node);
} 
void tap(ll a, ll b, ll l, ll r, ll node)
{
	if(l > b || r < a) return;
	
	if(l >= a && r <= b) {
		uni(0, node, 0);
		return;
	}
	
	tap(a, b, l, (l+r)/2, node*2);
	tap(a, b, (l+r)/2+1, r, node*2+1);
}
int main()
{
	scanf("%d%d", &n, &q);
	
	for(ll i = 1; i <= n; i++)
		scanf("%lld", &v[i]);
	
	build(1, n, 1);
	
	for(ll i = 1; i <= q; i++) {
		scanf("%lld%lld%lld", &l, &r, &x);
		
		E[0].clear();
		T[0] = {-1, -1};
		tap(l, r, 1, n, 1);
		
		if(T[0].ss == -1 || T[0].ff + T[0].ss <= x) puts("1");
		else puts("0");
	}
}
Compilation message (stderr)
sortbooks.cpp: In function 'void uni(long long int, long long int, long long int)':
sortbooks.cpp:32:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  while(pos < E[CEP].size() && pos2 < E[SAG].size()) {
        ~~~~^~~~~~~~~~~~~~~
sortbooks.cpp:32:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  while(pos < E[CEP].size() && pos2 < E[SAG].size()) {
                               ~~~~~^~~~~~~~~~~~~~~
sortbooks.cpp:40:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  while(pos < E[CEP].size()) res.pb(E[CEP][pos++]);
        ~~~~^~~~~~~~~~~~~~~
sortbooks.cpp:41:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  while(pos2 < E[SAG].size()) res.pb(E[SAG][pos2++]);
        ~~~~~^~~~~~~~~~~~~~~
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:89:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &q);
  ~~~~~^~~~~~~~~~~~~~~~
sortbooks.cpp:92:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld", &v[i]);
   ~~~~~^~~~~~~~~~~~~~~
sortbooks.cpp:97:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%lld%lld", &l, &r, &x);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |