This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define f first
#define s second
#define pb push_back
#define ld long double
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vec vector
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using oset = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
const ld eps = 1e-6;
const int mod = 1e9 + 7;
const int oo = 2e9;
const ll OO = 2e18;
const int N = 1e6 + 10;
int n, q, w[N], ans[4 * N];
vec<int> s[N];
void merge(int v)
{
vec<int> &a = s[2 * v + 1], &b = s[2 * v + 2];
int n = sz(a), m = sz(b);
int i = 0, j = 0;
while (i < n || j < m)
{
if (i == n) s[v].pb(b[j++]);
else if (j == m) s[v].pb(a[i++]);
else
{
if (a[i] < b[j]) s[v].pb(a[i++]);
else s[v].pb(b[j++]);
}
}
ans[v] = max(ans[2 * v + 1], ans[2 * v + 2]);
auto it = lower_bound(all(s[2 * v + 2]), s[2 * v + 1].back());
if (it != s[2 * v + 2].begin())
{
--it;
ans[v] = max(ans[v], s[2 * v + 1].back() + *it);
}
}
void build(int v, int tl, int tr)
{
if (tl == tr)
{
s[v].pb(w[tl]);
return;
}
int m = (tl + tr) / 2;
build(2 * v + 1, tl, m);
build(2 * v + 2, m + 1, tr);
merge(v);
}
void get(vec<int> &p, int l, int r, int v, int tl, int tr)
{
if (tl > r || tr < l) return;
if (tl >= l && tr <= r)
{
p.pb(v);
return;
}
int m = (tl + tr) / 2;
get(p, l, r, 2 * v + 1, tl, m);
get(p, l, r, 2 * v + 2, m + 1, tr);
}
void solve()
{
cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> w[i];
build(0, 1, n);
while (q--)
{
int l, r, k;
cin >> l >> r >> k;
vec<int> p;
get(p, l, r, 0, 1, n);
int mx = 0;
for (int i = 0; i < sz(p); i++)
{
mx = max(mx, ans[p[i]]);
for (int j = i + 1; j < sz(p); j++)
{
auto it = lower_bound(all(s[p[j]]), s[p[i]].back());
if (it != s[p[j]].begin())
{
--it;
mx = max(mx, s[p[i]].back() + *it);
}
}
}
if (mx <= k) cout << 1;
else cout << 0;
cout << "\n";
}
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
# | 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... |