#include <bits/stdc++.h>
using namespace std;
int n,m,i,j,l,r,ans;
int mas[2000001],par[2000001],val[2000001],ori[2000001],hey[5000001],pre[2000001][22];
pair <int,int> p[2000001][22];
vector <int> v[2000001];
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 a,int b) {
int c=ori[b-a+1];
return max(pre[a][c],pre[b-(1<<c)+1][c]);
}
int main() {
for (i=1;i<20;i++)
ori[(1<<i)]=i;
for (i=1;i<1000001;i++)
if (!ori[i])
ori[i]=ori[i-1];
cin>>n>>m;
for (i=1;i<=n;i++) {
cin>>mas[i];
pre[i][0]=mas[i];
}
for (j=1;j<20;j++)
for (i=1;(i+(1<<j)-1)<=n;i++)
pre[i][j]=max(pre[i][j-1],pre[i+(1<<(j-1))][j-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,par[i]-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,r));
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:45: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 |
54 ms |
51320 KB |
Output is correct |
2 |
Correct |
59 ms |
51320 KB |
Output is correct |
3 |
Correct |
62 ms |
51448 KB |
Output is correct |
4 |
Correct |
54 ms |
51320 KB |
Output is correct |
5 |
Correct |
54 ms |
51320 KB |
Output is correct |
6 |
Correct |
60 ms |
51328 KB |
Output is correct |
7 |
Correct |
57 ms |
51424 KB |
Output is correct |
8 |
Correct |
56 ms |
51364 KB |
Output is correct |
9 |
Correct |
57 ms |
51320 KB |
Output is correct |
10 |
Correct |
59 ms |
51428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
51320 KB |
Output is correct |
2 |
Correct |
59 ms |
51320 KB |
Output is correct |
3 |
Correct |
62 ms |
51448 KB |
Output is correct |
4 |
Correct |
54 ms |
51320 KB |
Output is correct |
5 |
Correct |
54 ms |
51320 KB |
Output is correct |
6 |
Correct |
60 ms |
51328 KB |
Output is correct |
7 |
Correct |
57 ms |
51424 KB |
Output is correct |
8 |
Correct |
56 ms |
51364 KB |
Output is correct |
9 |
Correct |
57 ms |
51320 KB |
Output is correct |
10 |
Correct |
59 ms |
51428 KB |
Output is correct |
11 |
Correct |
73 ms |
51720 KB |
Output is correct |
12 |
Correct |
76 ms |
52768 KB |
Output is correct |
13 |
Correct |
81 ms |
52740 KB |
Output is correct |
14 |
Correct |
84 ms |
52856 KB |
Output is correct |
15 |
Correct |
90 ms |
52900 KB |
Output is correct |
16 |
Correct |
80 ms |
52888 KB |
Output is correct |
17 |
Correct |
77 ms |
52600 KB |
Output is correct |
18 |
Correct |
100 ms |
52864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1303 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
587 ms |
82144 KB |
Output is correct |
2 |
Correct |
581 ms |
82120 KB |
Output is correct |
3 |
Correct |
685 ms |
86792 KB |
Output is correct |
4 |
Correct |
717 ms |
86732 KB |
Output is correct |
5 |
Correct |
672 ms |
86840 KB |
Output is correct |
6 |
Correct |
602 ms |
86356 KB |
Output is correct |
7 |
Correct |
598 ms |
86256 KB |
Output is correct |
8 |
Correct |
602 ms |
84460 KB |
Output is correct |
9 |
Correct |
450 ms |
53000 KB |
Output is correct |
10 |
Correct |
598 ms |
84616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
51320 KB |
Output is correct |
2 |
Correct |
59 ms |
51320 KB |
Output is correct |
3 |
Correct |
62 ms |
51448 KB |
Output is correct |
4 |
Correct |
54 ms |
51320 KB |
Output is correct |
5 |
Correct |
54 ms |
51320 KB |
Output is correct |
6 |
Correct |
60 ms |
51328 KB |
Output is correct |
7 |
Correct |
57 ms |
51424 KB |
Output is correct |
8 |
Correct |
56 ms |
51364 KB |
Output is correct |
9 |
Correct |
57 ms |
51320 KB |
Output is correct |
10 |
Correct |
59 ms |
51428 KB |
Output is correct |
11 |
Correct |
73 ms |
51720 KB |
Output is correct |
12 |
Correct |
76 ms |
52768 KB |
Output is correct |
13 |
Correct |
81 ms |
52740 KB |
Output is correct |
14 |
Correct |
84 ms |
52856 KB |
Output is correct |
15 |
Correct |
90 ms |
52900 KB |
Output is correct |
16 |
Correct |
80 ms |
52888 KB |
Output is correct |
17 |
Correct |
77 ms |
52600 KB |
Output is correct |
18 |
Correct |
100 ms |
52864 KB |
Output is correct |
19 |
Correct |
1323 ms |
113568 KB |
Output is correct |
20 |
Correct |
1317 ms |
113624 KB |
Output is correct |
21 |
Correct |
1254 ms |
113860 KB |
Output is correct |
22 |
Correct |
1287 ms |
113584 KB |
Output is correct |
23 |
Correct |
1275 ms |
113880 KB |
Output is correct |
24 |
Correct |
1449 ms |
123044 KB |
Output is correct |
25 |
Correct |
1455 ms |
123072 KB |
Output is correct |
26 |
Correct |
1503 ms |
123340 KB |
Output is correct |
27 |
Correct |
1493 ms |
123412 KB |
Output is correct |
28 |
Correct |
1533 ms |
123420 KB |
Output is correct |
29 |
Correct |
1504 ms |
123392 KB |
Output is correct |
30 |
Correct |
1587 ms |
123616 KB |
Output is correct |
31 |
Correct |
1518 ms |
123768 KB |
Output is correct |
32 |
Correct |
1544 ms |
123708 KB |
Output is correct |
33 |
Correct |
1539 ms |
123796 KB |
Output is correct |
34 |
Correct |
1332 ms |
123756 KB |
Output is correct |
35 |
Correct |
1437 ms |
123524 KB |
Output is correct |
36 |
Correct |
1301 ms |
123624 KB |
Output is correct |
37 |
Correct |
1395 ms |
123524 KB |
Output is correct |
38 |
Correct |
1346 ms |
123568 KB |
Output is correct |
39 |
Correct |
1350 ms |
120452 KB |
Output is correct |
40 |
Correct |
1178 ms |
98504 KB |
Output is correct |
41 |
Correct |
1273 ms |
118900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
51320 KB |
Output is correct |
2 |
Correct |
59 ms |
51320 KB |
Output is correct |
3 |
Correct |
62 ms |
51448 KB |
Output is correct |
4 |
Correct |
54 ms |
51320 KB |
Output is correct |
5 |
Correct |
54 ms |
51320 KB |
Output is correct |
6 |
Correct |
60 ms |
51328 KB |
Output is correct |
7 |
Correct |
57 ms |
51424 KB |
Output is correct |
8 |
Correct |
56 ms |
51364 KB |
Output is correct |
9 |
Correct |
57 ms |
51320 KB |
Output is correct |
10 |
Correct |
59 ms |
51428 KB |
Output is correct |
11 |
Correct |
73 ms |
51720 KB |
Output is correct |
12 |
Correct |
76 ms |
52768 KB |
Output is correct |
13 |
Correct |
81 ms |
52740 KB |
Output is correct |
14 |
Correct |
84 ms |
52856 KB |
Output is correct |
15 |
Correct |
90 ms |
52900 KB |
Output is correct |
16 |
Correct |
80 ms |
52888 KB |
Output is correct |
17 |
Correct |
77 ms |
52600 KB |
Output is correct |
18 |
Correct |
100 ms |
52864 KB |
Output is correct |
19 |
Runtime error |
1303 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
20 |
Halted |
0 ms |
0 KB |
- |