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;
#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;
const int INF=0x3f3f3f3f;
const int N=1e6+50;
const int OFF=(1<<20);
int n, q;
int p[N], sol[N];
int T[2*OFF], levi[N];
vector <ppi> que[N];
vector <int> st;
void update(int pos, int val) {
pos+=OFF; T[pos]=max(T[pos], val);
pos/=2;
while (pos) {
T[pos]=max(T[pos*2], T[pos*2+1]);
pos/=2;
}
}
int query(int a, int b, int pos=1, int lo=0, int hi=OFF) {
if (lo>=a && hi<=b) return T[pos];
if (lo>=b || hi<=a) return 0;
int mid=(lo+hi)/2;
return max(query(a, b, pos*2, lo, mid), query(a, b, pos*2+1, mid, hi));
}
void solve() {
for (int i=0; i<n; ++i) {
while (st.size() && p[st.back()]<=p[i]) st.pop_back();
levi[i]=st.empty() ? -1 : st.back();
}
for (int i=0; i<n; ++i) {
if (levi[i]!=-1) update(levi[i], p[i]+p[levi[i]]);
for (ppi pp:que[i]) sol[pp.Y]=query(pp.X.X, i+1)<=pp.X.Y;
}
for (int i=0; i<q; ++i) printf("%d\n", sol[i]);
}
void load() {
scanf("%d %d", &n, &q);
for (int i=0; i<n; ++i) scanf("%d", &p[i]);
for (int i=0; i<q; ++i) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
que[b].pb(mp(mp(a, c), i));
}
}
int main() {
load();
solve();
return 0;
}
Compilation message (stderr)
sortbooks.cpp: In function 'void load()':
sortbooks.cpp:54:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
54 | scanf("%d %d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~
sortbooks.cpp:55:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
55 | for (int i=0; i<n; ++i) scanf("%d", &p[i]);
| ~~~~~^~~~~~~~~~~~~
sortbooks.cpp:58:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
58 | scanf("%d %d %d", &a, &b, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |