//In the name of God
#include <bits/stdc++.h>
#include "books.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const ll maxn = 1e6 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
#define pb push_back
#define Mp make_pair
#define F first
#define S second
#define Sz(x) ll((x).size())
#define all(x) (x).begin(), (x).end()
ll a[maxn], t, tt, c[maxn], par[maxn];
ll mn[maxn], mx[maxn], mnp[maxn], mxp[maxn];
bool vis[maxn];
vector<ll> vec, g[maxn], gp[maxn], gg[maxn];
vector<pll> v2;
void dfs(ll v){
vis[v] = 1;
for(ll u : g[v]){
if(!vis[u]) dfs(u);
}
vec.pb(v);
}
void sfd(ll v){
vis[v] = 1;
c[v] = tt;
mnp[tt] = min(mnp[tt], mn[v]);
mxp[tt] = max(mxp[tt], mx[v]);
for(ll u : gp[v]){
if(!vis[u]) sfd(u);
}
}
ll solve(ll v, ll s){
ll x = 0;
ll ans = 0;
ll i = 0;
for(ll u : gg[v]){
if(mnp[u] <= s && s <= mxp[u]) x = i;
ans += solve(u, s);
i++;
}
if(mnp[v] == -1){
return ans + Sz(gg[v]) - 1;
}
ans += max(0ll, min(x, Sz(gg[v]) - x - 1));
return ans;
}
long long minimum_walk(vector<int> p, int s){
ll n = Sz(p);
ll l = 0;
while(l < n && p[l] == l) l++;
ll r = n;
while(r && p[r - 1] == r - 1) r--;
l = min(l, (ll)s);
r = max(r, s + 1ll);
mn[0] = -1;
mx[0] = n + 1;
t++;
for(ll i = l; i < r; i++){
if(p[i] > i){
a[i]++;
a[p[i]]--;
}
if(!vis[i]){
ll v = i;
mn[t] = inf;
mx[t] = -inf;
do{
vis[v] = 1;
mx[t] = max(mx[t], v);
mn[t] = min(mn[t], v);
v = p[v];
}while(v != i);
t++;
}
}
ll ans = 0;
for(ll i = 1; i < n; i++){
ans += a[i - 1];
a[i] += a[i - 1];
}
for(ll i = 0; i < s; i++){
g[c[i]].pb(c[i + 1]);
gp[c[i + 1]].pb(c[i]);
}
for(ll i = s; i + 1 < n; i++){
g[c[i + 1]].pb(c[i]);
gp[c[i]].pb(c[i + 1]);
}
// SCC
fill(vis, vis + t, 0);
for(ll i = 0; i < t; i++){
if(!vis[i]) dfs(i);
}
reverse(all(vec));
fill(vis, vis + t, 0);
for(ll i = 0; i < t; i++){
if(!vis[vec[i]]){
mnp[tt] = inf;
mxp[tt] = -inf;
sfd(vec[i]);
tt++;
}
}
vec.clear();
ll root = -1;
for(ll i = 0; i < tt; i++){
if(mnp[i] == -1) root = i;
v2.pb(Mp(mnp[i], i));
}
sort(all(v2));
for(pll i : v2){
if(vec.empty()){
vec.pb(i.S);
continue;
}
while(mxp[vec.back()] < i.F) vec.pop_back();
gg[vec.back()].pb(i.S);
vec.pb(i.S);
}
ans += solve(root, s);
return ans * 2;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
70728 KB |
Output is correct |
2 |
Correct |
42 ms |
70792 KB |
Output is correct |
3 |
Correct |
41 ms |
70696 KB |
Output is correct |
4 |
Correct |
40 ms |
70716 KB |
Output is correct |
5 |
Correct |
45 ms |
70748 KB |
Output is correct |
6 |
Correct |
41 ms |
70772 KB |
Output is correct |
7 |
Correct |
42 ms |
70688 KB |
Output is correct |
8 |
Correct |
42 ms |
70788 KB |
Output is correct |
9 |
Correct |
42 ms |
70688 KB |
Output is correct |
10 |
Correct |
42 ms |
70724 KB |
Output is correct |
11 |
Correct |
41 ms |
70708 KB |
Output is correct |
12 |
Correct |
41 ms |
70760 KB |
Output is correct |
13 |
Correct |
41 ms |
70732 KB |
Output is correct |
14 |
Correct |
42 ms |
70756 KB |
Output is correct |
15 |
Correct |
41 ms |
70748 KB |
Output is correct |
16 |
Correct |
42 ms |
70780 KB |
Output is correct |
17 |
Correct |
42 ms |
70684 KB |
Output is correct |
18 |
Correct |
41 ms |
70800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
70728 KB |
Output is correct |
2 |
Correct |
42 ms |
70792 KB |
Output is correct |
3 |
Correct |
41 ms |
70696 KB |
Output is correct |
4 |
Correct |
40 ms |
70716 KB |
Output is correct |
5 |
Correct |
45 ms |
70748 KB |
Output is correct |
6 |
Correct |
41 ms |
70772 KB |
Output is correct |
7 |
Correct |
42 ms |
70688 KB |
Output is correct |
8 |
Correct |
42 ms |
70788 KB |
Output is correct |
9 |
Correct |
42 ms |
70688 KB |
Output is correct |
10 |
Correct |
42 ms |
70724 KB |
Output is correct |
11 |
Correct |
41 ms |
70708 KB |
Output is correct |
12 |
Correct |
41 ms |
70760 KB |
Output is correct |
13 |
Correct |
41 ms |
70732 KB |
Output is correct |
14 |
Correct |
42 ms |
70756 KB |
Output is correct |
15 |
Correct |
41 ms |
70748 KB |
Output is correct |
16 |
Correct |
42 ms |
70780 KB |
Output is correct |
17 |
Correct |
42 ms |
70684 KB |
Output is correct |
18 |
Correct |
41 ms |
70800 KB |
Output is correct |
19 |
Correct |
42 ms |
70832 KB |
Output is correct |
20 |
Correct |
43 ms |
70756 KB |
Output is correct |
21 |
Correct |
42 ms |
70724 KB |
Output is correct |
22 |
Correct |
41 ms |
70812 KB |
Output is correct |
23 |
Correct |
45 ms |
70840 KB |
Output is correct |
24 |
Correct |
42 ms |
70840 KB |
Output is correct |
25 |
Correct |
40 ms |
70888 KB |
Output is correct |
26 |
Correct |
42 ms |
70848 KB |
Output is correct |
27 |
Correct |
40 ms |
70732 KB |
Output is correct |
28 |
Correct |
44 ms |
70828 KB |
Output is correct |
29 |
Correct |
41 ms |
70852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
70728 KB |
Output is correct |
2 |
Correct |
42 ms |
70792 KB |
Output is correct |
3 |
Correct |
41 ms |
70696 KB |
Output is correct |
4 |
Correct |
40 ms |
70716 KB |
Output is correct |
5 |
Correct |
45 ms |
70748 KB |
Output is correct |
6 |
Correct |
41 ms |
70772 KB |
Output is correct |
7 |
Correct |
42 ms |
70688 KB |
Output is correct |
8 |
Correct |
42 ms |
70788 KB |
Output is correct |
9 |
Correct |
42 ms |
70688 KB |
Output is correct |
10 |
Correct |
42 ms |
70724 KB |
Output is correct |
11 |
Correct |
41 ms |
70708 KB |
Output is correct |
12 |
Correct |
41 ms |
70760 KB |
Output is correct |
13 |
Correct |
41 ms |
70732 KB |
Output is correct |
14 |
Correct |
42 ms |
70756 KB |
Output is correct |
15 |
Correct |
41 ms |
70748 KB |
Output is correct |
16 |
Correct |
42 ms |
70780 KB |
Output is correct |
17 |
Correct |
42 ms |
70684 KB |
Output is correct |
18 |
Correct |
41 ms |
70800 KB |
Output is correct |
19 |
Correct |
42 ms |
70832 KB |
Output is correct |
20 |
Correct |
43 ms |
70756 KB |
Output is correct |
21 |
Correct |
42 ms |
70724 KB |
Output is correct |
22 |
Correct |
41 ms |
70812 KB |
Output is correct |
23 |
Correct |
45 ms |
70840 KB |
Output is correct |
24 |
Correct |
42 ms |
70840 KB |
Output is correct |
25 |
Correct |
40 ms |
70888 KB |
Output is correct |
26 |
Correct |
42 ms |
70848 KB |
Output is correct |
27 |
Correct |
40 ms |
70732 KB |
Output is correct |
28 |
Correct |
44 ms |
70828 KB |
Output is correct |
29 |
Correct |
41 ms |
70852 KB |
Output is correct |
30 |
Correct |
235 ms |
103228 KB |
Output is correct |
31 |
Correct |
233 ms |
103320 KB |
Output is correct |
32 |
Correct |
204 ms |
102244 KB |
Output is correct |
33 |
Correct |
293 ms |
159572 KB |
Output is correct |
34 |
Correct |
293 ms |
159708 KB |
Output is correct |
35 |
Correct |
288 ms |
145356 KB |
Output is correct |
36 |
Correct |
246 ms |
124276 KB |
Output is correct |
37 |
Correct |
221 ms |
106136 KB |
Output is correct |
38 |
Correct |
213 ms |
103700 KB |
Output is correct |
39 |
Correct |
213 ms |
103564 KB |
Output is correct |
40 |
Correct |
225 ms |
103300 KB |
Output is correct |
41 |
Correct |
224 ms |
103240 KB |
Output is correct |
42 |
Correct |
222 ms |
103252 KB |
Output is correct |
43 |
Correct |
336 ms |
161876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
70816 KB |
Output is correct |
2 |
Incorrect |
43 ms |
70768 KB |
3rd lines differ - on the 1st token, expected: '9800', found: '9798' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
70728 KB |
Output is correct |
2 |
Correct |
42 ms |
70792 KB |
Output is correct |
3 |
Correct |
41 ms |
70696 KB |
Output is correct |
4 |
Correct |
40 ms |
70716 KB |
Output is correct |
5 |
Correct |
45 ms |
70748 KB |
Output is correct |
6 |
Correct |
41 ms |
70772 KB |
Output is correct |
7 |
Correct |
42 ms |
70688 KB |
Output is correct |
8 |
Correct |
42 ms |
70788 KB |
Output is correct |
9 |
Correct |
42 ms |
70688 KB |
Output is correct |
10 |
Correct |
42 ms |
70724 KB |
Output is correct |
11 |
Correct |
41 ms |
70708 KB |
Output is correct |
12 |
Correct |
41 ms |
70760 KB |
Output is correct |
13 |
Correct |
41 ms |
70732 KB |
Output is correct |
14 |
Correct |
42 ms |
70756 KB |
Output is correct |
15 |
Correct |
41 ms |
70748 KB |
Output is correct |
16 |
Correct |
42 ms |
70780 KB |
Output is correct |
17 |
Correct |
42 ms |
70684 KB |
Output is correct |
18 |
Correct |
41 ms |
70800 KB |
Output is correct |
19 |
Correct |
42 ms |
70832 KB |
Output is correct |
20 |
Correct |
43 ms |
70756 KB |
Output is correct |
21 |
Correct |
42 ms |
70724 KB |
Output is correct |
22 |
Correct |
41 ms |
70812 KB |
Output is correct |
23 |
Correct |
45 ms |
70840 KB |
Output is correct |
24 |
Correct |
42 ms |
70840 KB |
Output is correct |
25 |
Correct |
40 ms |
70888 KB |
Output is correct |
26 |
Correct |
42 ms |
70848 KB |
Output is correct |
27 |
Correct |
40 ms |
70732 KB |
Output is correct |
28 |
Correct |
44 ms |
70828 KB |
Output is correct |
29 |
Correct |
41 ms |
70852 KB |
Output is correct |
30 |
Correct |
235 ms |
103228 KB |
Output is correct |
31 |
Correct |
233 ms |
103320 KB |
Output is correct |
32 |
Correct |
204 ms |
102244 KB |
Output is correct |
33 |
Correct |
293 ms |
159572 KB |
Output is correct |
34 |
Correct |
293 ms |
159708 KB |
Output is correct |
35 |
Correct |
288 ms |
145356 KB |
Output is correct |
36 |
Correct |
246 ms |
124276 KB |
Output is correct |
37 |
Correct |
221 ms |
106136 KB |
Output is correct |
38 |
Correct |
213 ms |
103700 KB |
Output is correct |
39 |
Correct |
213 ms |
103564 KB |
Output is correct |
40 |
Correct |
225 ms |
103300 KB |
Output is correct |
41 |
Correct |
224 ms |
103240 KB |
Output is correct |
42 |
Correct |
222 ms |
103252 KB |
Output is correct |
43 |
Correct |
336 ms |
161876 KB |
Output is correct |
44 |
Correct |
42 ms |
70816 KB |
Output is correct |
45 |
Incorrect |
43 ms |
70768 KB |
3rd lines differ - on the 1st token, expected: '9800', found: '9798' |
46 |
Halted |
0 ms |
0 KB |
- |