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>
#define all(s) s.begin(), s.end()
#define pii pair<int, int>
#define fr first
#define sc second
#define pb push_back
using namespace std;
const int N = (int)1e6 + 6;
int a[N];
struct T {
int mx, best;
vector <int> vec = {};
};
T tree[N * 4];
int n, Q;
void build(int v, int tl, int tr) {
if (tl == tr) {
tree[v].mx = a[tl];
tree[v].vec = {a[tl]};
tree[v].best = 0;
return;
}
int mid = tl + tr >> 1;
build(v + v, tl, mid);
build(v + v + 1, mid + 1, tr);
merge(tree[v + v].vec.begin(), tree[v + v].vec.end(), tree[v + v + 1].vec.begin(), tree[v + v + 1].vec.end(), back_inserter(tree[v].vec));
tree[v].mx = max(tree[v + v].mx, tree[v + v + 1].mx);
tree[v].best = max(tree[v + v].best, tree[v + v + 1].best);
int another = 0;
if (tree[v + v].mx > tree[v + v + 1].vec[0]) {
another = tree[v + v].mx + *(--lower_bound(all(tree[v + v + 1].vec), tree[v + v].mx));
}
tree[v].best = max(tree[v].best, another);
}
pii get(int l, int r, int v = 1, int tl = 1, int tr = n, int Curmx = 0) { // best / maximum in l...r
if (l > tr || tl > r) {
return {0, 0};
}
if (l <= tl && tr <= r) {
int another = 0;
if (tree[v].vec[0] < Curmx) {
another = Curmx + *(--lower_bound(all(tree[v].vec), Curmx));
}
return {max(tree[v].best, another), tree[v].mx};
}
int mid = tl + tr >> 1;
int ans = 0;
pii q1 = get(l, r, v + v, tl, mid, Curmx);
pii q2 = get(l, r, v + v + 1, mid + 1, tr, max(Curmx, q1.sc));
return {max(q1.fr, q2.fr), max(q1.sc, q2.sc)};
}
void subtask3(vector <pair<pii, int>> query) {
bool ok = 1;
int mn = tree[1].vec[0];
for (auto c : query) {
ok &= (c.sc < mn);
}
if (ok) {
vector <int> pref(n + 5, 0);
a[n + 1] = 2e9 + 9;
for (int i = 1; i <= n; i++) {
pref[i] = pref[i - 1];
if (a[i] > a[i + 1]) {
pref[i]++;
}
}
for (auto c : query) {
int l = c.fr.fr, r = c.fr.sc, k = c.sc;
if (l == r) {
puts("1");
} else {
if (pref[r - 1] - pref[l - 1]) {
puts("0");
} else {
puts("1");
}
}
}
exit(0);
} else {
return;
}
}
main() {
cin >> n >> Q;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
build(1, 1, n);
vector <pair<pii, int>> query;
while (Q--) {
int l, r, k;
scanf("%d %d %d", &l, &r, &k);
query.pb({{l, r}, k});
}
subtask3(query);
for (auto c : query) {
if (get(c.fr.fr, c.fr.sc).fr <= c.sc) {
puts("1");
} else {
puts("0");
}
}
}
Compilation message (stderr)
sortbooks.cpp: In function 'void build(int, int, int)':
sortbooks.cpp:26:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = tl + tr >> 1;
~~~^~~~
sortbooks.cpp: In function 'std::pair<int, int> get(int, int, int, int, int, int)':
sortbooks.cpp:50:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = tl + tr >> 1;
~~~^~~~
sortbooks.cpp:51:7: warning: unused variable 'ans' [-Wunused-variable]
int ans = 0;
^~~
sortbooks.cpp: In function 'void subtask3(std::vector<std::pair<std::pair<int, int>, int> >)':
sortbooks.cpp:73:37: warning: unused variable 'k' [-Wunused-variable]
int l = c.fr.fr, r = c.fr.sc, k = c.sc;
^
sortbooks.cpp: At global scope:
sortbooks.cpp:90:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:93:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i]);
~~~~~^~~~~~~~~~~~~
sortbooks.cpp:100:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &l, &r, &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... |