#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define mk make_pair
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
vector<int> mx[30];
vector<int> mr[30];
vector<int> v[30];
int val;
int n,m;
int level,zereg[30];
int l,r,k;
int a,b;
int ret,tur;
int maximum_val;
bool check;
int go(int MX, int k, int x){
a=zereg[level-k]*x;
b=zereg[level-k]*(x+1)-1;
// mr-iin k dahi level // a -> b
if(MX<=mr[k][a]) return 0;
while(a<b){
int mid=(a+b+1)/2;
if(mr[k][mid]<MX){
a=mid;
}
else b=mid-1;
}
return mr[k][a]+MX;
}
void construct_v(){
for(int i=0; i<zereg[level]; i++){
v[level].pb(0);
}
for(int i=level-1; i>=0; i--){
for(int j=0; j<zereg[i]; j++){
v[i].pb(max(go(mx[i+1][j*2],i+1,j*2+1),max(v[i+1][j*2],v[i+1][j*2+1])));
}
}
}
void dfs(int k, int x){
if(check==0) return;
int y=zereg[level-k]*x;
int z=zereg[level-k]*(x+1)-1;
if(l<=y && z<=r){
if(maximum_val==-1){
maximum_val=mx[k][x];
if(v[k][x]>val) check=0;
return;
}
if(go(maximum_val,k,x)>val || v[k][x]>val){
check=0;
}
maximum_val=max(maximum_val,mx[k][x]);
return;
}
if(z<l || y>r || k==level) return;
dfs(k+1,x*2);
dfs(k+1,x*2+1);
}
int main(){
IOS
cin>>n>>m;
zereg[0]=1;
for(int i=0; i<=30; i++){
if(zereg[i]>=n){
level=i;
break;
}
zereg[i+1]=zereg[i]*2;
}
for(int i=0; i<n; i++){
cin>>val;
mx[level].pb(val);
mr[level].pb(val);
}
for(int i=n; i<zereg[level]; i++){
mx[level].pb(0);
mr[level].pb(1e9+5);
}
for(int i=level-1; i>=0; i--){
for(int j=1; j<mx[i+1].size(); j+=2){
mx[i].pb(max(mx[i+1][j],mx[i+1][j-1]));
}
}
int urt=1;
for(int i=level-1; i>=0; i--){
urt*=2;
for(int j=0; j<zereg[level]; j+=urt){
l=j;
r=j+urt/2;
while(l!=j+urt/2 || r!=j+urt){
if(l==j+urt/2){
mr[i].pb(mr[i+1][r]);
r++;
continue;
}
if(r==j+urt){
mr[i].pb(mr[i+1][l]);
l++;
continue;
}
if(mr[i+1][l]>=mr[i+1][r]){
mr[i].pb(mr[i+1][r]);
r++;
}
else{
mr[i].pb(mr[i+1][l]);
l++;
}
}
}
}
construct_v();
//
while(m--){
cin>>l>>r>>val;
l--;
r--;
maximum_val=-1;
check=1;
dfs(0,0);
cout<<check<<'\n';
}
}
Compilation message
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:92:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for(int j=1; j<mx[i+1].size(); j+=2){
| ~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
3 ms |
452 KB |
Output is correct |
12 |
Correct |
3 ms |
972 KB |
Output is correct |
13 |
Correct |
3 ms |
972 KB |
Output is correct |
14 |
Correct |
6 ms |
972 KB |
Output is correct |
15 |
Correct |
5 ms |
944 KB |
Output is correct |
16 |
Correct |
4 ms |
972 KB |
Output is correct |
17 |
Correct |
3 ms |
588 KB |
Output is correct |
18 |
Correct |
3 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1044 ms |
105040 KB |
Output is correct |
2 |
Correct |
1057 ms |
104988 KB |
Output is correct |
3 |
Correct |
1045 ms |
136180 KB |
Output is correct |
4 |
Correct |
1073 ms |
128376 KB |
Output is correct |
5 |
Correct |
1659 ms |
118748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
108 ms |
11776 KB |
Output is correct |
2 |
Correct |
92 ms |
13804 KB |
Output is correct |
3 |
Correct |
97 ms |
13816 KB |
Output is correct |
4 |
Correct |
107 ms |
13864 KB |
Output is correct |
5 |
Correct |
97 ms |
13824 KB |
Output is correct |
6 |
Correct |
70 ms |
13672 KB |
Output is correct |
7 |
Correct |
75 ms |
13784 KB |
Output is correct |
8 |
Correct |
79 ms |
13432 KB |
Output is correct |
9 |
Correct |
38 ms |
1784 KB |
Output is correct |
10 |
Correct |
75 ms |
13476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
3 ms |
452 KB |
Output is correct |
12 |
Correct |
3 ms |
972 KB |
Output is correct |
13 |
Correct |
3 ms |
972 KB |
Output is correct |
14 |
Correct |
6 ms |
972 KB |
Output is correct |
15 |
Correct |
5 ms |
944 KB |
Output is correct |
16 |
Correct |
4 ms |
972 KB |
Output is correct |
17 |
Correct |
3 ms |
588 KB |
Output is correct |
18 |
Correct |
3 ms |
972 KB |
Output is correct |
19 |
Correct |
249 ms |
31064 KB |
Output is correct |
20 |
Correct |
246 ms |
31008 KB |
Output is correct |
21 |
Correct |
222 ms |
30804 KB |
Output is correct |
22 |
Correct |
225 ms |
30692 KB |
Output is correct |
23 |
Correct |
223 ms |
30848 KB |
Output is correct |
24 |
Correct |
217 ms |
30688 KB |
Output is correct |
25 |
Correct |
212 ms |
30776 KB |
Output is correct |
26 |
Correct |
235 ms |
30876 KB |
Output is correct |
27 |
Correct |
240 ms |
30896 KB |
Output is correct |
28 |
Correct |
259 ms |
30916 KB |
Output is correct |
29 |
Correct |
284 ms |
30884 KB |
Output is correct |
30 |
Correct |
251 ms |
30960 KB |
Output is correct |
31 |
Correct |
289 ms |
30928 KB |
Output is correct |
32 |
Correct |
244 ms |
30972 KB |
Output is correct |
33 |
Correct |
271 ms |
30912 KB |
Output is correct |
34 |
Correct |
189 ms |
30596 KB |
Output is correct |
35 |
Correct |
203 ms |
30588 KB |
Output is correct |
36 |
Correct |
194 ms |
30400 KB |
Output is correct |
37 |
Correct |
182 ms |
30320 KB |
Output is correct |
38 |
Correct |
183 ms |
30532 KB |
Output is correct |
39 |
Correct |
201 ms |
29432 KB |
Output is correct |
40 |
Correct |
153 ms |
16564 KB |
Output is correct |
41 |
Correct |
170 ms |
29152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
3 ms |
452 KB |
Output is correct |
12 |
Correct |
3 ms |
972 KB |
Output is correct |
13 |
Correct |
3 ms |
972 KB |
Output is correct |
14 |
Correct |
6 ms |
972 KB |
Output is correct |
15 |
Correct |
5 ms |
944 KB |
Output is correct |
16 |
Correct |
4 ms |
972 KB |
Output is correct |
17 |
Correct |
3 ms |
588 KB |
Output is correct |
18 |
Correct |
3 ms |
972 KB |
Output is correct |
19 |
Correct |
1044 ms |
105040 KB |
Output is correct |
20 |
Correct |
1057 ms |
104988 KB |
Output is correct |
21 |
Correct |
1045 ms |
136180 KB |
Output is correct |
22 |
Correct |
1073 ms |
128376 KB |
Output is correct |
23 |
Correct |
1659 ms |
118748 KB |
Output is correct |
24 |
Correct |
108 ms |
11776 KB |
Output is correct |
25 |
Correct |
92 ms |
13804 KB |
Output is correct |
26 |
Correct |
97 ms |
13816 KB |
Output is correct |
27 |
Correct |
107 ms |
13864 KB |
Output is correct |
28 |
Correct |
97 ms |
13824 KB |
Output is correct |
29 |
Correct |
70 ms |
13672 KB |
Output is correct |
30 |
Correct |
75 ms |
13784 KB |
Output is correct |
31 |
Correct |
79 ms |
13432 KB |
Output is correct |
32 |
Correct |
38 ms |
1784 KB |
Output is correct |
33 |
Correct |
75 ms |
13476 KB |
Output is correct |
34 |
Correct |
249 ms |
31064 KB |
Output is correct |
35 |
Correct |
246 ms |
31008 KB |
Output is correct |
36 |
Correct |
222 ms |
30804 KB |
Output is correct |
37 |
Correct |
225 ms |
30692 KB |
Output is correct |
38 |
Correct |
223 ms |
30848 KB |
Output is correct |
39 |
Correct |
217 ms |
30688 KB |
Output is correct |
40 |
Correct |
212 ms |
30776 KB |
Output is correct |
41 |
Correct |
235 ms |
30876 KB |
Output is correct |
42 |
Correct |
240 ms |
30896 KB |
Output is correct |
43 |
Correct |
259 ms |
30916 KB |
Output is correct |
44 |
Correct |
284 ms |
30884 KB |
Output is correct |
45 |
Correct |
251 ms |
30960 KB |
Output is correct |
46 |
Correct |
289 ms |
30928 KB |
Output is correct |
47 |
Correct |
244 ms |
30972 KB |
Output is correct |
48 |
Correct |
271 ms |
30912 KB |
Output is correct |
49 |
Correct |
189 ms |
30596 KB |
Output is correct |
50 |
Correct |
203 ms |
30588 KB |
Output is correct |
51 |
Correct |
194 ms |
30400 KB |
Output is correct |
52 |
Correct |
182 ms |
30320 KB |
Output is correct |
53 |
Correct |
183 ms |
30532 KB |
Output is correct |
54 |
Correct |
201 ms |
29432 KB |
Output is correct |
55 |
Correct |
153 ms |
16564 KB |
Output is correct |
56 |
Correct |
170 ms |
29152 KB |
Output is correct |
57 |
Correct |
1473 ms |
138512 KB |
Output is correct |
58 |
Correct |
1413 ms |
138524 KB |
Output is correct |
59 |
Correct |
1432 ms |
138420 KB |
Output is correct |
60 |
Correct |
1418 ms |
138352 KB |
Output is correct |
61 |
Correct |
1400 ms |
138480 KB |
Output is correct |
62 |
Correct |
1434 ms |
138424 KB |
Output is correct |
63 |
Correct |
1108 ms |
136564 KB |
Output is correct |
64 |
Correct |
1125 ms |
136556 KB |
Output is correct |
65 |
Correct |
1518 ms |
138560 KB |
Output is correct |
66 |
Correct |
1623 ms |
138332 KB |
Output is correct |
67 |
Correct |
1536 ms |
118076 KB |
Output is correct |
68 |
Correct |
1600 ms |
122216 KB |
Output is correct |
69 |
Correct |
1577 ms |
138528 KB |
Output is correct |
70 |
Correct |
1601 ms |
138460 KB |
Output is correct |
71 |
Correct |
1601 ms |
131324 KB |
Output is correct |
72 |
Correct |
1614 ms |
138632 KB |
Output is correct |
73 |
Correct |
933 ms |
135080 KB |
Output is correct |
74 |
Correct |
956 ms |
136108 KB |
Output is correct |
75 |
Correct |
919 ms |
135080 KB |
Output is correct |
76 |
Correct |
933 ms |
135028 KB |
Output is correct |
77 |
Correct |
935 ms |
125472 KB |
Output is correct |
78 |
Correct |
1143 ms |
132416 KB |
Output is correct |
79 |
Correct |
951 ms |
74172 KB |
Output is correct |
80 |
Correct |
1056 ms |
129492 KB |
Output is correct |