#include"books.h"
#include <vector>
#include<bits/stdc++.h>
const long long maxn=1000000+10;
using namespace std;
long long vis[maxn],visa[maxn],all[maxn],av[maxn],tr[maxn];
long long n;
long long mainres=0,last=0;
vector<pair<long long,pair<long long,long long>>>alle;
struct dsu{
long long par[maxn];
dsu(){
for(long long i=0;i<maxn;i++){
par[i]=i;
}
}
void clear(){
for(long long i=0;i<maxn;i++){
par[i]=i;
}
}
long long root(long long u){
if(u==par[u]){
return u;
}
return par[u]=root(par[u]);
}
long long con(long long u,long long v){
u=root(u);
v=root(v);
if(u==v){
return 0;
}
par[u]=v;
return 1;
}
}ds;
void solve(long long l,long long r,long long cl,long long cr){
if(l!=cl){
l--;
cl=min(cl,av[l]);
return solve(l,r,cl,cr);
}
if(r!=cr){
r++;
cr=max(cr,tr[av[r]]);
return solve(l,r,cl,cr);
}
long long fl=0,fr=0,hl=l-1,hr=r+1,mxl=l,mxr=r;
for(;hr<n;hr++){
if(hr==all[hr]){
continue;
}
if(hr>mxr){
fr+=(hr-mxr)*2;
}
mxr=max(mxr,tr[av[hr]]);
if(av[hr]<l){
break;
}
}
for(;hl>=0;hl--){
if(hl==all[hl]){
continue;
}
if(hl<mxl){
fl+=(mxl-hl)*2;
}
mxl=min(mxl,av[hl]);
if(tr[av[hl]]>r){
break;
}
}
if(hr==n){
mainres+=fr+fl;
return ;
}
if(fl<fr){
mainres+=fl;
return solve(l,r,mxl,cr);
}else{
mainres+=fr;
return solve(l,r,cl,mxr);
}
}
long long minimum_walk(std::vector<int> p,int s){
mainres=0;
n=p.size();
for(long long i=0;i<=n;i++){
vis[i]=visa[i]=0;
all[i]=p[i];
}
last=0;
long long cnt1=0;
for(long long i=0;i<n;i++){
if(p[i]>i){
cnt1++;
visa[p[i]]=1;
}
if(visa[i]){
cnt1--;
}
mainres+=cnt1*2;
if(cnt1!=0){
last=i+1;
}
}
for(long long i=0;i<n;i++){
if(i==p[i]){
continue;
}
if(vis[i]==0){
//mainres+=2;
long long now=i;
do{
av[now]=i;
vis[now]=1;
tr[i]=max(tr[i],now);
now=p[now];
}while(now!=i);
}
}
last=-1;
alle.clear();
solve(s,s,av[s],tr[av[s]]);
return mainres;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8284 KB |
Output is correct |
2 |
Correct |
4 ms |
8428 KB |
Output is correct |
3 |
Correct |
5 ms |
8284 KB |
Output is correct |
4 |
Correct |
5 ms |
8284 KB |
Output is correct |
5 |
Correct |
3 ms |
8284 KB |
Output is correct |
6 |
Correct |
4 ms |
8284 KB |
Output is correct |
7 |
Correct |
5 ms |
8232 KB |
Output is correct |
8 |
Correct |
4 ms |
8272 KB |
Output is correct |
9 |
Correct |
4 ms |
8284 KB |
Output is correct |
10 |
Correct |
5 ms |
8152 KB |
Output is correct |
11 |
Correct |
3 ms |
8284 KB |
Output is correct |
12 |
Correct |
3 ms |
8280 KB |
Output is correct |
13 |
Correct |
3 ms |
8284 KB |
Output is correct |
14 |
Correct |
4 ms |
8280 KB |
Output is correct |
15 |
Correct |
5 ms |
8280 KB |
Output is correct |
16 |
Correct |
3 ms |
8284 KB |
Output is correct |
17 |
Correct |
6 ms |
8284 KB |
Output is correct |
18 |
Correct |
4 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8284 KB |
Output is correct |
2 |
Correct |
4 ms |
8428 KB |
Output is correct |
3 |
Correct |
5 ms |
8284 KB |
Output is correct |
4 |
Correct |
5 ms |
8284 KB |
Output is correct |
5 |
Correct |
3 ms |
8284 KB |
Output is correct |
6 |
Correct |
4 ms |
8284 KB |
Output is correct |
7 |
Correct |
5 ms |
8232 KB |
Output is correct |
8 |
Correct |
4 ms |
8272 KB |
Output is correct |
9 |
Correct |
4 ms |
8284 KB |
Output is correct |
10 |
Correct |
5 ms |
8152 KB |
Output is correct |
11 |
Correct |
3 ms |
8284 KB |
Output is correct |
12 |
Correct |
3 ms |
8280 KB |
Output is correct |
13 |
Correct |
3 ms |
8284 KB |
Output is correct |
14 |
Correct |
4 ms |
8280 KB |
Output is correct |
15 |
Correct |
5 ms |
8280 KB |
Output is correct |
16 |
Correct |
3 ms |
8284 KB |
Output is correct |
17 |
Correct |
6 ms |
8284 KB |
Output is correct |
18 |
Correct |
4 ms |
8284 KB |
Output is correct |
19 |
Correct |
3 ms |
8280 KB |
Output is correct |
20 |
Correct |
3 ms |
8140 KB |
Output is correct |
21 |
Correct |
5 ms |
8272 KB |
Output is correct |
22 |
Correct |
5 ms |
8284 KB |
Output is correct |
23 |
Correct |
4 ms |
8284 KB |
Output is correct |
24 |
Correct |
5 ms |
8284 KB |
Output is correct |
25 |
Correct |
4 ms |
8284 KB |
Output is correct |
26 |
Correct |
5 ms |
8312 KB |
Output is correct |
27 |
Correct |
4 ms |
8284 KB |
Output is correct |
28 |
Correct |
3 ms |
8284 KB |
Output is correct |
29 |
Correct |
3 ms |
8172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8284 KB |
Output is correct |
2 |
Correct |
4 ms |
8428 KB |
Output is correct |
3 |
Correct |
5 ms |
8284 KB |
Output is correct |
4 |
Correct |
5 ms |
8284 KB |
Output is correct |
5 |
Correct |
3 ms |
8284 KB |
Output is correct |
6 |
Correct |
4 ms |
8284 KB |
Output is correct |
7 |
Correct |
5 ms |
8232 KB |
Output is correct |
8 |
Correct |
4 ms |
8272 KB |
Output is correct |
9 |
Correct |
4 ms |
8284 KB |
Output is correct |
10 |
Correct |
5 ms |
8152 KB |
Output is correct |
11 |
Correct |
3 ms |
8284 KB |
Output is correct |
12 |
Correct |
3 ms |
8280 KB |
Output is correct |
13 |
Correct |
3 ms |
8284 KB |
Output is correct |
14 |
Correct |
4 ms |
8280 KB |
Output is correct |
15 |
Correct |
5 ms |
8280 KB |
Output is correct |
16 |
Correct |
3 ms |
8284 KB |
Output is correct |
17 |
Correct |
6 ms |
8284 KB |
Output is correct |
18 |
Correct |
4 ms |
8284 KB |
Output is correct |
19 |
Correct |
3 ms |
8280 KB |
Output is correct |
20 |
Correct |
3 ms |
8140 KB |
Output is correct |
21 |
Correct |
5 ms |
8272 KB |
Output is correct |
22 |
Correct |
5 ms |
8284 KB |
Output is correct |
23 |
Correct |
4 ms |
8284 KB |
Output is correct |
24 |
Correct |
5 ms |
8284 KB |
Output is correct |
25 |
Correct |
4 ms |
8284 KB |
Output is correct |
26 |
Correct |
5 ms |
8312 KB |
Output is correct |
27 |
Correct |
4 ms |
8284 KB |
Output is correct |
28 |
Correct |
3 ms |
8284 KB |
Output is correct |
29 |
Correct |
3 ms |
8172 KB |
Output is correct |
30 |
Correct |
129 ms |
54100 KB |
Output is correct |
31 |
Correct |
152 ms |
54012 KB |
Output is correct |
32 |
Correct |
91 ms |
46144 KB |
Output is correct |
33 |
Correct |
123 ms |
61940 KB |
Output is correct |
34 |
Correct |
107 ms |
61780 KB |
Output is correct |
35 |
Correct |
112 ms |
61780 KB |
Output is correct |
36 |
Correct |
163 ms |
61780 KB |
Output is correct |
37 |
Correct |
122 ms |
61836 KB |
Output is correct |
38 |
Correct |
100 ms |
58760 KB |
Output is correct |
39 |
Correct |
102 ms |
58108 KB |
Output is correct |
40 |
Correct |
100 ms |
55032 KB |
Output is correct |
41 |
Correct |
108 ms |
54140 KB |
Output is correct |
42 |
Correct |
114 ms |
54356 KB |
Output is correct |
43 |
Correct |
122 ms |
61820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
8280 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4070' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8284 KB |
Output is correct |
2 |
Correct |
4 ms |
8428 KB |
Output is correct |
3 |
Correct |
5 ms |
8284 KB |
Output is correct |
4 |
Correct |
5 ms |
8284 KB |
Output is correct |
5 |
Correct |
3 ms |
8284 KB |
Output is correct |
6 |
Correct |
4 ms |
8284 KB |
Output is correct |
7 |
Correct |
5 ms |
8232 KB |
Output is correct |
8 |
Correct |
4 ms |
8272 KB |
Output is correct |
9 |
Correct |
4 ms |
8284 KB |
Output is correct |
10 |
Correct |
5 ms |
8152 KB |
Output is correct |
11 |
Correct |
3 ms |
8284 KB |
Output is correct |
12 |
Correct |
3 ms |
8280 KB |
Output is correct |
13 |
Correct |
3 ms |
8284 KB |
Output is correct |
14 |
Correct |
4 ms |
8280 KB |
Output is correct |
15 |
Correct |
5 ms |
8280 KB |
Output is correct |
16 |
Correct |
3 ms |
8284 KB |
Output is correct |
17 |
Correct |
6 ms |
8284 KB |
Output is correct |
18 |
Correct |
4 ms |
8284 KB |
Output is correct |
19 |
Correct |
3 ms |
8280 KB |
Output is correct |
20 |
Correct |
3 ms |
8140 KB |
Output is correct |
21 |
Correct |
5 ms |
8272 KB |
Output is correct |
22 |
Correct |
5 ms |
8284 KB |
Output is correct |
23 |
Correct |
4 ms |
8284 KB |
Output is correct |
24 |
Correct |
5 ms |
8284 KB |
Output is correct |
25 |
Correct |
4 ms |
8284 KB |
Output is correct |
26 |
Correct |
5 ms |
8312 KB |
Output is correct |
27 |
Correct |
4 ms |
8284 KB |
Output is correct |
28 |
Correct |
3 ms |
8284 KB |
Output is correct |
29 |
Correct |
3 ms |
8172 KB |
Output is correct |
30 |
Correct |
129 ms |
54100 KB |
Output is correct |
31 |
Correct |
152 ms |
54012 KB |
Output is correct |
32 |
Correct |
91 ms |
46144 KB |
Output is correct |
33 |
Correct |
123 ms |
61940 KB |
Output is correct |
34 |
Correct |
107 ms |
61780 KB |
Output is correct |
35 |
Correct |
112 ms |
61780 KB |
Output is correct |
36 |
Correct |
163 ms |
61780 KB |
Output is correct |
37 |
Correct |
122 ms |
61836 KB |
Output is correct |
38 |
Correct |
100 ms |
58760 KB |
Output is correct |
39 |
Correct |
102 ms |
58108 KB |
Output is correct |
40 |
Correct |
100 ms |
55032 KB |
Output is correct |
41 |
Correct |
108 ms |
54140 KB |
Output is correct |
42 |
Correct |
114 ms |
54356 KB |
Output is correct |
43 |
Correct |
122 ms |
61820 KB |
Output is correct |
44 |
Incorrect |
4 ms |
8280 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4070' |
45 |
Halted |
0 ms |
0 KB |
- |