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>
#define endl "\n"
using namespace std;
const int maxn = 1e6+5;
int n,m;
int w[maxn],bigidx[maxn];
int what[maxn],jump[maxn][20];
void pre()
{
int last = 0;
for(int j = 0; j <= 20; j++)
{
int gr = min(n,(1<<(j+1)));
for(int i = last + 1; i <= gr; i++) what[i] = j;
last = gr;
}
for(int i = 1; i <= n; i++) jump[i][0] = w[i];
for(int j = 1; j <= 20; j++)
{
for(int i = 1; i <= n; i++)
{
jump[i][j] = max(jump[i][j-1],jump[jump[i][j-1]+1][j-1]);
}
}
}
int query(int l, int r)
{
if(l>r) return -1;
int step = what[r-l+1];
return max(jump[l][step],jump[r-(1<<step)+1][step]);
}
bool check(int l, int r, int k)
{
int maxch,razmqna;
maxch = razmqna = 0;
for(int i = l; i < r; i = bigidx[i])
{
maxch = w[i];
int pom = query(i+1,min(r,bigidx[i]-1));
if(pom!=-1) maxch += pom;
razmqna = max(razmqna,maxch);
}
return k >= razmqna;
}
void read()
{
cin >> n >> m;
pre();
for(int i = 1; i <= n; i++) cin >> w[i];
w[n+1] = 1e9+1;
stack<int>s1;
s1.push(n+1);
for(int i = n; i >= 1; i--)
{
while(!s1.empty()&&w[s1.top()]<w[i]) s1.pop();
bigidx[i] = s1.top();
s1.push(i);
}
int l,r,k;
for(int i = 1; i <= m; i++)
{
cin >> l >> r >> k;
if(check(l,r,k)) cout << 1 << endl;
else cout << 0 << endl;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
read();
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... |