#include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef long double ld;
#define pb push_back
#define pf push_front
#define fi first
#define se second
const ll mod = 1e9+7, mxn = (1LL<<21)+7;
struct node
{
vector<ll> v;
ll ans = -mod;
};
node merge(node const a, node const b)
{
node c;
c.ans = max({a.ans, b.ans});
ll p = lower_bound(b.v.begin(), b.v.end(), a.v.back())-b.v.begin()-1;
if (p >= 0) c.ans = max({c.ans,b.v[p]+a.v.back()});
ll id_a = 0, id_b = 0;
while (id_a < a.v.size() && id_b < b.v.size())
{
if (a.v[id_a] < b.v[id_b])
{
if (c.v.empty() || c.v.back() != a.v[id_a]) c.v.pb(a.v[id_a++]);
else id_a++;
}
else
{
if (c.v.empty() || b.v[id_b] != c.v.back()) c.v.pb(b.v[id_b++]);
else id_b++;
}
}
while (id_a < a.v.size())
{
if (c.v.empty() || c.v.back() != a.v[id_a]) c.v.pb(a.v[id_a++]);
else id_a++;
}
while (id_b < b.v.size())
{
if (c.v.empty() || b.v[id_b] != c.v.back()) c.v.pb(b.v[id_b++]);
else id_b++;
}
return c;
}
vector<node> st(mxn<<2);
ll a[mxn], n, q, ans, f;
void build(ll id, ll l, ll r)
{
if (l == r) {st[id].v = {a[l]}; return;}
ll m = (r+l)>>1;
build(id<<1,l,m); build(id<<1|1,m+1,r);
st[id] = merge(st[id<<1], st[id<<1|1]);
}
void get(ll id, ll l, ll r, ll u, ll v)
{
if (u <= l && r <= v)
{
ans = max({ans, st[id].ans});
ll p = lower_bound(st[id].v.begin(), st[id].v.end(), f)-st[id].v.begin()-1;
if (p >= 0) ans = max({ans, st[id].v[p]+f});
f = max(f, st[id].v.back());
return ;
}
ll m = (r+l)>>1;
if (v <= m) get(id<<1,l,m,u,v);
else if (m+1 <= u) get(id<<1|1,m+1,r,u,v);
else {get(id<<1,l,m,u,v); get(id<<1|1,m+1,r,u,v);}
}
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("test.inp","r",stdin); freopen("test.out","w",stdout); freopen("test.err","w",stderr);
cin >> n >> q;
for (ll i = 1; i <= n; i++) cin >> a[i];
ll pw = (1LL<<(__lg(n-1)+1));
build(1,1,pw);
while (q--)
{
ll l, r, k; cin >> l >> r >> k; ans = 0; f = -mod; get(1,1,pw,l,r);
cout << (ans <= k) << '\n';
}
}
Compilation message
sortbooks.cpp: In function 'node merge(node, node)':
sortbooks.cpp:22:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | while (id_a < a.v.size() && id_b < b.v.size())
| ~~~~~^~~~~~~~~~~~
sortbooks.cpp:22:38: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | while (id_a < a.v.size() && id_b < b.v.size())
| ~~~~~^~~~~~~~~~~~
sortbooks.cpp:35:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | while (id_a < a.v.size())
| ~~~~~^~~~~~~~~~~~
sortbooks.cpp:40:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | while (id_b < b.v.size())
| ~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
43 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
43 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
39 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
46 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
43 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
43 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |