#include <bits/stdc++.h>
using namespace std;
int n,m,i,j,l,r,ans,N=1024*1024;
int mas[1000001],val[1000001],tree[2048*1024];
pair <int,int> p[1000001][20];
vector <int> v[1000001];
inline int maxi(int a,int b) {
if (a>b)
return a;
return b;
}
void dfs(int u) {
int i;
for (i=1;i<20;i++)
if (p[p[u][i-1].first][i-1].first)
p[u][i]={p[p[u][i-1].first][i-1].first,maxi(p[u][i-1].second,p[p[u][i-1].first][i-1].second)};
for (i=0;i<(v[u].size());i++)
dfs(v[u][i]);
}
int solve(int x,int y,int p,int l,int r) {
if (l>y || x>r)
return 0;
if (l>=x && r<=y)
return tree[p];
return maxi(solve(x,y,2*p,l,(l+r)/2),solve(x,y,2*p+1,(l+r)/2+1,r));
}
void build_tree(int p) {
if (p>=N) {
if ((p-N+1)<=n)
tree[p]=mas[p-N+1];
return;
}
build_tree(2*p);
build_tree(2*p+1);
tree[p]=maxi(tree[2*p],tree[2*p+1]);
}
int main() {
scanf("%d %d",&n,&m);
for (i=1;i<=n;i++)
scanf("%d",&(mas[i]));
build_tree(1);
for (i=n;i>0;i--) {
for (;r>0;r--)
if (mas[val[r]]>=mas[i])
break;
p[i][0].first=val[r];
if (val[r]>(i+1))
p[i][0].second=mas[i]+solve(i+1+N-1,val[r]-1+N-1,1,N,2*N-1);
v[val[r]].push_back(i);
val[++r]=i;
}
for (i=0;i<(v[0].size());i++)
dfs(v[0][i]);
for (mas[0]=1;mas[0]<=m;mas[0]++) {
scanf("%d %d %d",&l,&r,&j);
ans=0;
for (i=19;i>=0;i--)
if (p[l][i].first && p[l][i].first<=r) {
ans=maxi(ans,p[l][i].second);
l=p[l][i].first;
}
if (r>l)
ans=maxi(ans,mas[l]+solve(l+1+N-1,r+N-1,1,N,2*N-1));
if (ans>j)
printf("0\n");
else
printf("1\n");
}
}
Compilation message
sortbooks.cpp: In function 'void dfs(int)':
sortbooks.cpp:17:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i=0;i<(v[u].size());i++)
~^~~~~~~~~~~~~~
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:52:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i=0;i<(v[0].size());i++)
~^~~~~~~~~~~~~~
sortbooks.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n,&m);
~~~~~^~~~~~~~~~~~~~~
sortbooks.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&(mas[i]));
~~~~~^~~~~~~~~~~~~~~~
sortbooks.cpp:55:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&l,&r,&j);
~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
27896 KB |
Output is correct |
2 |
Correct |
35 ms |
27896 KB |
Output is correct |
3 |
Correct |
35 ms |
28024 KB |
Output is correct |
4 |
Correct |
35 ms |
28024 KB |
Output is correct |
5 |
Correct |
35 ms |
28024 KB |
Output is correct |
6 |
Correct |
36 ms |
28024 KB |
Output is correct |
7 |
Correct |
35 ms |
28024 KB |
Output is correct |
8 |
Correct |
36 ms |
28124 KB |
Output is correct |
9 |
Correct |
35 ms |
28024 KB |
Output is correct |
10 |
Correct |
35 ms |
27996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
27896 KB |
Output is correct |
2 |
Correct |
35 ms |
27896 KB |
Output is correct |
3 |
Correct |
35 ms |
28024 KB |
Output is correct |
4 |
Correct |
35 ms |
28024 KB |
Output is correct |
5 |
Correct |
35 ms |
28024 KB |
Output is correct |
6 |
Correct |
36 ms |
28024 KB |
Output is correct |
7 |
Correct |
35 ms |
28024 KB |
Output is correct |
8 |
Correct |
36 ms |
28124 KB |
Output is correct |
9 |
Correct |
35 ms |
28024 KB |
Output is correct |
10 |
Correct |
35 ms |
27996 KB |
Output is correct |
11 |
Correct |
38 ms |
28280 KB |
Output is correct |
12 |
Correct |
39 ms |
28920 KB |
Output is correct |
13 |
Correct |
40 ms |
28916 KB |
Output is correct |
14 |
Correct |
42 ms |
29052 KB |
Output is correct |
15 |
Correct |
42 ms |
29048 KB |
Output is correct |
16 |
Correct |
40 ms |
29176 KB |
Output is correct |
17 |
Correct |
39 ms |
28920 KB |
Output is correct |
18 |
Correct |
40 ms |
29176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1895 ms |
218040 KB |
Output is correct |
2 |
Correct |
1974 ms |
210760 KB |
Output is correct |
3 |
Correct |
1877 ms |
210560 KB |
Output is correct |
4 |
Correct |
1872 ms |
210864 KB |
Output is correct |
5 |
Correct |
2855 ms |
262144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
187 ms |
48200 KB |
Output is correct |
2 |
Correct |
184 ms |
47524 KB |
Output is correct |
3 |
Correct |
220 ms |
52652 KB |
Output is correct |
4 |
Correct |
229 ms |
52776 KB |
Output is correct |
5 |
Correct |
223 ms |
52600 KB |
Output is correct |
6 |
Correct |
147 ms |
52216 KB |
Output is correct |
7 |
Correct |
148 ms |
52388 KB |
Output is correct |
8 |
Correct |
183 ms |
50552 KB |
Output is correct |
9 |
Correct |
81 ms |
29608 KB |
Output is correct |
10 |
Correct |
184 ms |
50812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
27896 KB |
Output is correct |
2 |
Correct |
35 ms |
27896 KB |
Output is correct |
3 |
Correct |
35 ms |
28024 KB |
Output is correct |
4 |
Correct |
35 ms |
28024 KB |
Output is correct |
5 |
Correct |
35 ms |
28024 KB |
Output is correct |
6 |
Correct |
36 ms |
28024 KB |
Output is correct |
7 |
Correct |
35 ms |
28024 KB |
Output is correct |
8 |
Correct |
36 ms |
28124 KB |
Output is correct |
9 |
Correct |
35 ms |
28024 KB |
Output is correct |
10 |
Correct |
35 ms |
27996 KB |
Output is correct |
11 |
Correct |
38 ms |
28280 KB |
Output is correct |
12 |
Correct |
39 ms |
28920 KB |
Output is correct |
13 |
Correct |
40 ms |
28916 KB |
Output is correct |
14 |
Correct |
42 ms |
29052 KB |
Output is correct |
15 |
Correct |
42 ms |
29048 KB |
Output is correct |
16 |
Correct |
40 ms |
29176 KB |
Output is correct |
17 |
Correct |
39 ms |
28920 KB |
Output is correct |
18 |
Correct |
40 ms |
29176 KB |
Output is correct |
19 |
Correct |
422 ms |
65676 KB |
Output is correct |
20 |
Correct |
396 ms |
65712 KB |
Output is correct |
21 |
Correct |
340 ms |
65896 KB |
Output is correct |
22 |
Correct |
334 ms |
65912 KB |
Output is correct |
23 |
Correct |
342 ms |
65980 KB |
Output is correct |
24 |
Correct |
370 ms |
76008 KB |
Output is correct |
25 |
Correct |
382 ms |
76048 KB |
Output is correct |
26 |
Correct |
436 ms |
76060 KB |
Output is correct |
27 |
Correct |
458 ms |
76096 KB |
Output is correct |
28 |
Correct |
471 ms |
75896 KB |
Output is correct |
29 |
Correct |
482 ms |
76056 KB |
Output is correct |
30 |
Correct |
474 ms |
76024 KB |
Output is correct |
31 |
Correct |
481 ms |
75964 KB |
Output is correct |
32 |
Correct |
482 ms |
75984 KB |
Output is correct |
33 |
Correct |
484 ms |
75916 KB |
Output is correct |
34 |
Correct |
287 ms |
75984 KB |
Output is correct |
35 |
Correct |
312 ms |
75896 KB |
Output is correct |
36 |
Correct |
278 ms |
76024 KB |
Output is correct |
37 |
Correct |
281 ms |
76068 KB |
Output is correct |
38 |
Correct |
283 ms |
75972 KB |
Output is correct |
39 |
Correct |
404 ms |
72680 KB |
Output is correct |
40 |
Correct |
299 ms |
58872 KB |
Output is correct |
41 |
Correct |
372 ms |
72056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
27896 KB |
Output is correct |
2 |
Correct |
35 ms |
27896 KB |
Output is correct |
3 |
Correct |
35 ms |
28024 KB |
Output is correct |
4 |
Correct |
35 ms |
28024 KB |
Output is correct |
5 |
Correct |
35 ms |
28024 KB |
Output is correct |
6 |
Correct |
36 ms |
28024 KB |
Output is correct |
7 |
Correct |
35 ms |
28024 KB |
Output is correct |
8 |
Correct |
36 ms |
28124 KB |
Output is correct |
9 |
Correct |
35 ms |
28024 KB |
Output is correct |
10 |
Correct |
35 ms |
27996 KB |
Output is correct |
11 |
Correct |
38 ms |
28280 KB |
Output is correct |
12 |
Correct |
39 ms |
28920 KB |
Output is correct |
13 |
Correct |
40 ms |
28916 KB |
Output is correct |
14 |
Correct |
42 ms |
29052 KB |
Output is correct |
15 |
Correct |
42 ms |
29048 KB |
Output is correct |
16 |
Correct |
40 ms |
29176 KB |
Output is correct |
17 |
Correct |
39 ms |
28920 KB |
Output is correct |
18 |
Correct |
40 ms |
29176 KB |
Output is correct |
19 |
Correct |
1895 ms |
218040 KB |
Output is correct |
20 |
Correct |
1974 ms |
210760 KB |
Output is correct |
21 |
Correct |
1877 ms |
210560 KB |
Output is correct |
22 |
Correct |
1872 ms |
210864 KB |
Output is correct |
23 |
Correct |
2855 ms |
262144 KB |
Output is correct |
24 |
Correct |
187 ms |
48200 KB |
Output is correct |
25 |
Correct |
184 ms |
47524 KB |
Output is correct |
26 |
Correct |
220 ms |
52652 KB |
Output is correct |
27 |
Correct |
229 ms |
52776 KB |
Output is correct |
28 |
Correct |
223 ms |
52600 KB |
Output is correct |
29 |
Correct |
147 ms |
52216 KB |
Output is correct |
30 |
Correct |
148 ms |
52388 KB |
Output is correct |
31 |
Correct |
183 ms |
50552 KB |
Output is correct |
32 |
Correct |
81 ms |
29608 KB |
Output is correct |
33 |
Correct |
184 ms |
50812 KB |
Output is correct |
34 |
Correct |
422 ms |
65676 KB |
Output is correct |
35 |
Correct |
396 ms |
65712 KB |
Output is correct |
36 |
Correct |
340 ms |
65896 KB |
Output is correct |
37 |
Correct |
334 ms |
65912 KB |
Output is correct |
38 |
Correct |
342 ms |
65980 KB |
Output is correct |
39 |
Correct |
370 ms |
76008 KB |
Output is correct |
40 |
Correct |
382 ms |
76048 KB |
Output is correct |
41 |
Correct |
436 ms |
76060 KB |
Output is correct |
42 |
Correct |
458 ms |
76096 KB |
Output is correct |
43 |
Correct |
471 ms |
75896 KB |
Output is correct |
44 |
Correct |
482 ms |
76056 KB |
Output is correct |
45 |
Correct |
474 ms |
76024 KB |
Output is correct |
46 |
Correct |
481 ms |
75964 KB |
Output is correct |
47 |
Correct |
482 ms |
75984 KB |
Output is correct |
48 |
Correct |
484 ms |
75916 KB |
Output is correct |
49 |
Correct |
287 ms |
75984 KB |
Output is correct |
50 |
Correct |
312 ms |
75896 KB |
Output is correct |
51 |
Correct |
278 ms |
76024 KB |
Output is correct |
52 |
Correct |
281 ms |
76068 KB |
Output is correct |
53 |
Correct |
283 ms |
75972 KB |
Output is correct |
54 |
Correct |
404 ms |
72680 KB |
Output is correct |
55 |
Correct |
299 ms |
58872 KB |
Output is correct |
56 |
Correct |
372 ms |
72056 KB |
Output is correct |
57 |
Correct |
1957 ms |
210800 KB |
Output is correct |
58 |
Correct |
1914 ms |
210788 KB |
Output is correct |
59 |
Correct |
1754 ms |
210776 KB |
Output is correct |
60 |
Correct |
1832 ms |
210628 KB |
Output is correct |
61 |
Correct |
1771 ms |
210848 KB |
Output is correct |
62 |
Correct |
1791 ms |
210668 KB |
Output is correct |
63 |
Correct |
1967 ms |
262144 KB |
Output is correct |
64 |
Correct |
2021 ms |
262144 KB |
Output is correct |
65 |
Correct |
2740 ms |
262144 KB |
Output is correct |
66 |
Correct |
2692 ms |
262144 KB |
Output is correct |
67 |
Correct |
2850 ms |
262144 KB |
Output is correct |
68 |
Correct |
2679 ms |
262144 KB |
Output is correct |
69 |
Correct |
2674 ms |
262144 KB |
Output is correct |
70 |
Correct |
2768 ms |
262144 KB |
Output is correct |
71 |
Correct |
2748 ms |
262144 KB |
Output is correct |
72 |
Correct |
2794 ms |
262144 KB |
Output is correct |
73 |
Correct |
1499 ms |
262144 KB |
Output is correct |
74 |
Correct |
1509 ms |
262144 KB |
Output is correct |
75 |
Correct |
1502 ms |
262144 KB |
Output is correct |
76 |
Correct |
1501 ms |
262144 KB |
Output is correct |
77 |
Correct |
1480 ms |
262144 KB |
Output is correct |
78 |
Correct |
2213 ms |
245228 KB |
Output is correct |
79 |
Correct |
1565 ms |
146576 KB |
Output is correct |
80 |
Correct |
2043 ms |
244564 KB |
Output is correct |