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;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e6;
struct Query { int l, r, k; };
int N, M, A[MAXN+10];
Query B[MAXN+10];
vector<int> tree[MAXN*4+10];
int tree2[MAXN*4+10];
void init(int node, int tl, int tr)
{
if(tl==tr)
{
tree[node].push_back(A[tl]);
tree2[node]=0;
return;
}
int mid=tl+tr>>1;
init(node*2, tl, mid);
init(node*2+1, mid+1, tr);
tree[node].resize(tree[node*2].size()+tree[node*2+1].size());
merge(tree[node*2].begin(), tree[node*2].end(), tree[node*2+1].begin(), tree[node*2+1].end(), tree[node].begin());
auto it=lower_bound(tree[node*2+1].begin(), tree[node*2+1].end(), tree[node*2].back());
if(it!=tree[node*2+1].begin()) tree2[node]=*(--it)+tree[node*2].back();
tree2[node]=max({tree2[node], tree2[node*2], tree2[node*2+1]});
}
void query(int node, int tl, int tr, int l, int r, vector<int> &V)
{
if(r<tl || tr<l) return;
if(l<=tl && tr<=r)
{
V.push_back(node);
return;
}
int mid=tl+tr>>1;
query(node*2, tl, mid, l, r, V);
query(node*2+1, mid+1, tr, l, r, V);
}
int main()
{
int i, j;
scanf("%d%d", &N, &M);
for(i=1; i<=N; i++) scanf("%d", &A[i]);
for(i=1; i<=M; i++) scanf("%d%d%d", &B[i].l, &B[i].r, &B[i].k);
init(1, 1, N);
for(i=1; i<=M; i++)
{
vector<int> V;
query(1, 1, N, B[i].l, B[i].r, V);
int ans=0, val=0;
for(auto it : V)
{
ans=max(ans, tree2[it]);
auto jt=lower_bound(tree[it].begin(), tree[it].end(), val);
if(jt!=tree[it].begin()) ans=max(ans, *(--jt)+val);
val=max(val, tree[it].back());
}
printf("%d\n", val<=B[i].k);
}
}
Compilation message (stderr)
sortbooks.cpp: In function 'void init(int, int, int)':
sortbooks.cpp:26:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
sortbooks.cpp: In function 'void query(int, int, int, int, int, std::vector<int>&)':
sortbooks.cpp:45:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:52:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
sortbooks.cpp:54:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~
sortbooks.cpp:55:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=N; i++) scanf("%d", &A[i]);
~~~~~^~~~~~~~~~~~~
sortbooks.cpp:56:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=M; i++) scanf("%d%d%d", &B[i].l, &B[i].r, &B[i].k);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |