#include <bits/stdc++.h>
#include "books.h"
using namespace std;
typedef long long int ll;
typedef complex<double> point;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
//#define endl '\n'
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout<<x<<'\n', 0;
const int N=1e6+10,inf=(ll)1e9;
ll vis[N];
pii seg[N*4];
pii ba[N];
ll E[N];
void upd(ll nod,ll l,ll r,ll id,pii val){
ba[id]=val;
if (r-l==1){
seg[nod]=val;
return ;
}
ll mid=(r+l)/2;
if (mid>id) upd(nod*2,l,mid,id,val);
else upd(nod*2+1,mid,r,id,val);
seg[nod].F=min(seg[nod*2].F,seg[nod*2+1].F);
seg[nod].S=max(seg[nod*2].S,seg[nod*2+1].S);
}
pii get(ll nod,ll l,ll r,ll L,ll R){
if (l>=R || L>=r) return {inf,0};
if (l>=L && r<=R) return seg[nod];
ll mid=(r+l)/2;
pii a=get(nod*2,l,mid,L,R);
pii b=get(nod*2+1,mid,r,L,R);
return {min(a.F,b.F),max(a.S,b.S)};
}
long long minimum_walk(std::vector<int32_t> p, int32_t s) {
memset(vis,0,sizeof vis);
vector <pii> baz;
ll n=p.size();
ll ans=0;
for (int i=0;i<n;i++){
// cout << i << " rf" << endl;
if (vis[i]) continue;
if (p[i]==i){
upd(1,0,n,i,{i,i});
continue;
}
ll mn=i,mx=i;
ll j=p[i];
ans+=abs(p[i]-i);
while(j!=i){
ans+=abs(p[j]-j);
// cout << j << " " << p[j] << endl;
mn=min(mn,j);
mx=max(mx,j);
vis[j]=1;
j=p[j];
}
j=p[i];
while(j!=i){
upd(1,0,n,j,{mn,mx});
j=p[j];
}
upd(1,0,n,i,{mn,mx});
vis[i]=1;
baz.pb({mn,-mx});
}
// cout << ans << endl;
sort(baz.begin(),baz.end());
vector <pii> a;
if (baz.size()){
a.pb(baz[0]);
for (int i=1;i<baz.size();i++){
pii p=baz[i],q=a.back();
if (-p.S<=-q.S) continue;
if (p.F<=-q.S){
a.pop_back();
q.S=p.S;
a.pb(q);
}
else
a.pb(baz[i]);
}
for (auto u : a){
for (int i=u.F;i<=-u.S;i++){
E[i]++;
}
}
ll p1=0;
if (a[0].F>s){
ans+=(a[0].F-s)*2;
p1=1;
}
if (-a.back().S<s){
ans+=(s+a.back().S)*2;
p1=1;
}
if (p1==0 && E[s]!=0){
pii b={s,s};
while(true){
while(true){
pii z=get(1,0,n,b.F,b.S+1);
if (z!=b) b=z;
else break;
}
ll z=lower_bound(a.begin(),a.end(),pii({b.F,-inf}))-a.begin();
// cout << b.F << " dc " << b.S << endl;
if (z==a.size() || a[z].F!=b.F){
b.F--;
b.S++;
ans+=2;
}
else break;
}
}
for (int i=1;i<a.size();i++){
ans+=(a[i].F+a[i-1].S)*2;
}
}
return ans;
}
/*
int32_t main() {
int32_t n, s;
assert(2 == scanf("%d %d", &n, &s));
vector<int32_t> p((unsigned) n);
for(int i = 0; i < n; i++)
assert(1 == scanf("%d", &p[(unsigned) i]));
long long res = minimum_walk(p, s);
printf("%lld\n", res);
return 0;
}
*/
/*
4 0
0 2 3 1
4 2
1 3 2 0
*/
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int32_t)':
books.cpp:79:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for (int i=1;i<baz.size();i++){
| ~^~~~~~~~~~~
books.cpp:115:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | if (z==a.size() || a[z].F!=b.F){
| ~^~~~~~~~~~
books.cpp:123:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | for (int i=1;i<a.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8140 KB |
Output is correct |
2 |
Correct |
5 ms |
8140 KB |
Output is correct |
3 |
Correct |
4 ms |
8140 KB |
Output is correct |
4 |
Correct |
4 ms |
8140 KB |
Output is correct |
5 |
Correct |
5 ms |
8140 KB |
Output is correct |
6 |
Correct |
5 ms |
8032 KB |
Output is correct |
7 |
Correct |
4 ms |
8140 KB |
Output is correct |
8 |
Correct |
4 ms |
8140 KB |
Output is correct |
9 |
Correct |
6 ms |
8144 KB |
Output is correct |
10 |
Correct |
4 ms |
8140 KB |
Output is correct |
11 |
Correct |
5 ms |
8140 KB |
Output is correct |
12 |
Correct |
5 ms |
8140 KB |
Output is correct |
13 |
Correct |
4 ms |
8140 KB |
Output is correct |
14 |
Correct |
4 ms |
8140 KB |
Output is correct |
15 |
Correct |
4 ms |
8088 KB |
Output is correct |
16 |
Correct |
5 ms |
8140 KB |
Output is correct |
17 |
Correct |
5 ms |
8140 KB |
Output is correct |
18 |
Correct |
5 ms |
8140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8140 KB |
Output is correct |
2 |
Correct |
5 ms |
8140 KB |
Output is correct |
3 |
Correct |
4 ms |
8140 KB |
Output is correct |
4 |
Correct |
4 ms |
8140 KB |
Output is correct |
5 |
Correct |
5 ms |
8140 KB |
Output is correct |
6 |
Correct |
5 ms |
8032 KB |
Output is correct |
7 |
Correct |
4 ms |
8140 KB |
Output is correct |
8 |
Correct |
4 ms |
8140 KB |
Output is correct |
9 |
Correct |
6 ms |
8144 KB |
Output is correct |
10 |
Correct |
4 ms |
8140 KB |
Output is correct |
11 |
Correct |
5 ms |
8140 KB |
Output is correct |
12 |
Correct |
5 ms |
8140 KB |
Output is correct |
13 |
Correct |
4 ms |
8140 KB |
Output is correct |
14 |
Correct |
4 ms |
8140 KB |
Output is correct |
15 |
Correct |
4 ms |
8088 KB |
Output is correct |
16 |
Correct |
5 ms |
8140 KB |
Output is correct |
17 |
Correct |
5 ms |
8140 KB |
Output is correct |
18 |
Correct |
5 ms |
8140 KB |
Output is correct |
19 |
Correct |
5 ms |
8140 KB |
Output is correct |
20 |
Correct |
5 ms |
8140 KB |
Output is correct |
21 |
Correct |
5 ms |
8140 KB |
Output is correct |
22 |
Correct |
5 ms |
8140 KB |
Output is correct |
23 |
Correct |
5 ms |
8140 KB |
Output is correct |
24 |
Correct |
5 ms |
8140 KB |
Output is correct |
25 |
Correct |
5 ms |
8140 KB |
Output is correct |
26 |
Correct |
5 ms |
8140 KB |
Output is correct |
27 |
Correct |
5 ms |
8140 KB |
Output is correct |
28 |
Correct |
5 ms |
8140 KB |
Output is correct |
29 |
Correct |
5 ms |
8140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8140 KB |
Output is correct |
2 |
Correct |
5 ms |
8140 KB |
Output is correct |
3 |
Correct |
4 ms |
8140 KB |
Output is correct |
4 |
Correct |
4 ms |
8140 KB |
Output is correct |
5 |
Correct |
5 ms |
8140 KB |
Output is correct |
6 |
Correct |
5 ms |
8032 KB |
Output is correct |
7 |
Correct |
4 ms |
8140 KB |
Output is correct |
8 |
Correct |
4 ms |
8140 KB |
Output is correct |
9 |
Correct |
6 ms |
8144 KB |
Output is correct |
10 |
Correct |
4 ms |
8140 KB |
Output is correct |
11 |
Correct |
5 ms |
8140 KB |
Output is correct |
12 |
Correct |
5 ms |
8140 KB |
Output is correct |
13 |
Correct |
4 ms |
8140 KB |
Output is correct |
14 |
Correct |
4 ms |
8140 KB |
Output is correct |
15 |
Correct |
4 ms |
8088 KB |
Output is correct |
16 |
Correct |
5 ms |
8140 KB |
Output is correct |
17 |
Correct |
5 ms |
8140 KB |
Output is correct |
18 |
Correct |
5 ms |
8140 KB |
Output is correct |
19 |
Correct |
5 ms |
8140 KB |
Output is correct |
20 |
Correct |
5 ms |
8140 KB |
Output is correct |
21 |
Correct |
5 ms |
8140 KB |
Output is correct |
22 |
Correct |
5 ms |
8140 KB |
Output is correct |
23 |
Correct |
5 ms |
8140 KB |
Output is correct |
24 |
Correct |
5 ms |
8140 KB |
Output is correct |
25 |
Correct |
5 ms |
8140 KB |
Output is correct |
26 |
Correct |
5 ms |
8140 KB |
Output is correct |
27 |
Correct |
5 ms |
8140 KB |
Output is correct |
28 |
Correct |
5 ms |
8140 KB |
Output is correct |
29 |
Correct |
5 ms |
8140 KB |
Output is correct |
30 |
Correct |
1006 ms |
72252 KB |
Output is correct |
31 |
Correct |
1002 ms |
72324 KB |
Output is correct |
32 |
Correct |
510 ms |
64492 KB |
Output is correct |
33 |
Correct |
501 ms |
78256 KB |
Output is correct |
34 |
Correct |
515 ms |
78264 KB |
Output is correct |
35 |
Correct |
517 ms |
78628 KB |
Output is correct |
36 |
Correct |
498 ms |
78056 KB |
Output is correct |
37 |
Correct |
485 ms |
72892 KB |
Output is correct |
38 |
Correct |
498 ms |
72448 KB |
Output is correct |
39 |
Correct |
510 ms |
72452 KB |
Output is correct |
40 |
Correct |
526 ms |
72260 KB |
Output is correct |
41 |
Correct |
833 ms |
72308 KB |
Output is correct |
42 |
Correct |
741 ms |
72188 KB |
Output is correct |
43 |
Correct |
502 ms |
76248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8140 KB |
Output is correct |
2 |
Correct |
5 ms |
8140 KB |
Output is correct |
3 |
Correct |
5 ms |
8140 KB |
Output is correct |
4 |
Correct |
5 ms |
8140 KB |
Output is correct |
5 |
Correct |
5 ms |
8140 KB |
Output is correct |
6 |
Correct |
5 ms |
8140 KB |
Output is correct |
7 |
Correct |
5 ms |
8140 KB |
Output is correct |
8 |
Correct |
5 ms |
8140 KB |
Output is correct |
9 |
Correct |
5 ms |
8140 KB |
Output is correct |
10 |
Correct |
5 ms |
8140 KB |
Output is correct |
11 |
Correct |
5 ms |
8140 KB |
Output is correct |
12 |
Correct |
5 ms |
8224 KB |
Output is correct |
13 |
Correct |
5 ms |
8140 KB |
Output is correct |
14 |
Correct |
5 ms |
8216 KB |
Output is correct |
15 |
Correct |
5 ms |
8140 KB |
Output is correct |
16 |
Correct |
5 ms |
8140 KB |
Output is correct |
17 |
Correct |
5 ms |
8196 KB |
Output is correct |
18 |
Correct |
5 ms |
8200 KB |
Output is correct |
19 |
Correct |
5 ms |
8140 KB |
Output is correct |
20 |
Correct |
5 ms |
8140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8140 KB |
Output is correct |
2 |
Correct |
5 ms |
8140 KB |
Output is correct |
3 |
Correct |
4 ms |
8140 KB |
Output is correct |
4 |
Correct |
4 ms |
8140 KB |
Output is correct |
5 |
Correct |
5 ms |
8140 KB |
Output is correct |
6 |
Correct |
5 ms |
8032 KB |
Output is correct |
7 |
Correct |
4 ms |
8140 KB |
Output is correct |
8 |
Correct |
4 ms |
8140 KB |
Output is correct |
9 |
Correct |
6 ms |
8144 KB |
Output is correct |
10 |
Correct |
4 ms |
8140 KB |
Output is correct |
11 |
Correct |
5 ms |
8140 KB |
Output is correct |
12 |
Correct |
5 ms |
8140 KB |
Output is correct |
13 |
Correct |
4 ms |
8140 KB |
Output is correct |
14 |
Correct |
4 ms |
8140 KB |
Output is correct |
15 |
Correct |
4 ms |
8088 KB |
Output is correct |
16 |
Correct |
5 ms |
8140 KB |
Output is correct |
17 |
Correct |
5 ms |
8140 KB |
Output is correct |
18 |
Correct |
5 ms |
8140 KB |
Output is correct |
19 |
Correct |
5 ms |
8140 KB |
Output is correct |
20 |
Correct |
5 ms |
8140 KB |
Output is correct |
21 |
Correct |
5 ms |
8140 KB |
Output is correct |
22 |
Correct |
5 ms |
8140 KB |
Output is correct |
23 |
Correct |
5 ms |
8140 KB |
Output is correct |
24 |
Correct |
5 ms |
8140 KB |
Output is correct |
25 |
Correct |
5 ms |
8140 KB |
Output is correct |
26 |
Correct |
5 ms |
8140 KB |
Output is correct |
27 |
Correct |
5 ms |
8140 KB |
Output is correct |
28 |
Correct |
5 ms |
8140 KB |
Output is correct |
29 |
Correct |
5 ms |
8140 KB |
Output is correct |
30 |
Correct |
1006 ms |
72252 KB |
Output is correct |
31 |
Correct |
1002 ms |
72324 KB |
Output is correct |
32 |
Correct |
510 ms |
64492 KB |
Output is correct |
33 |
Correct |
501 ms |
78256 KB |
Output is correct |
34 |
Correct |
515 ms |
78264 KB |
Output is correct |
35 |
Correct |
517 ms |
78628 KB |
Output is correct |
36 |
Correct |
498 ms |
78056 KB |
Output is correct |
37 |
Correct |
485 ms |
72892 KB |
Output is correct |
38 |
Correct |
498 ms |
72448 KB |
Output is correct |
39 |
Correct |
510 ms |
72452 KB |
Output is correct |
40 |
Correct |
526 ms |
72260 KB |
Output is correct |
41 |
Correct |
833 ms |
72308 KB |
Output is correct |
42 |
Correct |
741 ms |
72188 KB |
Output is correct |
43 |
Correct |
502 ms |
76248 KB |
Output is correct |
44 |
Correct |
5 ms |
8140 KB |
Output is correct |
45 |
Correct |
5 ms |
8140 KB |
Output is correct |
46 |
Correct |
5 ms |
8140 KB |
Output is correct |
47 |
Correct |
5 ms |
8140 KB |
Output is correct |
48 |
Correct |
5 ms |
8140 KB |
Output is correct |
49 |
Correct |
5 ms |
8140 KB |
Output is correct |
50 |
Correct |
5 ms |
8140 KB |
Output is correct |
51 |
Correct |
5 ms |
8140 KB |
Output is correct |
52 |
Correct |
5 ms |
8140 KB |
Output is correct |
53 |
Correct |
5 ms |
8140 KB |
Output is correct |
54 |
Correct |
5 ms |
8140 KB |
Output is correct |
55 |
Correct |
5 ms |
8224 KB |
Output is correct |
56 |
Correct |
5 ms |
8140 KB |
Output is correct |
57 |
Correct |
5 ms |
8216 KB |
Output is correct |
58 |
Correct |
5 ms |
8140 KB |
Output is correct |
59 |
Correct |
5 ms |
8140 KB |
Output is correct |
60 |
Correct |
5 ms |
8196 KB |
Output is correct |
61 |
Correct |
5 ms |
8200 KB |
Output is correct |
62 |
Correct |
5 ms |
8140 KB |
Output is correct |
63 |
Correct |
5 ms |
8140 KB |
Output is correct |
64 |
Correct |
509 ms |
74080 KB |
Output is correct |
65 |
Correct |
505 ms |
74048 KB |
Output is correct |
66 |
Correct |
506 ms |
72808 KB |
Output is correct |
67 |
Correct |
486 ms |
72536 KB |
Output is correct |
68 |
Correct |
55 ms |
15680 KB |
Output is correct |
69 |
Correct |
48 ms |
15596 KB |
Output is correct |
70 |
Correct |
47 ms |
15584 KB |
Output is correct |
71 |
Correct |
48 ms |
15652 KB |
Output is correct |
72 |
Correct |
47 ms |
15552 KB |
Output is correct |
73 |
Correct |
50 ms |
15280 KB |
Output is correct |
74 |
Correct |
509 ms |
78176 KB |
Output is correct |
75 |
Correct |
511 ms |
78208 KB |
Output is correct |
76 |
Correct |
527 ms |
76312 KB |
Output is correct |
77 |
Correct |
548 ms |
76256 KB |
Output is correct |
78 |
Correct |
517 ms |
78212 KB |
Output is correct |
79 |
Correct |
525 ms |
78104 KB |
Output is correct |
80 |
Correct |
481 ms |
64372 KB |
Output is correct |
81 |
Correct |
637 ms |
80224 KB |
Output is correct |
82 |
Correct |
653 ms |
86924 KB |
Output is correct |
83 |
Correct |
578 ms |
82860 KB |
Output is correct |
84 |
Correct |
545 ms |
82456 KB |
Output is correct |
85 |
Correct |
531 ms |
80708 KB |
Output is correct |
86 |
Correct |
500 ms |
79816 KB |
Output is correct |
87 |
Correct |
642 ms |
81580 KB |
Output is correct |
88 |
Correct |
623 ms |
83744 KB |
Output is correct |
89 |
Correct |
584 ms |
83416 KB |
Output is correct |
90 |
Correct |
489 ms |
79296 KB |
Output is correct |
91 |
Correct |
493 ms |
79216 KB |
Output is correct |
92 |
Correct |
526 ms |
79092 KB |
Output is correct |