#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
#define ll long long
#define int long long
#define left (node<<1),l,((l+r)>>1)
#define right ((node<<1)|1),((l+r)>>1) + 1,r
using namespace std;
const int N = 1000005;
int a[N],sgt[4*N],ans[N],add[4*N],pref[4*N],t[4*N],n,q;
deque <pair<int,int> > dq;
vector <pair<pair<int,int>,int> > v[N];
void Build(int node,int l,int r){
if (l == r) {t[node] = l; return;}
Build(left),Build(right);
int x = t[(node<<1)],y = t[((node<<1)|1)];
if (a[x] > a[y]) t[node] = x;
else t[node] = y;
}
int GET(int node,int l,int r,int L,int R){
if (r < L || R < l) return 0;
if (L <= l && r <= R) return t[node];
int x = GET(left,L,R),y = GET(right,L,R);
if (a[x] > a[y]) return x;
return y;
}
void Push(int node,int l,int r){
if (add[node] == 0) return;
if (l!=r){
add[(node<<1)] = add[((node<<1)|1)] = add[node];
int mid = ((l+r)>>1);
sgt[(node<<1)] = add[node] + t[(node<<1)];
sgt[((node<<1)|1)] = add[node] + t[((node<<1)|1)];
}
add[node]=0;
}
void upd(int node,int l,int r,int L,int R,int val){
if (r < L || R < l) return ;
if (L <= l && r <= R) {
sgt[node] = val + t[node];
add[node] = val;
return;
}
Push(node,l,r);
upd(left,L,R,val),upd(right,L,R,val);
sgt[node] = max(sgt[(node<<1)],sgt[((node<<1)|1)]);
}
ll get(int node,int l,int r,int L,int R){
if (r < L || R < l) return 0;
if (L <= l && r <= R) return sgt[node];
Push(node,l,r);
ll x = get(left,L,R),y=get(right,L,R);
return max(x,y);
}
main (){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
cin>>n>>q;
for (int i=1;i<=n;i++)
cin>>a[i];
a[0] = 0;
Build(1,1,n);
for (int i=1;i<=4*N;i++)
t[i] = a[t[i]];
for (int i=1;i<=q;i++){
int l,r,k;
cin>>l>>r>>k;
v[l].pb({{r,k},i});
}
dq.push_back({5e9,n + 1});
for (int i=n;i>=1;i--){
if (i == n) dq.push_back({a[n],n});
else{
while(dq.back().f < a[i]) dq.pop_back();
if (i + 1 <= dq.back().s - 1)
upd(1,1,n,i + 1,dq.back().s - 1,a[i]);
dq.push_back({a[i],i});
}
for (int j=0;j<v[i].size();j++){
int r = v[i][j].f.f,k = v[i][j].f.s,ind = v[i][j].s;
ll x = get(1,1,n,i,r);
if (x > k) ans[ind] = 0;
else ans[ind] = 1;
}
}
for (int i=1;i<=q;i++) cout<<ans[i]<<"\n";
}
Compilation message
sortbooks.cpp: In function 'void Push(long long int, long long int, long long int)':
sortbooks.cpp:33:13: warning: unused variable 'mid' [-Wunused-variable]
33 | int mid = ((l+r)>>1);
| ^~~
sortbooks.cpp: At global scope:
sortbooks.cpp:57:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
57 | main (){
| ^
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:87:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int j=0;j<v[i].size();j++){
| ~^~~~~~~~~~~~
sortbooks.cpp:68:21: warning: iteration 4000019 invokes undefined behavior [-Waggressive-loop-optimizations]
68 | t[i] = a[t[i]];
| ~~~^
sortbooks.cpp:67:19: note: within this loop
67 | for (int i=1;i<=4*N;i++)
| ~^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
55108 KB |
Output is correct |
2 |
Correct |
37 ms |
55044 KB |
Output is correct |
3 |
Correct |
36 ms |
55160 KB |
Output is correct |
4 |
Correct |
37 ms |
55068 KB |
Output is correct |
5 |
Correct |
35 ms |
55116 KB |
Output is correct |
6 |
Correct |
38 ms |
55116 KB |
Output is correct |
7 |
Correct |
43 ms |
55100 KB |
Output is correct |
8 |
Correct |
37 ms |
55092 KB |
Output is correct |
9 |
Correct |
37 ms |
55108 KB |
Output is correct |
10 |
Correct |
38 ms |
55108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
55108 KB |
Output is correct |
2 |
Correct |
37 ms |
55044 KB |
Output is correct |
3 |
Correct |
36 ms |
55160 KB |
Output is correct |
4 |
Correct |
37 ms |
55068 KB |
Output is correct |
5 |
Correct |
35 ms |
55116 KB |
Output is correct |
6 |
Correct |
38 ms |
55116 KB |
Output is correct |
7 |
Correct |
43 ms |
55100 KB |
Output is correct |
8 |
Correct |
37 ms |
55092 KB |
Output is correct |
9 |
Correct |
37 ms |
55108 KB |
Output is correct |
10 |
Correct |
38 ms |
55108 KB |
Output is correct |
11 |
Correct |
40 ms |
55308 KB |
Output is correct |
12 |
Correct |
39 ms |
55468 KB |
Output is correct |
13 |
Correct |
40 ms |
55560 KB |
Output is correct |
14 |
Correct |
42 ms |
55612 KB |
Output is correct |
15 |
Correct |
48 ms |
55620 KB |
Output is correct |
16 |
Correct |
40 ms |
55516 KB |
Output is correct |
17 |
Correct |
40 ms |
55444 KB |
Output is correct |
18 |
Correct |
40 ms |
55592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1975 ms |
138012 KB |
Output is correct |
2 |
Correct |
2003 ms |
138052 KB |
Output is correct |
3 |
Correct |
1938 ms |
138012 KB |
Output is correct |
4 |
Correct |
1969 ms |
138084 KB |
Output is correct |
5 |
Correct |
1575 ms |
121816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
179 ms |
64212 KB |
Output is correct |
2 |
Correct |
148 ms |
64648 KB |
Output is correct |
3 |
Correct |
139 ms |
61736 KB |
Output is correct |
4 |
Correct |
156 ms |
61792 KB |
Output is correct |
5 |
Correct |
138 ms |
61636 KB |
Output is correct |
6 |
Correct |
117 ms |
61492 KB |
Output is correct |
7 |
Correct |
118 ms |
61472 KB |
Output is correct |
8 |
Correct |
128 ms |
63516 KB |
Output is correct |
9 |
Correct |
95 ms |
59032 KB |
Output is correct |
10 |
Correct |
130 ms |
63040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
55108 KB |
Output is correct |
2 |
Correct |
37 ms |
55044 KB |
Output is correct |
3 |
Correct |
36 ms |
55160 KB |
Output is correct |
4 |
Correct |
37 ms |
55068 KB |
Output is correct |
5 |
Correct |
35 ms |
55116 KB |
Output is correct |
6 |
Correct |
38 ms |
55116 KB |
Output is correct |
7 |
Correct |
43 ms |
55100 KB |
Output is correct |
8 |
Correct |
37 ms |
55092 KB |
Output is correct |
9 |
Correct |
37 ms |
55108 KB |
Output is correct |
10 |
Correct |
38 ms |
55108 KB |
Output is correct |
11 |
Correct |
40 ms |
55308 KB |
Output is correct |
12 |
Correct |
39 ms |
55468 KB |
Output is correct |
13 |
Correct |
40 ms |
55560 KB |
Output is correct |
14 |
Correct |
42 ms |
55612 KB |
Output is correct |
15 |
Correct |
48 ms |
55620 KB |
Output is correct |
16 |
Correct |
40 ms |
55516 KB |
Output is correct |
17 |
Correct |
40 ms |
55444 KB |
Output is correct |
18 |
Correct |
40 ms |
55592 KB |
Output is correct |
19 |
Correct |
340 ms |
73300 KB |
Output is correct |
20 |
Correct |
379 ms |
73196 KB |
Output is correct |
21 |
Correct |
283 ms |
74404 KB |
Output is correct |
22 |
Correct |
287 ms |
74324 KB |
Output is correct |
23 |
Correct |
284 ms |
74356 KB |
Output is correct |
24 |
Correct |
250 ms |
69544 KB |
Output is correct |
25 |
Correct |
242 ms |
69444 KB |
Output is correct |
26 |
Correct |
315 ms |
69060 KB |
Output is correct |
27 |
Correct |
267 ms |
69192 KB |
Output is correct |
28 |
Correct |
272 ms |
68768 KB |
Output is correct |
29 |
Correct |
295 ms |
68488 KB |
Output is correct |
30 |
Correct |
281 ms |
68292 KB |
Output is correct |
31 |
Correct |
285 ms |
68364 KB |
Output is correct |
32 |
Correct |
290 ms |
68472 KB |
Output is correct |
33 |
Correct |
287 ms |
68408 KB |
Output is correct |
34 |
Correct |
234 ms |
68064 KB |
Output is correct |
35 |
Correct |
223 ms |
68036 KB |
Output is correct |
36 |
Correct |
218 ms |
68036 KB |
Output is correct |
37 |
Correct |
220 ms |
68164 KB |
Output is correct |
38 |
Correct |
221 ms |
68036 KB |
Output is correct |
39 |
Correct |
250 ms |
69840 KB |
Output is correct |
40 |
Correct |
198 ms |
66452 KB |
Output is correct |
41 |
Correct |
239 ms |
70648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
55108 KB |
Output is correct |
2 |
Correct |
37 ms |
55044 KB |
Output is correct |
3 |
Correct |
36 ms |
55160 KB |
Output is correct |
4 |
Correct |
37 ms |
55068 KB |
Output is correct |
5 |
Correct |
35 ms |
55116 KB |
Output is correct |
6 |
Correct |
38 ms |
55116 KB |
Output is correct |
7 |
Correct |
43 ms |
55100 KB |
Output is correct |
8 |
Correct |
37 ms |
55092 KB |
Output is correct |
9 |
Correct |
37 ms |
55108 KB |
Output is correct |
10 |
Correct |
38 ms |
55108 KB |
Output is correct |
11 |
Correct |
40 ms |
55308 KB |
Output is correct |
12 |
Correct |
39 ms |
55468 KB |
Output is correct |
13 |
Correct |
40 ms |
55560 KB |
Output is correct |
14 |
Correct |
42 ms |
55612 KB |
Output is correct |
15 |
Correct |
48 ms |
55620 KB |
Output is correct |
16 |
Correct |
40 ms |
55516 KB |
Output is correct |
17 |
Correct |
40 ms |
55444 KB |
Output is correct |
18 |
Correct |
40 ms |
55592 KB |
Output is correct |
19 |
Correct |
1975 ms |
138012 KB |
Output is correct |
20 |
Correct |
2003 ms |
138052 KB |
Output is correct |
21 |
Correct |
1938 ms |
138012 KB |
Output is correct |
22 |
Correct |
1969 ms |
138084 KB |
Output is correct |
23 |
Correct |
1575 ms |
121816 KB |
Output is correct |
24 |
Correct |
179 ms |
64212 KB |
Output is correct |
25 |
Correct |
148 ms |
64648 KB |
Output is correct |
26 |
Correct |
139 ms |
61736 KB |
Output is correct |
27 |
Correct |
156 ms |
61792 KB |
Output is correct |
28 |
Correct |
138 ms |
61636 KB |
Output is correct |
29 |
Correct |
117 ms |
61492 KB |
Output is correct |
30 |
Correct |
118 ms |
61472 KB |
Output is correct |
31 |
Correct |
128 ms |
63516 KB |
Output is correct |
32 |
Correct |
95 ms |
59032 KB |
Output is correct |
33 |
Correct |
130 ms |
63040 KB |
Output is correct |
34 |
Correct |
340 ms |
73300 KB |
Output is correct |
35 |
Correct |
379 ms |
73196 KB |
Output is correct |
36 |
Correct |
283 ms |
74404 KB |
Output is correct |
37 |
Correct |
287 ms |
74324 KB |
Output is correct |
38 |
Correct |
284 ms |
74356 KB |
Output is correct |
39 |
Correct |
250 ms |
69544 KB |
Output is correct |
40 |
Correct |
242 ms |
69444 KB |
Output is correct |
41 |
Correct |
315 ms |
69060 KB |
Output is correct |
42 |
Correct |
267 ms |
69192 KB |
Output is correct |
43 |
Correct |
272 ms |
68768 KB |
Output is correct |
44 |
Correct |
295 ms |
68488 KB |
Output is correct |
45 |
Correct |
281 ms |
68292 KB |
Output is correct |
46 |
Correct |
285 ms |
68364 KB |
Output is correct |
47 |
Correct |
290 ms |
68472 KB |
Output is correct |
48 |
Correct |
287 ms |
68408 KB |
Output is correct |
49 |
Correct |
234 ms |
68064 KB |
Output is correct |
50 |
Correct |
223 ms |
68036 KB |
Output is correct |
51 |
Correct |
218 ms |
68036 KB |
Output is correct |
52 |
Correct |
220 ms |
68164 KB |
Output is correct |
53 |
Correct |
221 ms |
68036 KB |
Output is correct |
54 |
Correct |
250 ms |
69840 KB |
Output is correct |
55 |
Correct |
198 ms |
66452 KB |
Output is correct |
56 |
Correct |
239 ms |
70648 KB |
Output is correct |
57 |
Correct |
1971 ms |
171488 KB |
Output is correct |
58 |
Correct |
1999 ms |
171512 KB |
Output is correct |
59 |
Correct |
1851 ms |
173048 KB |
Output is correct |
60 |
Correct |
1836 ms |
173224 KB |
Output is correct |
61 |
Correct |
1841 ms |
173036 KB |
Output is correct |
62 |
Correct |
1824 ms |
173084 KB |
Output is correct |
63 |
Correct |
1177 ms |
151928 KB |
Output is correct |
64 |
Correct |
1170 ms |
151672 KB |
Output is correct |
65 |
Correct |
1524 ms |
157272 KB |
Output is correct |
66 |
Correct |
1517 ms |
157132 KB |
Output is correct |
67 |
Correct |
1519 ms |
156908 KB |
Output is correct |
68 |
Correct |
1562 ms |
155216 KB |
Output is correct |
69 |
Correct |
1570 ms |
155176 KB |
Output is correct |
70 |
Correct |
1607 ms |
155132 KB |
Output is correct |
71 |
Correct |
1570 ms |
155068 KB |
Output is correct |
72 |
Correct |
1582 ms |
155184 KB |
Output is correct |
73 |
Correct |
1008 ms |
145956 KB |
Output is correct |
74 |
Correct |
1044 ms |
146572 KB |
Output is correct |
75 |
Correct |
1018 ms |
146108 KB |
Output is correct |
76 |
Correct |
1012 ms |
145640 KB |
Output is correct |
77 |
Correct |
1029 ms |
145976 KB |
Output is correct |
78 |
Correct |
1345 ms |
152860 KB |
Output is correct |
79 |
Correct |
922 ms |
130396 KB |
Output is correct |
80 |
Correct |
1341 ms |
158632 KB |
Output is correct |