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<bits/stdc++.h>
using namespace std;
const int mxN = 1e6 + 5;
int LOG[mxN];
struct SparseTable{
vector<vector<int>>sTable;
SparseTable(int n, vector<int>&v){
sTable.resize(LOG[n] + 1);
for(int i = 0;i <= LOG[n];++i){
sTable[i].resize(n + 1);
}
for(int i = 1;i <= n;++i){
sTable[0][i] = v[i];
}
for(int i = 1;i <= LOG[n];++i){
for(int j = 1;j + (1 << (i - 1)) <= n;++j){
sTable[i][j] = max(sTable[i - 1][j], sTable[i - 1][j + (1 << (i - 1))]);
}
}
}
int Query(int l, int r){
int lg = LOG[r - l + 1];
return max(sTable[lg][l], sTable[lg][r - (1 << lg) + 1]);
}
};
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q, MAX = 0;
cin >> n >> q;
vector<int>v(n + 1);
vector<vector<int>>pos(1001);
LOG[1] = 0;
for(int i = 2;i <= n;++i)LOG[i] = LOG[i >> 1] + 1;
for(int i = 1;i <= n;++i){
cin >> v[i];
if(v[i] <= 1000)pos[v[i]].push_back(i);
MAX = max(MAX, v[i]);
}
vector<int>pref(n + 1);
for(int i = 1;i < n;++i){
pref[i] = (v[i] > v[i + 1]);
}
for(int i = 1;i < n;++i){
pref[i] += pref[i - 1];
}
SparseTable table(n, v);
while(q--){
int l, r, k;
cin >> l >> r >> k;
if(n <= 5000 && q <= 5000){
int mx = v[l], res = 0;
for(int i = l + 1;i <= r;++i){
if(mx > v[i]){
res = max(res, mx + v[i]);
}
mx = max(mx, v[i]);
}
cout << (res <= k) << '\n';
}
else if(n <= 1e5 && MAX <= 1000){
int mx = 0;
for(int i = 0;i <= 1000;++i){
if(pos[i].size() != 0){
auto itr = upper_bound(pos[i].begin(), pos[i].end(), r);
if(itr == pos[i].begin())continue;
itr--;
if(*itr >= l){
int R = *itr, MX = table.Query(l, R);
mx = max(mx, (MX > i) * (MX + i));
}
}
}
cout << (mx <= k) << '\n';
}
else{
int cnt = pref[r - 1] - pref[l - 1];
if(cnt){
cout << 0 << '\n';
}
else{
cout << 1 << '\n';
}
}
}
}
# | 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... |