#include <bits/stdc++.h>
using namespace std;
int n,m,i,j,l,r,ans,N=1024*1024;
int mas[1000001],par[1000001],val[1000001],hey[3000001],tree[3000001];
pair <int,int> p[1000001][20];
vector <int> v[1000001];
void dfs(int u) {
int i;
p[u][0]={par[u],val[u]};
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,max(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 max(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]=max(tree[2*p],tree[2*p+1]);
}
int main() {
ios::sync_with_stdio(0);
cin>>n>>m;
for (i=1;i<=n;i++)
cin>>mas[i];
build_tree(1);
for (i=n;i>0;i--) {
for (;r>0;r--)
if (mas[hey[r]]>=mas[i])
break;
par[i]=hey[r];
v[hey[r]].push_back(i);
hey[++r]=i;
}
for (i=1;i<=n;i++)
if (par[i]>(i+1))
val[i]=mas[i]+solve(i+1+N-1,par[i]-1+N-1,1,N,2*N-1);
for (i=0;i<(v[0].size());i++)
dfs(v[0][i]);
while (cin>>l>>r>>m) {
if (l==r) {
cout<<"1"<<endl;
continue;
}
ans=0;
for (i=19;i>=0;i--)
if (p[l][i].first && p[l][i].first<=r) {
ans=max(ans,p[l][i].second);
l=p[l][i].first;
}
if (r>l)
ans=max(ans,mas[l]+solve(l+1+N-1,r+N-1,1,N,2*N-1));
if (ans>m)
cout<<"0"<<endl;
else
cout<<"1"<<endl;
}
}
Compilation message
sortbooks.cpp: In function 'void dfs(int)':
sortbooks.cpp:13: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:50:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i=0;i<(v[0].size());i++)
~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
28024 KB |
Output is correct |
2 |
Correct |
34 ms |
28024 KB |
Output is correct |
3 |
Correct |
37 ms |
28152 KB |
Output is correct |
4 |
Correct |
36 ms |
28036 KB |
Output is correct |
5 |
Correct |
35 ms |
28104 KB |
Output is correct |
6 |
Correct |
37 ms |
28152 KB |
Output is correct |
7 |
Correct |
37 ms |
28000 KB |
Output is correct |
8 |
Correct |
37 ms |
28024 KB |
Output is correct |
9 |
Correct |
36 ms |
28024 KB |
Output is correct |
10 |
Correct |
37 ms |
28024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
28024 KB |
Output is correct |
2 |
Correct |
34 ms |
28024 KB |
Output is correct |
3 |
Correct |
37 ms |
28152 KB |
Output is correct |
4 |
Correct |
36 ms |
28036 KB |
Output is correct |
5 |
Correct |
35 ms |
28104 KB |
Output is correct |
6 |
Correct |
37 ms |
28152 KB |
Output is correct |
7 |
Correct |
37 ms |
28000 KB |
Output is correct |
8 |
Correct |
37 ms |
28024 KB |
Output is correct |
9 |
Correct |
36 ms |
28024 KB |
Output is correct |
10 |
Correct |
37 ms |
28024 KB |
Output is correct |
11 |
Correct |
45 ms |
28280 KB |
Output is correct |
12 |
Correct |
46 ms |
28948 KB |
Output is correct |
13 |
Correct |
47 ms |
28920 KB |
Output is correct |
14 |
Correct |
54 ms |
28920 KB |
Output is correct |
15 |
Correct |
55 ms |
28920 KB |
Output is correct |
16 |
Correct |
53 ms |
29048 KB |
Output is correct |
17 |
Correct |
52 ms |
28920 KB |
Output is correct |
18 |
Correct |
53 ms |
29048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3055 ms |
223316 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
453 ms |
47168 KB |
Output is correct |
2 |
Correct |
437 ms |
47224 KB |
Output is correct |
3 |
Correct |
499 ms |
51792 KB |
Output is correct |
4 |
Correct |
498 ms |
51764 KB |
Output is correct |
5 |
Correct |
487 ms |
51704 KB |
Output is correct |
6 |
Correct |
420 ms |
51452 KB |
Output is correct |
7 |
Correct |
425 ms |
51464 KB |
Output is correct |
8 |
Correct |
455 ms |
49864 KB |
Output is correct |
9 |
Correct |
337 ms |
28464 KB |
Output is correct |
10 |
Correct |
455 ms |
49912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
28024 KB |
Output is correct |
2 |
Correct |
34 ms |
28024 KB |
Output is correct |
3 |
Correct |
37 ms |
28152 KB |
Output is correct |
4 |
Correct |
36 ms |
28036 KB |
Output is correct |
5 |
Correct |
35 ms |
28104 KB |
Output is correct |
6 |
Correct |
37 ms |
28152 KB |
Output is correct |
7 |
Correct |
37 ms |
28000 KB |
Output is correct |
8 |
Correct |
37 ms |
28024 KB |
Output is correct |
9 |
Correct |
36 ms |
28024 KB |
Output is correct |
10 |
Correct |
37 ms |
28024 KB |
Output is correct |
11 |
Correct |
45 ms |
28280 KB |
Output is correct |
12 |
Correct |
46 ms |
28948 KB |
Output is correct |
13 |
Correct |
47 ms |
28920 KB |
Output is correct |
14 |
Correct |
54 ms |
28920 KB |
Output is correct |
15 |
Correct |
55 ms |
28920 KB |
Output is correct |
16 |
Correct |
53 ms |
29048 KB |
Output is correct |
17 |
Correct |
52 ms |
28920 KB |
Output is correct |
18 |
Correct |
53 ms |
29048 KB |
Output is correct |
19 |
Correct |
922 ms |
66160 KB |
Output is correct |
20 |
Correct |
922 ms |
66212 KB |
Output is correct |
21 |
Correct |
860 ms |
66168 KB |
Output is correct |
22 |
Correct |
858 ms |
66296 KB |
Output is correct |
23 |
Correct |
859 ms |
66028 KB |
Output is correct |
24 |
Correct |
963 ms |
75400 KB |
Output is correct |
25 |
Correct |
988 ms |
75500 KB |
Output is correct |
26 |
Correct |
1008 ms |
75576 KB |
Output is correct |
27 |
Correct |
1002 ms |
75664 KB |
Output is correct |
28 |
Correct |
1024 ms |
75544 KB |
Output is correct |
29 |
Correct |
1024 ms |
75624 KB |
Output is correct |
30 |
Correct |
1032 ms |
75408 KB |
Output is correct |
31 |
Correct |
1066 ms |
75516 KB |
Output is correct |
32 |
Correct |
1030 ms |
75492 KB |
Output is correct |
33 |
Correct |
1078 ms |
75496 KB |
Output is correct |
34 |
Correct |
837 ms |
75396 KB |
Output is correct |
35 |
Correct |
835 ms |
75352 KB |
Output is correct |
36 |
Correct |
853 ms |
75456 KB |
Output is correct |
37 |
Correct |
840 ms |
75412 KB |
Output is correct |
38 |
Correct |
835 ms |
75604 KB |
Output is correct |
39 |
Correct |
946 ms |
72440 KB |
Output is correct |
40 |
Correct |
848 ms |
58140 KB |
Output is correct |
41 |
Correct |
924 ms |
71648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
28024 KB |
Output is correct |
2 |
Correct |
34 ms |
28024 KB |
Output is correct |
3 |
Correct |
37 ms |
28152 KB |
Output is correct |
4 |
Correct |
36 ms |
28036 KB |
Output is correct |
5 |
Correct |
35 ms |
28104 KB |
Output is correct |
6 |
Correct |
37 ms |
28152 KB |
Output is correct |
7 |
Correct |
37 ms |
28000 KB |
Output is correct |
8 |
Correct |
37 ms |
28024 KB |
Output is correct |
9 |
Correct |
36 ms |
28024 KB |
Output is correct |
10 |
Correct |
37 ms |
28024 KB |
Output is correct |
11 |
Correct |
45 ms |
28280 KB |
Output is correct |
12 |
Correct |
46 ms |
28948 KB |
Output is correct |
13 |
Correct |
47 ms |
28920 KB |
Output is correct |
14 |
Correct |
54 ms |
28920 KB |
Output is correct |
15 |
Correct |
55 ms |
28920 KB |
Output is correct |
16 |
Correct |
53 ms |
29048 KB |
Output is correct |
17 |
Correct |
52 ms |
28920 KB |
Output is correct |
18 |
Correct |
53 ms |
29048 KB |
Output is correct |
19 |
Execution timed out |
3055 ms |
223316 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |