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 fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
static struct FastInput
{
static constexpr int BUF_SIZE = 1 << 20;
char buf[BUF_SIZE];
size_t chars_read = 0;
size_t buf_pos = 0;
FILE *in = stdin;
char cur = 0;
inline char get_char()
{
if (buf_pos >= chars_read)
{
chars_read = fread(buf, 1, BUF_SIZE, in);
buf_pos = 0;
buf[0] = (chars_read == 0 ? -1 : buf[0]);
}
return cur = buf[buf_pos++];
}
inline void tie(int) {}
inline explicit operator bool()
{
return cur != -1;
}
inline static bool is_blank(char c)
{
return c <= ' ';
}
inline bool skip_blanks()
{
while (is_blank(cur) && cur != -1)
{
get_char();
}
return cur != -1;
}
inline FastInput &operator>>(char &c)
{
skip_blanks();
c = cur;
return *this;
}
inline FastInput &operator>>(string &s)
{
if (skip_blanks())
{
s.clear();
do
{
s += cur;
} while (!is_blank(get_char()));
}
return *this;
}
template <typename T>
inline FastInput &read_integer(T &n)
{
// unsafe, doesn't check that characters are actually digits
n = 0;
if (skip_blanks())
{
int sign = +1;
if (cur == '-')
{
sign = -1;
get_char();
}
do
{
n += n + (n << 3) + cur - '0';
} while (!is_blank(get_char()));
n *= sign;
}
return *this;
}
template <typename T>
inline typename enable_if<is_integral<T>::value, FastInput &>::type operator>>(T &n)
{
return read_integer(n);
}
#if !defined(_WIN32) || defined(_WIN64)
inline FastInput &operator>>(__int128 &n)
{
return read_integer(n);
}
#endif
template <typename T>
inline typename enable_if<is_floating_point<T>::value, FastInput &>::type operator>>(T &n)
{
// not sure if really fast, for compatibility only
n = 0;
if (skip_blanks())
{
string s;
(*this) >> s;
sscanf(s.c_str(), "%lf", &n);
}
return *this;
}
} fast_input;
#define cin fast_input
static struct FastOutput
{
static constexpr int BUF_SIZE = 1 << 20;
char buf[BUF_SIZE];
size_t buf_pos = 0;
static constexpr int TMP_SIZE = 1 << 20;
char tmp[TMP_SIZE];
FILE *out = stdout;
inline void put_char(char c)
{
buf[buf_pos++] = c;
if (buf_pos == BUF_SIZE)
{
fwrite(buf, 1, buf_pos, out);
buf_pos = 0;
}
}
~FastOutput()
{
fwrite(buf, 1, buf_pos, out);
}
inline FastOutput &operator<<(char c)
{
put_char(c);
return *this;
}
inline FastOutput &operator<<(const char *s)
{
while (*s)
{
put_char(*s++);
}
return *this;
}
inline FastOutput &operator<<(const string &s)
{
for (int i = 0; i < (int)s.size(); i++)
{
put_char(s[i]);
}
return *this;
}
template <typename T>
inline char *integer_to_string(T n)
{
// beware of TMP_SIZE
char *p = tmp + TMP_SIZE - 1;
if (n == 0)
{
*--p = '0';
}
else
{
bool is_negative = false;
if (n < 0)
{
is_negative = true;
n = -n;
}
while (n > 0)
{
*--p = (char)('0' + n % 10);
n /= 10;
}
if (is_negative)
{
*--p = '-';
}
}
return p;
}
template <typename T>
inline typename enable_if<is_integral<T>::value, char *>::type stringify(T n)
{
return integer_to_string(n);
}
#if !defined(_WIN32) || defined(_WIN64)
inline char *stringify(__int128 n)
{
return integer_to_string(n);
}
#endif
template <typename T>
inline typename enable_if<is_floating_point<T>::value, char *>::type stringify(T n)
{
sprintf(tmp, "%.17f", n);
return tmp;
}
template <typename T>
inline FastOutput &operator<<(const T &n)
{
auto p = stringify(n);
for (; *p != 0; p++)
{
put_char(*p);
}
return *this;
}
} fast_output;
#define cout fast_output
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
x += k;
x %= mod;
if (x < 0)
x += mod;
}
void del(int &x, int k)
{
x -= k;
x %= mod;
if (x < 0)
x += mod;
}
const int MAXN = (int)3e5 + 9;
const int MAXV = (int)1e9 + 9; //maximum value of any element in array
//array values can be negative too, use appropriate minimum and maximum value
struct wavelet_tree {
int lo, hi;
wavelet_tree *l, *r;
int *b, *c, bsz, csz; // c holds the prefix sum of elements
wavelet_tree() {
lo = 1;
hi = 0;
bsz = 0;
csz = 0, l = NULL;
r = NULL;
}
void init(int *from, int *to, int x, int y) {
lo = x, hi = y;
if(from >= to) return;
int mid = (lo + hi) >> 1;
auto f = [mid](int x) {
return x <= mid;
};
b = (int*)malloc((to - from + 2) * sizeof(int));
bsz = 0;
b[bsz++] = 0;
c = (int*)malloc((to - from + 2) * sizeof(int));
csz = 0;
c[csz++] = 0;
for(auto it = from; it != to; it++) {
b[bsz] = (b[bsz - 1] + f(*it));
c[csz] = (c[csz - 1] + (*it));
bsz++;
csz++;
}
if(hi == lo) return;
auto pivot = stable_partition(from, to, f);
l = new wavelet_tree();
l->init(from, pivot, lo, mid);
r = new wavelet_tree();
r->init(pivot, to, mid + 1, hi);
}
//kth smallest element in [l, r]
//for array [1,2,1,3,5] 2nd smallest is 1 and 3rd smallest is 2
int kth(int l, int r, int k) {
if(l > r) return 0;
if(lo == hi) return lo;
int inLeft = b[r] - b[l - 1], lb = b[l - 1], rb = b[r];
if(k <= inLeft) return this->l->kth(lb + 1, rb, k);
return this->r->kth(l - lb, r - rb, k - inLeft);
}
//count of numbers in [l, r] Less than or equal to k
int LTE(int l, int r, int k) {
if(l > r || k < lo) return 0;
if(hi <= k) return r - l + 1;
int lb = b[l - 1], rb = b[r];
return this->l->LTE(lb + 1, rb, k) + this->r->LTE(l - lb, r - rb, k);
}
//count of numbers in [l, r] equal to k
int count(int l, int r, int k) {
if(l > r || k < lo || k > hi) return 0;
if(lo == hi) return r - l + 1;
int lb = b[l - 1], rb = b[r];
int mid = (lo + hi) >> 1;
if(k <= mid) return this->l->count(lb + 1, rb, k);
return this->r->count(l - lb, r - rb, k);
}
//sum of numbers in [l ,r] less than or equal to k
int sum(int l, int r, int k) {
if(l > r or k < lo) return 0;
if(hi <= k) return c[r] - c[l - 1];
int lb = b[l - 1], rb = b[r];
return this->l->sum(lb + 1, rb, k) + this->r->sum(l - lb, r - rb, k);
}
~wavelet_tree() {
delete l;
delete r;
}
}tree;
struct BIT
{
int t[maxn];
BIT()
{
fo(i, 0, maxn - 1) t[i] = -1;
}
void up(int x, int val)
{
for (; x < maxn; x += lb(x))
maximize(t[x], val);
}
void clear(int x)
{
for (; x < maxn; x += lb(x))
t[x] = -1;
}
int get(int x)
{
int ans = -1;
for (; x; x -= lb(x))
maximize(ans, t[x]);
return ans;
}
} minn;
struct BIT1
{
int t[maxn];
BIT1()
{
fo(i, 1, maxn - 1) t[i] = -1;
}
void up(int x, int val)
{
for (; x; x -= lb(x))
maximize(t[x], val);
}
void clear(int x)
{
for (; x; x -= lb(x))
t[x] = -1;
}
int get(int x)
{
int ans = -1;
for (; x < maxn; x += lb(x))
maximize(ans, t[x]);
return ans;
}
} maxx;
int n, q, a[maxn];
int res[maxn];
int sum[maxn];
int maxl[maxn];
vi nen;
int arr[maxn];
void calc(int l, int r, vector<array<int, 4>> qry)
{
int mid = l + r >> 1;
maxl[mid] = a[mid];
fod(i, mid - 1, l)
{
maxl[i] = max(maxl[i + 1], a[i]);
minn.up(arr[i + 1], a[i + 1]);
int x = minn.get(arr[i] - 1);
if (x != -1)
sum[i] = x + a[i];
maximize(sum[i], sum[i + 1]);
}
fo(i, mid + 2, r)
{
maxx.up(arr[i - 1], a[i - 1]);
int x = maxx.get(arr[i] + 1);
if (x != -1)
sum[i] = x + a[i];
maximize(sum[i], sum[i - 1]);
}
fod(i, mid, l) minn.clear(arr[i]);
fo(i, mid + 1, r) maxx.clear(arr[i]);
vector<array<int, 4>> ll, rr;
for (auto [l, r, val, id] : qry)
if (l != r)
{
if (l <= mid and r >= mid)
{
if (max(sum[l], sum[r]) > val)
res[id] = 1;
else
{
int L = max(val - maxl[l], 0);
if (tree.LTE(mid + 1, r, maxl[l] - 1) -
tree.LTE(mid + 1, r, L) >
0)
res[id] = 1;
}
}
else if (r < mid)
ll.push_back({l, r, val, id});
else if (l > mid)
rr.push_back({l, r, val, id});
}
fo(i,l,r) sum[i] = maxl[i] = 0;
if (l != r)
{
calc(l, mid, ll);
calc(mid + 1, r, rr);
}
}
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
fo(i, 1, n)
{
cin >> a[i];
nen.pb(a[i]);
}
tree.init(a + 1,a + n + 1,-MAXV,MAXV);
sort(all(nen));
nen.erase(unique(all(nen)), nen.end());
fo(i,1,n) arr[i] = lower_bound(all(nen),a[i]) - nen.begin() + 1;
vector<array<int, 4>> qry(q);
fo(i, 0, q - 1)
{
cin >> qry[i][0] >> qry[i][1] >> qry[i][2];
qry[i][3] = i + 1;
}
calc(1, n, qry);
fo(i, 1, q) cout << (res[i] ^ 1) << "\n";
}
Compilation message (stderr)
sortbooks.cpp: In function 'void calc(int, int, std::vector<std::array<int, 4> >)':
sortbooks.cpp:419:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
419 | int mid = l + r >> 1;
| ~~^~~
sortbooks.cpp: At global scope:
sortbooks.cpp:469:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
469 | main()
| ^~~~
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:478:13: warning: passing NULL to non-pointer argument 1 of 'void FastInput::tie(int)' [-Wconversion-null]
478 | cin.tie(NULL);
| ^~~~
sortbooks.cpp:61:21: note: declared here
61 | inline void tie(int) {}
| ^~~
sortbooks.cpp:474:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
474 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:475:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
475 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |