#include <bits/stdc++.h>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define MAXN 1000013
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef pair<pii, pii> ppp;
int N, Q;
int arr[MAXN];
int pos[MAXN];
int sparse[20][MAXN];
vector<ppp> queries;
bitset<MAXN> ans;
//max (a[i] + a[j]) such that a[i] > a[j]
bool cmp(ppp a, ppp b)
{
return a.fi.se < b.fi.se;
}
int query(int l, int r)
{
int sz = 31 - __builtin_clz(r - l + 1);
return max(sparse[sz][l], sparse[sz][r - (1 << sz) + 1]);
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> Q;
int mxv = 0, mnw = 1000000013, mxw = 0;
FOR(i, 0, N)
{
cin >> arr[i];
ckmin(mnw, arr[i]);
ckmax(mxw, arr[i]);
}
FOR(i, 0, Q)
{
int l, r, v; cin >> l >> r >> v; l--; r--; ckmax(mxv, v);
queries.PB({{l, r}, {v, i}});
}
FOR(i, 0, N)
{
sparse[0][i] = arr[i];
}
FOR(i, 1, 20)
{
FOR(j, 0, N - (1 << i))
{
sparse[i][j] = max(sparse[i - 1][j], sparse[i - 1][j + (1 << (i - 1))]);
}
}
if (N <= 5000)
{
//subtask 1, 2
FOR(i, 0, Q)
{
int l = queries[i].fi.fi, r = queries[i].fi.se, v = queries[i].se.fi, qid = queries[i].se.se;
FOR(j, l + 1, r + 1)
{
int mx = query(l, j - 1);
if (mx > arr[j] && mx + arr[j] > v)
{
ans[qid] = true;
break;
}
}
}
}
else if (mxw <= 1000)
{
//subtask 4
sort(ALL(queries), cmp);
int iter = 0;
//store rightmost positino of each #
FOR(i, 0, 1001) pos[i] = -1;
FOR(i, 0, N)
{
pos[arr[i]] = i;
while(iter < Q && queries[iter].fi.se == i)
{
int l = queries[iter].fi.fi, r = queries[iter].fi.se, v = queries[iter].se.fi, qid = queries[iter].se.se;
FOR(j, 0, 1001)
{
int x = pos[j];
if (x <= l) continue;
int mx = query(l, x - 1);
if (mx > j && mx + j > v)
{
ans[qid] = true;
break;
}
}
iter++;
}
}
}
else if (mxv < mnw)
{
//subtask 3
//you just need to figure out if it's <= here or not.
pos[N - 1] = N - 1;
FORD(i, N - 1, 0)
{
pos[i] = (arr[i + 1] < arr[i] ? i : pos[i + 1]);
}
FOR(i, 0, Q)
{
int l = queries[i].fi.fi, r = queries[i].fi.se, v = queries[i].se.fi, qid = queries[i].se.se;
ans[qid] = (pos[l] < r);
}
}
//left, right, value, qid;
FOR(i, 0, Q)
{
cout << (ans[i] ? 0 : 1) << '\n';
}
}
Compilation message
sortbooks.cpp: In function 'int32_t main()':
sortbooks.cpp:113:34: warning: unused variable 'r' [-Wunused-variable]
int l = queries[iter].fi.fi, r = queries[iter].fi.se, v = queries[iter].se.fi, qid = queries[iter].se.se;
^
sortbooks.cpp:140:52: warning: unused variable 'v' [-Wunused-variable]
int l = queries[i].fi.fi, r = queries[i].fi.se, v = queries[i].se.fi, qid = queries[i].se.se;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
4 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
508 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
4 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
508 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
4 ms |
636 KB |
Output is correct |
12 |
Correct |
5 ms |
760 KB |
Output is correct |
13 |
Correct |
5 ms |
760 KB |
Output is correct |
14 |
Correct |
6 ms |
888 KB |
Output is correct |
15 |
Correct |
6 ms |
916 KB |
Output is correct |
16 |
Correct |
39 ms |
1016 KB |
Output is correct |
17 |
Correct |
40 ms |
888 KB |
Output is correct |
18 |
Correct |
34 ms |
888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
700 ms |
100288 KB |
Output is correct |
2 |
Correct |
712 ms |
100288 KB |
Output is correct |
3 |
Correct |
707 ms |
100440 KB |
Output is correct |
4 |
Correct |
694 ms |
100376 KB |
Output is correct |
5 |
Correct |
724 ms |
100544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
249 ms |
9024 KB |
Output is correct |
2 |
Correct |
239 ms |
8988 KB |
Output is correct |
3 |
Correct |
336 ms |
10892 KB |
Output is correct |
4 |
Correct |
277 ms |
10904 KB |
Output is correct |
5 |
Correct |
253 ms |
10904 KB |
Output is correct |
6 |
Correct |
258 ms |
10828 KB |
Output is correct |
7 |
Correct |
260 ms |
10828 KB |
Output is correct |
8 |
Correct |
82 ms |
10728 KB |
Output is correct |
9 |
Correct |
125 ms |
3660 KB |
Output is correct |
10 |
Correct |
78 ms |
10600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
4 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
508 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
4 ms |
636 KB |
Output is correct |
12 |
Correct |
5 ms |
760 KB |
Output is correct |
13 |
Correct |
5 ms |
760 KB |
Output is correct |
14 |
Correct |
6 ms |
888 KB |
Output is correct |
15 |
Correct |
6 ms |
916 KB |
Output is correct |
16 |
Correct |
39 ms |
1016 KB |
Output is correct |
17 |
Correct |
40 ms |
888 KB |
Output is correct |
18 |
Correct |
34 ms |
888 KB |
Output is correct |
19 |
Incorrect |
146 ms |
18148 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
4 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
508 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
4 ms |
636 KB |
Output is correct |
12 |
Correct |
5 ms |
760 KB |
Output is correct |
13 |
Correct |
5 ms |
760 KB |
Output is correct |
14 |
Correct |
6 ms |
888 KB |
Output is correct |
15 |
Correct |
6 ms |
916 KB |
Output is correct |
16 |
Correct |
39 ms |
1016 KB |
Output is correct |
17 |
Correct |
40 ms |
888 KB |
Output is correct |
18 |
Correct |
34 ms |
888 KB |
Output is correct |
19 |
Correct |
700 ms |
100288 KB |
Output is correct |
20 |
Correct |
712 ms |
100288 KB |
Output is correct |
21 |
Correct |
707 ms |
100440 KB |
Output is correct |
22 |
Correct |
694 ms |
100376 KB |
Output is correct |
23 |
Correct |
724 ms |
100544 KB |
Output is correct |
24 |
Correct |
249 ms |
9024 KB |
Output is correct |
25 |
Correct |
239 ms |
8988 KB |
Output is correct |
26 |
Correct |
336 ms |
10892 KB |
Output is correct |
27 |
Correct |
277 ms |
10904 KB |
Output is correct |
28 |
Correct |
253 ms |
10904 KB |
Output is correct |
29 |
Correct |
258 ms |
10828 KB |
Output is correct |
30 |
Correct |
260 ms |
10828 KB |
Output is correct |
31 |
Correct |
82 ms |
10728 KB |
Output is correct |
32 |
Correct |
125 ms |
3660 KB |
Output is correct |
33 |
Correct |
78 ms |
10600 KB |
Output is correct |
34 |
Incorrect |
146 ms |
18148 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |