#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;
const int MAXN = maxn;
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;
}
};
wavelet_tree t;
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);
// cout << maxl[l] - 1 << ' ' << L;en;
// cout << t.LTE(mid + 1,r,maxl[l] - 1) << ' ' << t.LTE(mid + 1,r,L);en;
if (t.LTE(mid + 1, r, maxl[l] - 1) > t.LTE(mid + 1, r, L))
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);
}
}
int ww[maxn];
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];
ww[i] = a[i];
nen.pb(a[i]);
}
t.init(ww + 1,ww + 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
sortbooks.cpp: In function 'void calc(int, int, std::vector<std::array<int, 4> >)':
sortbooks.cpp:202:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
202 | int mid = l + r >> 1;
| ~~^~~
sortbooks.cpp: At global scope:
sortbooks.cpp:253:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
253 | main()
| ^~~~
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:258:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
258 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:259:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
259 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8280 KB |
Output is correct |
2 |
Correct |
4 ms |
8284 KB |
Output is correct |
3 |
Correct |
4 ms |
9048 KB |
Output is correct |
4 |
Correct |
4 ms |
8488 KB |
Output is correct |
5 |
Correct |
4 ms |
9052 KB |
Output is correct |
6 |
Correct |
7 ms |
10332 KB |
Output is correct |
7 |
Correct |
6 ms |
10332 KB |
Output is correct |
8 |
Correct |
5 ms |
8536 KB |
Output is correct |
9 |
Correct |
4 ms |
8284 KB |
Output is correct |
10 |
Correct |
4 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8280 KB |
Output is correct |
2 |
Correct |
4 ms |
8284 KB |
Output is correct |
3 |
Correct |
4 ms |
9048 KB |
Output is correct |
4 |
Correct |
4 ms |
8488 KB |
Output is correct |
5 |
Correct |
4 ms |
9052 KB |
Output is correct |
6 |
Correct |
7 ms |
10332 KB |
Output is correct |
7 |
Correct |
6 ms |
10332 KB |
Output is correct |
8 |
Correct |
5 ms |
8536 KB |
Output is correct |
9 |
Correct |
4 ms |
8284 KB |
Output is correct |
10 |
Correct |
4 ms |
8284 KB |
Output is correct |
11 |
Correct |
11 ms |
13912 KB |
Output is correct |
12 |
Correct |
25 ms |
25672 KB |
Output is correct |
13 |
Correct |
25 ms |
25692 KB |
Output is correct |
14 |
Correct |
25 ms |
26204 KB |
Output is correct |
15 |
Correct |
26 ms |
26204 KB |
Output is correct |
16 |
Correct |
10 ms |
11100 KB |
Output is correct |
17 |
Correct |
10 ms |
10588 KB |
Output is correct |
18 |
Correct |
10 ms |
9824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
287 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
172 ms |
43076 KB |
Output is correct |
2 |
Correct |
169 ms |
41272 KB |
Output is correct |
3 |
Correct |
182 ms |
42348 KB |
Output is correct |
4 |
Correct |
182 ms |
43452 KB |
Output is correct |
5 |
Correct |
187 ms |
44612 KB |
Output is correct |
6 |
Correct |
152 ms |
41032 KB |
Output is correct |
7 |
Correct |
153 ms |
41032 KB |
Output is correct |
8 |
Correct |
144 ms |
42056 KB |
Output is correct |
9 |
Correct |
60 ms |
14972 KB |
Output is correct |
10 |
Correct |
146 ms |
42060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8280 KB |
Output is correct |
2 |
Correct |
4 ms |
8284 KB |
Output is correct |
3 |
Correct |
4 ms |
9048 KB |
Output is correct |
4 |
Correct |
4 ms |
8488 KB |
Output is correct |
5 |
Correct |
4 ms |
9052 KB |
Output is correct |
6 |
Correct |
7 ms |
10332 KB |
Output is correct |
7 |
Correct |
6 ms |
10332 KB |
Output is correct |
8 |
Correct |
5 ms |
8536 KB |
Output is correct |
9 |
Correct |
4 ms |
8284 KB |
Output is correct |
10 |
Correct |
4 ms |
8284 KB |
Output is correct |
11 |
Correct |
11 ms |
13912 KB |
Output is correct |
12 |
Correct |
25 ms |
25672 KB |
Output is correct |
13 |
Correct |
25 ms |
25692 KB |
Output is correct |
14 |
Correct |
25 ms |
26204 KB |
Output is correct |
15 |
Correct |
26 ms |
26204 KB |
Output is correct |
16 |
Correct |
10 ms |
11100 KB |
Output is correct |
17 |
Correct |
10 ms |
10588 KB |
Output is correct |
18 |
Correct |
10 ms |
9824 KB |
Output is correct |
19 |
Runtime error |
216 ms |
262144 KB |
Execution killed with signal 9 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8280 KB |
Output is correct |
2 |
Correct |
4 ms |
8284 KB |
Output is correct |
3 |
Correct |
4 ms |
9048 KB |
Output is correct |
4 |
Correct |
4 ms |
8488 KB |
Output is correct |
5 |
Correct |
4 ms |
9052 KB |
Output is correct |
6 |
Correct |
7 ms |
10332 KB |
Output is correct |
7 |
Correct |
6 ms |
10332 KB |
Output is correct |
8 |
Correct |
5 ms |
8536 KB |
Output is correct |
9 |
Correct |
4 ms |
8284 KB |
Output is correct |
10 |
Correct |
4 ms |
8284 KB |
Output is correct |
11 |
Correct |
11 ms |
13912 KB |
Output is correct |
12 |
Correct |
25 ms |
25672 KB |
Output is correct |
13 |
Correct |
25 ms |
25692 KB |
Output is correct |
14 |
Correct |
25 ms |
26204 KB |
Output is correct |
15 |
Correct |
26 ms |
26204 KB |
Output is correct |
16 |
Correct |
10 ms |
11100 KB |
Output is correct |
17 |
Correct |
10 ms |
10588 KB |
Output is correct |
18 |
Correct |
10 ms |
9824 KB |
Output is correct |
19 |
Runtime error |
287 ms |
262144 KB |
Execution killed with signal 9 |
20 |
Halted |
0 ms |
0 KB |
- |