This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//
// A.cpp
// torelax
//
// Created by Rakhman on 1/5/21.
//
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <iterator>
#define ios ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define S second
#define F first
#define pb push_back
#define nl '\n'
#define NL cout << '\n';
#define EX exit(0)
#define all(s) s.begin(), s.end()
#define no_answer {cout << "NO"; exit(0);}
#define FOR(i, start, finish, k) for(llong i = start; i <= finish; i += k)
const long long mxn = 1e6 + 110;
const long long mnn = 1e3 + 2;
const long long mod = 1e9 + 7;
const long long inf = 1e18;
const long long OO = 1e9 + 100;
typedef long long llong;
typedef unsigned long long ullong;
using namespace std;
int n, m, b[mxn], pr[mxn], su[mxn], ans[mxn], pos[mxn];
int Q;
vector<int> rightq[mxn];
struct query{
int l, r, x;
}q[mxn];
void divideandconquer(int l, int r){
if(l == r) return ;
int m = (l + r) / 2;
for(int i = m + 1; i <= r; i++){
pr[i] = b[i];
if(i > m + 1){
ans[i] = ans[i - 1];
pr[i] = max(pr[i - 1], pr[i]);
if(pr[i - 1] > b[i]){
ans[i] = max(ans[i], pr[i - 1] + b[i]);
}
}
}
vector<int> v;
for(int i = m; i >= l; i--) {
su[i] = b[i];
if(i < m) {
ans[i] = ans[i + 1];
su[i] = max(su[i + 1], su[i]);
}
while(v.size() > 0 && v.back() <= b[i]){
if(b[i] > v.back()) ans[i] = max(ans[i], b[i] + v.back());
v.pop_back();
}
v.push_back(b[i]);
}
vector<int> kek;
for(int i = m + 1; i <= r; i++){
if(kek.size() == 0 || kek.back() < b[i]){
kek.push_back(b[i]);
}
for(int j = 0; j < rightq[i].size(); j++){
int left = q[rightq[i][j]].l, k = q[rightq[i][j]].x;
if(pos[rightq[i][j]] == 0) continue;
if(left <= m){
if(max(ans[r], ans[l]) > k) pos[rightq[i][j]] = 0;
else{
int mx = su[l];
int x = lower_bound(kek.begin(), kek.end(), mx) - kek.begin() - 1;
if(0 <= x && x < kek.size()) {
if(mx + kek[x] > k){
pos[rightq[i][j]] = 0;
continue;
}
}
}
}else{
continue;
}
}
}
divideandconquer(l, m);
divideandconquer(m + 1, r);
}
int main() {
cin >> n >> Q;
for(int i = 1; i <= n; i++){
cin >> b[i];
}
for(int i = 1; i <= Q; i++){
cin >> q[i].l >> q[i].r >> q[i].x;
rightq[q[i].r].push_back(i);
pos[i] = 1;
}
divideandconquer(1, n);
for(int i = 1; i <= Q; i++){
cout << pos[i] << nl;
}
}
Compilation message (stderr)
sortbooks.cpp: In function 'void divideandconquer(int, int)':
sortbooks.cpp:92:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for(int j = 0; j < rightq[i].size(); j++){
| ~~^~~~~~~~~~~~~~~~~~
sortbooks.cpp:100:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | if(0 <= x && x < kek.size()) {
| ~~^~~~~~~~~~~~
# | 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... |