#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
typedef long long ll;
const int maxn = 1e6+10;
const int inf = 1e9+10;
int n;
int a[maxn];
int ans[maxn];
struct Query
{
int l, r, k;
int ind;
} qry[maxn];
struct SegmentTree
{
ll tree[4*maxn];
ll lazy[4*maxn];
void build(int node, int l, int r)
{
if (l == r)
{
tree[node] = a[l];
return;
}
int mid = (l+r)>>1;
build(2*node, l, mid); build(2*node+1, mid+1, r);
tree[node] = max(tree[2*node], tree[2*node+1]);
}
void flush(int node, int l, int r)
{
if (!lazy[node]) return;
if (l != r)
lazy[2*node] += lazy[node], lazy[2*node+1] += lazy[node];
tree[node] += lazy[node];
lazy[node] = 0;
}
void upd(int node, int l, int r, int a, int b, int v)
{
flush(node, l, r);
if (l > r || l > b || r < a) return;
if (l >= a && r <= b)
{
lazy[node] = 1ll*v;
flush(node, l, r);
return;
}
int mid = (l+r)>>1;
upd(2*node, l, mid, a, b, v); upd(2*node+1, mid+1, r, a, b, v);
tree[node] = max(tree[2*node], tree[2*node+1]);
}
int query(int node, int l, int r, int a, int b)
{
flush(node, l, r);
if (l > b || r < a) return 0;
if (l >= a && r <= b) return tree[node];
int mid = (l+r)>>1;
return max(query(2*node, l, mid, a, b), query(2*node+1, mid+1, r, a, b));
}
} seg;
int main(void)
{
int m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
seg.build(1, 1, n);
for (int i = 1; i <= m; i++)
{
scanf("%d %d %d", &qry[i].l, &qry[i].r, &qry[i].k);
qry[i].ind = i;
}
sort(qry+1, qry+m+1, [&] (Query a, Query b) {return a.l > b.l;});
int ptr = n+1;
stack<int> stk;
for (int i = 1; i <= m; i++)
{
int l = qry[i].l, r = qry[i].r, k = qry[i].k;
while (ptr > l)
{
ptr--;
seg.upd(1, 1, n, ptr, ptr, -a[ptr]);
while (stk.size() && a[ptr] > a[stk.top()])
{
int ind = stk.top(); stk.pop();
seg.upd(1, 1, n, ind, ind, a[ind]);
seg.upd(1, 1, n, ind+1, (stk.size() ? stk.top()-1 : n), -a[ind]);
}
seg.upd(1, 1, n, ptr+1, (stk.size() ? stk.top()-1 : n), a[ptr]);
stk.push(ptr);
}
if (seg.query(1, 1, n, l, r) > k) ans[qry[i].ind] = 0;
else ans[qry[i].ind] = 1;
}
for (int i = 1; i <= m; i++)
printf("%d\n", ans[i]);
}
Compilation message
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:88:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
88 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
sortbooks.cpp:91:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
91 | scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
sortbooks.cpp:97:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
97 | scanf("%d %d %d", &qry[i].l, &qry[i].r, &qry[i].k);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
5 ms |
492 KB |
Output is correct |
12 |
Correct |
8 ms |
748 KB |
Output is correct |
13 |
Correct |
10 ms |
748 KB |
Output is correct |
14 |
Correct |
9 ms |
748 KB |
Output is correct |
15 |
Correct |
9 ms |
748 KB |
Output is correct |
16 |
Correct |
8 ms |
896 KB |
Output is correct |
17 |
Correct |
5 ms |
620 KB |
Output is correct |
18 |
Correct |
7 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2864 ms |
58612 KB |
Output is correct |
2 |
Correct |
2826 ms |
58888 KB |
Output is correct |
3 |
Correct |
2807 ms |
58796 KB |
Output is correct |
4 |
Correct |
2842 ms |
58680 KB |
Output is correct |
5 |
Correct |
2114 ms |
58768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
206 ms |
7020 KB |
Output is correct |
2 |
Correct |
199 ms |
7020 KB |
Output is correct |
3 |
Correct |
157 ms |
6892 KB |
Output is correct |
4 |
Correct |
152 ms |
6892 KB |
Output is correct |
5 |
Correct |
154 ms |
7020 KB |
Output is correct |
6 |
Correct |
141 ms |
6912 KB |
Output is correct |
7 |
Correct |
143 ms |
6916 KB |
Output is correct |
8 |
Correct |
172 ms |
7020 KB |
Output is correct |
9 |
Correct |
51 ms |
2540 KB |
Output is correct |
10 |
Correct |
171 ms |
7020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
5 ms |
492 KB |
Output is correct |
12 |
Correct |
8 ms |
748 KB |
Output is correct |
13 |
Correct |
10 ms |
748 KB |
Output is correct |
14 |
Correct |
9 ms |
748 KB |
Output is correct |
15 |
Correct |
9 ms |
748 KB |
Output is correct |
16 |
Correct |
8 ms |
896 KB |
Output is correct |
17 |
Correct |
5 ms |
620 KB |
Output is correct |
18 |
Correct |
7 ms |
748 KB |
Output is correct |
19 |
Correct |
471 ms |
13676 KB |
Output is correct |
20 |
Correct |
470 ms |
13676 KB |
Output is correct |
21 |
Correct |
435 ms |
13676 KB |
Output is correct |
22 |
Correct |
422 ms |
13676 KB |
Output is correct |
23 |
Correct |
423 ms |
13676 KB |
Output is correct |
24 |
Correct |
316 ms |
13548 KB |
Output is correct |
25 |
Correct |
310 ms |
13420 KB |
Output is correct |
26 |
Correct |
337 ms |
13676 KB |
Output is correct |
27 |
Correct |
344 ms |
13420 KB |
Output is correct |
28 |
Correct |
338 ms |
13420 KB |
Output is correct |
29 |
Correct |
347 ms |
13420 KB |
Output is correct |
30 |
Correct |
346 ms |
13548 KB |
Output is correct |
31 |
Correct |
343 ms |
13420 KB |
Output is correct |
32 |
Correct |
349 ms |
13548 KB |
Output is correct |
33 |
Correct |
356 ms |
13468 KB |
Output is correct |
34 |
Correct |
309 ms |
13420 KB |
Output is correct |
35 |
Correct |
301 ms |
13420 KB |
Output is correct |
36 |
Correct |
309 ms |
13420 KB |
Output is correct |
37 |
Correct |
301 ms |
13420 KB |
Output is correct |
38 |
Correct |
303 ms |
13420 KB |
Output is correct |
39 |
Correct |
348 ms |
13676 KB |
Output is correct |
40 |
Correct |
245 ms |
8940 KB |
Output is correct |
41 |
Correct |
359 ms |
13676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
5 ms |
492 KB |
Output is correct |
12 |
Correct |
8 ms |
748 KB |
Output is correct |
13 |
Correct |
10 ms |
748 KB |
Output is correct |
14 |
Correct |
9 ms |
748 KB |
Output is correct |
15 |
Correct |
9 ms |
748 KB |
Output is correct |
16 |
Correct |
8 ms |
896 KB |
Output is correct |
17 |
Correct |
5 ms |
620 KB |
Output is correct |
18 |
Correct |
7 ms |
748 KB |
Output is correct |
19 |
Correct |
2864 ms |
58612 KB |
Output is correct |
20 |
Correct |
2826 ms |
58888 KB |
Output is correct |
21 |
Correct |
2807 ms |
58796 KB |
Output is correct |
22 |
Correct |
2842 ms |
58680 KB |
Output is correct |
23 |
Correct |
2114 ms |
58768 KB |
Output is correct |
24 |
Correct |
206 ms |
7020 KB |
Output is correct |
25 |
Correct |
199 ms |
7020 KB |
Output is correct |
26 |
Correct |
157 ms |
6892 KB |
Output is correct |
27 |
Correct |
152 ms |
6892 KB |
Output is correct |
28 |
Correct |
154 ms |
7020 KB |
Output is correct |
29 |
Correct |
141 ms |
6912 KB |
Output is correct |
30 |
Correct |
143 ms |
6916 KB |
Output is correct |
31 |
Correct |
172 ms |
7020 KB |
Output is correct |
32 |
Correct |
51 ms |
2540 KB |
Output is correct |
33 |
Correct |
171 ms |
7020 KB |
Output is correct |
34 |
Correct |
471 ms |
13676 KB |
Output is correct |
35 |
Correct |
470 ms |
13676 KB |
Output is correct |
36 |
Correct |
435 ms |
13676 KB |
Output is correct |
37 |
Correct |
422 ms |
13676 KB |
Output is correct |
38 |
Correct |
423 ms |
13676 KB |
Output is correct |
39 |
Correct |
316 ms |
13548 KB |
Output is correct |
40 |
Correct |
310 ms |
13420 KB |
Output is correct |
41 |
Correct |
337 ms |
13676 KB |
Output is correct |
42 |
Correct |
344 ms |
13420 KB |
Output is correct |
43 |
Correct |
338 ms |
13420 KB |
Output is correct |
44 |
Correct |
347 ms |
13420 KB |
Output is correct |
45 |
Correct |
346 ms |
13548 KB |
Output is correct |
46 |
Correct |
343 ms |
13420 KB |
Output is correct |
47 |
Correct |
349 ms |
13548 KB |
Output is correct |
48 |
Correct |
356 ms |
13468 KB |
Output is correct |
49 |
Correct |
309 ms |
13420 KB |
Output is correct |
50 |
Correct |
301 ms |
13420 KB |
Output is correct |
51 |
Correct |
309 ms |
13420 KB |
Output is correct |
52 |
Correct |
301 ms |
13420 KB |
Output is correct |
53 |
Correct |
303 ms |
13420 KB |
Output is correct |
54 |
Correct |
348 ms |
13676 KB |
Output is correct |
55 |
Correct |
245 ms |
8940 KB |
Output is correct |
56 |
Correct |
359 ms |
13676 KB |
Output is correct |
57 |
Correct |
2775 ms |
74716 KB |
Output is correct |
58 |
Correct |
2813 ms |
72400 KB |
Output is correct |
59 |
Correct |
2632 ms |
71660 KB |
Output is correct |
60 |
Correct |
2642 ms |
72428 KB |
Output is correct |
61 |
Correct |
2630 ms |
71568 KB |
Output is correct |
62 |
Correct |
2617 ms |
72376 KB |
Output is correct |
63 |
Correct |
1736 ms |
71036 KB |
Output is correct |
64 |
Correct |
1741 ms |
71040 KB |
Output is correct |
65 |
Correct |
1947 ms |
72120 KB |
Output is correct |
66 |
Correct |
1941 ms |
75244 KB |
Output is correct |
67 |
Correct |
1969 ms |
74092 KB |
Output is correct |
68 |
Correct |
2021 ms |
75244 KB |
Output is correct |
69 |
Correct |
2020 ms |
75004 KB |
Output is correct |
70 |
Correct |
2005 ms |
71616 KB |
Output is correct |
71 |
Correct |
2031 ms |
75372 KB |
Output is correct |
72 |
Correct |
2028 ms |
72172 KB |
Output is correct |
73 |
Correct |
1682 ms |
70528 KB |
Output is correct |
74 |
Correct |
1685 ms |
71020 KB |
Output is correct |
75 |
Correct |
1687 ms |
70508 KB |
Output is correct |
76 |
Correct |
1687 ms |
88856 KB |
Output is correct |
77 |
Correct |
1680 ms |
71020 KB |
Output is correct |
78 |
Correct |
2063 ms |
79340 KB |
Output is correct |
79 |
Correct |
1277 ms |
61804 KB |
Output is correct |
80 |
Correct |
2209 ms |
84508 KB |
Output is correct |