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];
int ans[MAXN+10];
vector<int> tree[MAXN*4+10];
int tree2[MAXN*4+10];
vector<pii> todo[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);
}
void solve(int node, int tl, int tr)
{
int i, j;
sort(todo[node].begin(), todo[node].end());
i=0;
for(auto it : todo[node])
{
for(; i<tree[node].size() && i<=it.first; i++);
if(i<tree[node].size()) ans[it.second]=max(ans[it.second], it.first+tree[node][i]);
}
if(tl==tr) return;
int mid=tl+tr>>1;
solve(node*2, tl, mid);
solve(node*2+1, mid+1, tr);
}
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 val=0;
for(auto it : V)
{
ans[i]=max(ans[i], tree2[it]);
todo[it].push_back({val, i});
val=max(val, tree[it].back());
}
}
solve(1, 1, N);
for(i=1; i<=M; i++) printf("%d\n", ans[i]<=B[i].k);
}
Compilation message (stderr)
sortbooks.cpp: In function 'void init(int, int, int)':
sortbooks.cpp:28: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:47:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
sortbooks.cpp: In function 'void solve(int, int, int)':
sortbooks.cpp:60:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; i<tree[node].size() && i<=it.first; i++);
~^~~~~~~~~~~~~~~~~~
sortbooks.cpp:61:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(i<tree[node].size()) ans[it.second]=max(ans[it.second], it.first+tree[node][i]);
~^~~~~~~~~~~~~~~~~~
sortbooks.cpp:65:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
sortbooks.cpp:54:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:72:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
sortbooks.cpp:74: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:75: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:76: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... |