#include "books.h"
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll big = 1e18;
struct obj{
ll i,w;
bool operator<(const obj &o)const{
return w>o.w;
}
};
struct BIT{
ll n;
vector<ll> a;
BIT(ll N):n(N),a(N+5,0){}
void add(ll i, ll v){
for(;i<=n;i+=i&-i) a[i] += v;
}
ll get(ll i){
ll v = 0;
for(;i>0;i-=i&-i) v += a[i];
return v;
}
};
struct segtree{
ll n;
vector<ll> a;
segtree(ll N):n(N),a(N<<2,big){}
void mod(ll i, ll l, ll r, ll ql, ll qr, ll x){
if(ql<=l&&r<=qr) a[i] = min(a[i],x);
else{
ll m = l+r>>1;
if(ql<=m) mod(i<<1,l,m,ql,qr,x);
if(qr>m) mod(i<<1|1,m+1,r,ql,qr,x);
}
}
ll get(ll i, ll l, ll r, ll x){
if (l == r) return a[i];
else{
ll m = l+r>>1;
if (x<=m) return min(a[i],get(i<<1,l,m,x));
else return min(a[i],get(i<<1|1,m+1,r,x));
}
}
};
long long minimum_walk(std::vector<int> p, int s) {
ll ans = 0;
ll n = p.size();
ll gpc = 0;
vector<ll> gp(n,-1);
vector<ll> L,R;
vector<vector<ll>> gps;
for(ll i = 0;i<n;i++){
if (gp[i]==-1){
ll cur = gpc++;
gps.emplace_back();
L.push_back(n);
R.push_back(0);
ll x = i;
do{
L[cur] = min(x,L[cur]);
R[cur] = max(x,R[cur]);
gp[x] = cur;
gps[cur].push_back(x);
ans += abs(p[x]-x);
x = p[x];
} while(x!=i);
}
}
vector<ll> v(n,0);
for(ll i : L) v[i]++;
for(ll i : R) v[i]--;
for(ll i = 1;i<n;i++) v[i] += v[i-1];
ll rf = n-1;
while(rf&&p[rf]==rf) rf--;
for(ll i = 0;i<rf;i++) if(!v[i]) ans += 2;
ll cL = s, cR = s;
for(ll i : L) if(i<=s) cL=min(cL,i);
for(ll i : R) if(i>=s) cR=max(cR,i);
/*
vector<ll> dis(n,n);
segtree t(n);
priority_queue<obj> q;
q.push({s,0});
while(!q.empty()){
auto [i,w] = q.top();q.pop();
w = min(w,t.get(1,0,n-1,i));
if (dis[i]<=w) continue;
dis[i] = w;
q.push({L[gp[i]],w});
q.push({R[gp[i]],w});
t.mod(1,0,n-1,L[gp[i]],R[gp[i]],w);
if(i>0) q.push({i-1,w+1});
if(i<n-1) q.push({i+1,w+1});
}
ans += min(dis[cL],dis[cR])*2;*/
if (s!=0&&n<=1000){
ll m = gpc;
vector<vector<ll>> con(m,vector<ll>(m,n));
for(ll i = 0;i<m;i++){
for(ll j = 0;j<m;j++){
for(ll x : gps[j]){
for(ll y : gps[i]){
con[i][j] = min(con[i][j],abs(x-y));
}
if(L[i]<=x&&x<=R[i]) con[i][j] = 0;
}
}
}
for(ll i = 0;i<m;i++){
for(ll j = i;j<m;j++){
ll o = con[i][j]+con[j][i];
con[i][j] = con[j][i] = o;
}
}
vector<ll> dis(m,n);
priority_queue<obj> q;
q.push({gp[s],0});
while(!q.empty()){
auto [i,w] = q.top();q.pop();
if(dis[i]<=w) continue;
dis[i] = w;
for(ll j = 0;j<m;j++){
q.push({j,w+con[i][j]});
}
}
ll add = min(dis[gp[cL]],dis[gp[cR]]);
ans += add;
}
return ans;
}
Compilation message
books.cpp: In member function 'void segtree::mod(ll, ll, ll, ll, ll, ll)':
books.cpp:32:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | ll m = l+r>>1;
| ~^~
books.cpp: In member function 'll segtree::get(ll, ll, ll, ll)':
books.cpp:40:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
40 | ll m = l+r>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
78 ms |
35432 KB |
Output is correct |
31 |
Correct |
80 ms |
35108 KB |
Output is correct |
32 |
Correct |
107 ms |
94496 KB |
Output is correct |
33 |
Correct |
106 ms |
81652 KB |
Output is correct |
34 |
Correct |
107 ms |
80108 KB |
Output is correct |
35 |
Correct |
105 ms |
74736 KB |
Output is correct |
36 |
Correct |
101 ms |
48812 KB |
Output is correct |
37 |
Correct |
67 ms |
37128 KB |
Output is correct |
38 |
Correct |
66 ms |
34908 KB |
Output is correct |
39 |
Correct |
66 ms |
34640 KB |
Output is correct |
40 |
Correct |
67 ms |
32972 KB |
Output is correct |
41 |
Correct |
86 ms |
32892 KB |
Output is correct |
42 |
Correct |
76 ms |
32744 KB |
Output is correct |
43 |
Correct |
106 ms |
79864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
52 ms |
13252 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3165' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
78 ms |
35432 KB |
Output is correct |
31 |
Correct |
80 ms |
35108 KB |
Output is correct |
32 |
Correct |
107 ms |
94496 KB |
Output is correct |
33 |
Correct |
106 ms |
81652 KB |
Output is correct |
34 |
Correct |
107 ms |
80108 KB |
Output is correct |
35 |
Correct |
105 ms |
74736 KB |
Output is correct |
36 |
Correct |
101 ms |
48812 KB |
Output is correct |
37 |
Correct |
67 ms |
37128 KB |
Output is correct |
38 |
Correct |
66 ms |
34908 KB |
Output is correct |
39 |
Correct |
66 ms |
34640 KB |
Output is correct |
40 |
Correct |
67 ms |
32972 KB |
Output is correct |
41 |
Correct |
86 ms |
32892 KB |
Output is correct |
42 |
Correct |
76 ms |
32744 KB |
Output is correct |
43 |
Correct |
106 ms |
79864 KB |
Output is correct |
44 |
Incorrect |
52 ms |
13252 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3165' |
45 |
Halted |
0 ms |
0 KB |
- |