#include <bits/stdc++.h>
#ifdef ONPC
#include "t_debug.cpp"
#else
#define debug(...) 42
#endif
using namespace std;
//namespace pbds = __gnu_pbds;
using ll = long long;
const int inf = 1e9;
const ll infl = 1e18;
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rng(RANDOM);
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p) { return is >> p.first >> p.second; }
template<typename Cont> int sz(const Cont& cont) { return int(cont.size()); }
const string fileio = "";
constexpr int tests = 0, nmax = 2e5, nlog = __lg(nmax), mod = 1e9+7;
template<typename F>
struct SegTree {
int n;
vector<int> tree;
const F& merge;
SegTree(int n, int def, const F& merge) : n(n), tree(4*n, def), merge(merge) {
}
int query(int l, int r) {
l += n;
r += n;
int res = tree[0]; // def
while (l <= r) {
if (l % 2 == 1) res = merge(res, tree[l++]);
if (r % 2 == 0) res = merge(tree[r--], res);
l /= 2;
r /= 2;
}
return res;
}
void update(int i, int x) {
tree[i+= n] = x;
for (i /= 2; i >= 1; i /= 2)
tree[i] = merge(tree[i*2], tree[i*2+1]);
}
};
int solve() {
int n, m;
cin >> n >> m;
if (!cin) return 1;
vector<int> w(n);
for (auto& i : w) cin >> i;
vector<int> ix(n);
iota(ix.begin(), ix.end(), 0);
sort(ix.begin(), ix.end(), [&](int a, int b) {
return w[a] > w[b];
});
vector<tuple<int,int,int,int>> q(m);
vector<int> ans(n);
for (int i = 0; i < m; i++) {
int l, r, k;
cin >> l >> r >> k;
q[i] = {k, l, r, i};
}
sort(q.begin(), q.end(), greater{});
vector<int> left(n, -1);
vector<int> right(n, n);
{
vector<pair<int,int>> st{{-1, -1}};
for (int i = 0; i < n; i++) {
if (w[i] < st.back().first) {
left[i] = st.back().second;
}
while (w[i] <= st.back().first)
st.pop_back();
st.emplace_back(w[i], i);
}
}
{
vector<pair<int,int>> st{{inf+1, n}};
for (int i = n-1; i >= 0;i --) {
if (w[i] > st.back().first) {
right[i] = st.back().second;
}
while (w[i] >= st.back().first)
st.pop_back();
st.emplace_back(w[i], i);
}
}
SegTree lbound(n, -1, [](int a, int b) { return max(a, b); });
SegTree rbound(n, n, [](int a, int b) { return min(a, b); });
int j = 0;
for (auto [k, l, r, qi] : q) {
while (j < n && k < w[ix[j]]) {
lbound.update(ix[j], left[ix[j]]);
rbound.update(ix[j], right[ix[j]]);
j++;
}
int lb = lbound.query(l, r);
int rb = rbound.query(l, r);
assert(lb <= rb);
cout << (lb <= l && r <= rb) << '\n';
}
return 0;
}
signed main() {
int t = 1;
#ifdef ONPC
t = 10000;
#else
if (fileio.size()) {freopen((fileio+".in").c_str(),"r",stdin);freopen((fileio+".out").c_str(),"w",stdout);}
#endif
cin.tie(0)->sync_with_stdio(0);
if (tests) cin >> t;
while (t-- && cin) {
if (solve()) break;
#ifdef ONPC
cout << "____________________" << endl;
#endif
}
return 0;
}
/*
█████ █████ ███ ████
▒▒███ ▒▒███ ▒▒▒ ▒▒███
███████ ████████ ███████ ████ ▒███ █████ ████ ██████ ████████
███▒▒███ ▒▒███▒▒███ ███▒▒███ ▒▒███ ▒███ ▒▒███ ▒███ ███▒▒███▒▒███▒▒███
▒███ ▒███ ▒███ ▒▒▒ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒▒▒
▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███
▒▒████████ █████ ▒▒████████ █████ █████ ▒▒███████ ▒▒██████ █████
▒▒▒▒▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒███ ▒▒▒▒▒▒ ▒▒▒▒▒
███ ▒███
▒▒██████
▒▒▒▒▒▒
*/
Compilation message
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:120:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
120 | if (fileio.size()) {freopen((fileio+".in").c_str(),"r",stdin);freopen((fileio+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:120:74: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
120 | if (fileio.size()) {freopen((fileio+".in").c_str(),"r",stdin);freopen((fileio+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1039 ms |
136152 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
80 ms |
16076 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |