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 <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#define N 1000010
#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mod 1000000007
#define pii pair <int, int>
using namespace std;
int MX;
int n, q;
int v[N];
int arr[N];
pii T[N*4];
int l, r, x;
vector <int> E[N*4];
void uni(int CEP, int SAG, int node)
{
vector <int> res;
pii ans = {-1, -1};
if(node == 0) {
int a = lower_bound(E[SAG].begin(), E[SAG].end(), MX) - E[SAG].begin();
a--;
if(T[CEP].ss == -1 || T[SAG].ss == -1) { ans = (T[CEP].ss == -1 ? T[SAG] : T[CEP]); }
else {
if(T[CEP].ff + T[CEP].ss >= T[SAG].ff + T[SAG].ss) ans = T[CEP];
else ans = T[SAG];
}
if(a != -1) {
int x = E[SAG][a];
if(ans.ss == -1 || ans.ff + ans.ss < MX + x) ans = {MX, x};
}
if(T[node].ss == -1 || (ans.ss != -1 && ans.ff+ans.ss >= T[node].ff + T[node].ss)) T[node] = ans;
MX = max(MX, E[SAG].back());
return;
}
int pos = 0, pos2 = 0;
while(pos < E[CEP].size() && pos2 < E[SAG].size()) {
if(E[CEP][pos] <= E[SAG][pos2]) res.pb(E[CEP][pos++]);
else res.pb(E[SAG][pos2++]);
}
int a = pos2-1;
while(pos < E[CEP].size()) res.pb(E[CEP][pos++]);
while(pos2 < E[SAG].size()) res.pb(E[SAG][pos2++]);
if(T[CEP].ss == -1 || T[SAG].ss == -1) { ans = (T[CEP].ss == -1 ? T[SAG] : T[CEP]); }
else {
if(T[CEP].ff + T[CEP].ss >= T[SAG].ff + T[SAG].ss) ans = T[CEP];
else ans = T[SAG];
}
if(a != -1) {
int x = E[SAG][a];
if(ans.ss == -1 || ans.ff + ans.ss < E[CEP].back() + x) ans = {E[CEP].back(), x};
}
if(T[node].ss == -1 || (ans.ss != -1 && ans.ff+ans.ss >= T[node].ff + T[node].ss)) T[node] = ans;
E[node].clear();
for(auto i: res) E[node].pb(i);
}
void build(int l, int r, int node)
{
if(l == r) {
T[node] = {v[l], -1};
E[node].pb(v[l]);
return;
}
build(l, (l+r)/2, node*2);
build((l+r)/2+1, r, node*2+1);
uni(node*2, node*2+1, node);
}
void tap(int a, int b, int l, int r, int node)
{
if(l > b || r < a) return;
if(l >= a && r <= b) {
uni(0, node, 0);
return;
}
tap(a, b, l, (l+r)/2, node*2);
tap(a, b, (l+r)/2+1, r, node*2+1);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
int mn = 1e9;
for(int i = 1; i <= n; i++) cin >> v[i], mn = min(mn, v[i]);
for(int i = 2; i <= n; i++) {
arr[i] = arr[i-1];
if(v[i] < v[i-1]) arr[i]++;
}
build(1, n, 1);
for(int i = 1; i <= q; i++) {
cin >> l >> r >> x;
if(mn > x) {
int now = arr[r] - arr[l];
if(now) puts("0");
else puts("1");
continue;
}
MX = 0;
T[0] = {-1, -1};
tap(l, r, 1, n, 1);
if(T[0].ss == -1 || T[0].ff + T[0].ss <= x) puts("1");
else puts("0");
}
}
Compilation message (stderr)
sortbooks.cpp: In function 'void uni(int, int, int)':
sortbooks.cpp:58:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | while(pos < E[CEP].size() && pos2 < E[SAG].size()) {
| ~~~~^~~~~~~~~~~~~~~
sortbooks.cpp:58:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | while(pos < E[CEP].size() && pos2 < E[SAG].size()) {
| ~~~~~^~~~~~~~~~~~~~~
sortbooks.cpp:66:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | while(pos < E[CEP].size()) res.pb(E[CEP][pos++]);
| ~~~~^~~~~~~~~~~~~~~
sortbooks.cpp:67:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | while(pos2 < E[SAG].size()) res.pb(E[SAG][pos2++]);
| ~~~~~^~~~~~~~~~~~~~~
# | 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... |