This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// "We create our own demons"
#include <bits/stdc++.h>
using namespace std;
#ifdef ONPC
#include "debug.h"
#else
#define dbg(...)
#endif
#define int long long
#define ll long long
#define ld long double
#define pi pair<int, int>
// vector
#define sz(a) (int)((a).size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(x) (*max_element(all(x)))
#define minel(x) (*min_element(all(x)))
#define maxpos(x) (max_element(all(x)) - x.begin())
#define minpos(x) (min_element(all(x)) - x.begin())
#define rev(x) (reverse(all(x)))
// bits
#define popcount(n) __builtin_popcountll(n)
#define clz(n) __builtin_clzll(n)
// math
#define sqr(n) ((n) * (n))
#define divup(a, b) (((a) + (b)-1) / (b))
// ostream
#define Fixed(a) cout << fixed << setprecision(12) << a;
template <class T> bool chmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template <class T> bool chmax(T& a, const T& b) { return b > a ? a = b, 1 : 0; }
template <class T> using min_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> using max_queue = priority_queue<T, vector<T>, less<T>>;
template <class T> using V = vector<T>;
using vi = V<int>;
using vd = V<ld>;
using vb = V<bool>;
using vpi = V<pi>;
using vvi = V<vi>;
using vvb = V<vb>;
const int mod = 1e9 + 7; // 998244353 1e9 + 7
const ll inf = (int)(1e18) + 7;
const int inf_s = 1e9 + 7;
const ld eps = 1e-9;
const int B = 32;
const int N = 1000 + 3;
const int logn = 21;
const int maxn = 1e6 + 7;
/////////////////////////solve/////////////////////////
struct node {
int mx = 0;
};
node tree[4 * maxn];
node unite(node a, node b) {
node res;
res.mx = max(a.mx, b.mx);
return res;
}
void update(int i, int x, int v, int tl, int tr) {
if (tr - tl == 1) {
tree[v].mx = x;
return;
}
int tm = (tl + tr) >> 1;
if (i < tm) update(i, x, 2 * v, tl, tm);
else update(i, x, 2 * v + 1, tm, tr);
tree[v] = unite(tree[2 * v], tree[2 * v + 1]);
}
node get(int l, int r, int v, int tl, int tr) {
if (tl >= r || tr <= l) return {0};
if (tl >= l && tr <= r) return tree[v];
int tm = (tl + tr) >> 1;
return unite(get(l, r, 2 * v, tl, tm),
get(l, r, 2 * v + 1, tm, tr));
}
int logs[maxn];
int sp[logn][maxn];
void build(vi a) {
int n = sz(a);
for (int i = 2; i <= n; ++i) {
logs[i] = logs[i >> 1] + 1;
}
for (int i = 0; i < n; ++i) {
sp[0][i] = a[i];
}
for (int k = 0; k < logs[n]; ++k) {
for (int i = 0; i + (1LL << k) < n; ++i) {
sp[k + 1][i] = max(sp[k][i], sp[k][i + (1LL << k)]);
}
}
}
int get2(int l, int r) {
if (l >= r) return 0;
int pw = logs[r - l];
return max(sp[pw][l], sp[pw][r - (1LL << pw)]);
}
struct query {
int r, k, qi;
};
V<query> L[maxn];
int ans[maxn];
void solve() {
int n, m;
cin >> n >> m;
vi a(n + 1);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
a[n] = inf;
for (int qi = 0; qi < m; ++qi) {
int l, r, k;
cin >> l >> r >> k;
--l; --r;
L[l].push_back({r, k, qi});
}
build(a);
vi st;
st.push_back(n);
for (int i = n - 1; i >= 0; --i) {
while (!st.empty() && a[st.back()] < a[i]) {
int j = st.back();
st.pop_back();
update(j, 0, 1, 0, n);
}
int mx = get2(i + 1, st.back());
dbg(i, a[i], mx, st.back());
update(i, a[i] + mx, 1, 0, n);
st.push_back(i);
for (auto& [r, k, qi] : L[i]) {
// dbg(st);
auto it = upper_bound(rall(st), r);
dbg(*it, r);
int res = 0;
--it;
dbg(i, *it);
chmax(res, get(i, *it, 1, 0, n).mx);
dbg(res);
if (*it != n) {
chmax(res, a[*it] + get2(*it + 1, r + 1));
}
dbg(res);
ans[qi] = (res <= k);
}
}
for (int qi = 0; qi < m; ++qi) {
cout << ans[qi] << "\n";
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef ONPC
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
int t = 1;
//cin >> t;
for (int i = 1; i <= t; ++i) {
#ifdef ONPC
cerr << "===========" << i << "===========" << '\n';
#endif
solve();
}
#ifdef ONPC
cerr << endl << "Time " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
}
# | 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... |