Submission #390639

# Submission time Handle Problem Language Result Execution time Memory
390639 2021-04-16T12:44:18 Z Keshi Ancient Books (IOI17_books) C++17
50 / 100
461 ms 82248 KB
//In the name of God
#include <bits/stdc++.h>
#include "books.h"
using namespace std;

typedef int ll;
typedef pair<ll, ll> pll;

const ll maxn = 1e6 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e9;

#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()
#define lc (id << 1)
#define rc (lc | 1)

ll a[maxn], t, tt, c[maxn], rr[maxn];
ll b[maxn];
ll mn[maxn], mx[maxn], mnp[maxn], mxp[maxn];
bool vis[maxn];
vector<ll> vec, g[maxn];
vector<pll> v2;

ll seg[maxn << 2];

void bld(ll id, ll s, ll e){
	if(e - s == 1){
		seg[id] = b[s];
		return;
	}
	ll mid = (s + e) >> 1;
	bld(lc, s, mid);
	bld(rc, mid, e);
	seg[id] = max(seg[lc], seg[rc]);
}
ll get(ll id, ll s, ll e, ll l, ll r){
	if(r <= s || e <= l) return 0;
	if(l <= s && e <= r) return seg[id];
	ll mid = (s + e) >> 1;
	return max(get(lc, s, mid, l, r), get(rc, mid, e, l, r));
}

ll sz[maxn], cnt[maxn];
bool val[maxn];

ll solve(ll v, ll s){
	ll x = -1;
	ll ans = 0;
	for(ll i = 0; i < Sz(g[v]); i++){
		ll u = g[v][i];
		if(mnp[u] <= s && s <= mxp[u]) x = i;
		ans += solve(u, s);
		sz[v] += sz[u];
	}
	val[v] = (sz[v] == mxp[v] - mnp[v] + 1);
	if(x == -1 || mnp[v] > s || s > mxp[v]) return 0;
	ll j = 0;
	ll y = inf;
	ll o = mnp[g[v][x]];
	for(ll i = x; i--;){
		ll u = g[v][i];
		if(!val[u]) continue;
		j++;
		if(mxp[u] + 1 != o){
			y = min(y, j);
			break;
		}
		o = mnp[u];
	}
	y = min(y, j + 1);
	j = 0;
	o = mxp[g[v][x]];
	for(ll i = x + 1; i < Sz(g[v]); i++){
		ll u = g[v][i];
		if(!val[u]) continue;
		j++;
		if(mnp[u] != o + 1){
			y = min(y, j);
			break;
		}
		o = mxp[u];
	}
	y = min(y, j + 1);
	if(mnp[v] == -1){
		return ans + Sz(g[v]) - 1;
	}
	return ans + y;
}

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, s);
	r = max(r, s + 1);
	mn[0] = -1;
	mx[0] = n;
	t++;
	long long ans = 0;
	for(ll i = l; i < r; i++){
		ans += abs(p[i] - 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);
				cnt[t]++;
				v = p[v];
			}while(v != i);
			rr[mx[t]] = t;
			b[mn[t]] = mx[t];
			t++;
		}
	}
	bld(1, 0, n);
	for(ll i = 0; i < t; i++){
		v2.pb(Mp(mn[i], i));
	}
	for(pll i : v2){
		if(!vis[i.S]) continue;
		mnp[tt] = i.F;
		mxp[tt] = mx[i.S];
		sz[tt] = cnt[i.S];
		vis[i.S] = 0;
		while(true){
			ll x = get(1, 0, n, mnp[tt], mxp[tt] + 1);
			if(!vis[rr[x]] || x <= mxp[tt]) break;
			mxp[tt] = x;
			sz[tt] += cnt[rr[x]];
			vis[rr[x]] = 0;
		}
		tt++;
	}
	vec.clear();
	v2.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();
		g[vec.back()].pb(i.S);
		vec.pb(i.S);
	}
	ll xx = solve(root, s);
	ans += 2 * xx;

	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23756 KB Output is correct
2 Correct 16 ms 23848 KB Output is correct
3 Correct 16 ms 23780 KB Output is correct
4 Correct 16 ms 23788 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 15 ms 23756 KB Output is correct
7 Correct 16 ms 23756 KB Output is correct
8 Correct 15 ms 23820 KB Output is correct
9 Correct 15 ms 23972 KB Output is correct
10 Correct 17 ms 23788 KB Output is correct
11 Correct 16 ms 23884 KB Output is correct
12 Correct 16 ms 23816 KB Output is correct
13 Correct 16 ms 23816 KB Output is correct
14 Correct 16 ms 23908 KB Output is correct
15 Correct 16 ms 23884 KB Output is correct
16 Correct 15 ms 23860 KB Output is correct
17 Correct 15 ms 23796 KB Output is correct
18 Correct 16 ms 23840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23756 KB Output is correct
2 Correct 16 ms 23848 KB Output is correct
3 Correct 16 ms 23780 KB Output is correct
4 Correct 16 ms 23788 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 15 ms 23756 KB Output is correct
7 Correct 16 ms 23756 KB Output is correct
8 Correct 15 ms 23820 KB Output is correct
9 Correct 15 ms 23972 KB Output is correct
10 Correct 17 ms 23788 KB Output is correct
11 Correct 16 ms 23884 KB Output is correct
12 Correct 16 ms 23816 KB Output is correct
13 Correct 16 ms 23816 KB Output is correct
14 Correct 16 ms 23908 KB Output is correct
15 Correct 16 ms 23884 KB Output is correct
16 Correct 15 ms 23860 KB Output is correct
17 Correct 15 ms 23796 KB Output is correct
18 Correct 16 ms 23840 KB Output is correct
19 Correct 16 ms 23884 KB Output is correct
20 Correct 15 ms 23896 KB Output is correct
21 Correct 15 ms 23900 KB Output is correct
22 Correct 16 ms 23884 KB Output is correct
23 Correct 16 ms 23884 KB Output is correct
24 Correct 16 ms 23848 KB Output is correct
25 Correct 16 ms 23816 KB Output is correct
26 Correct 16 ms 23908 KB Output is correct
27 Correct 16 ms 23892 KB Output is correct
28 Correct 15 ms 23884 KB Output is correct
29 Correct 16 ms 23884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23756 KB Output is correct
2 Correct 16 ms 23848 KB Output is correct
3 Correct 16 ms 23780 KB Output is correct
4 Correct 16 ms 23788 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 15 ms 23756 KB Output is correct
7 Correct 16 ms 23756 KB Output is correct
8 Correct 15 ms 23820 KB Output is correct
9 Correct 15 ms 23972 KB Output is correct
10 Correct 17 ms 23788 KB Output is correct
11 Correct 16 ms 23884 KB Output is correct
12 Correct 16 ms 23816 KB Output is correct
13 Correct 16 ms 23816 KB Output is correct
14 Correct 16 ms 23908 KB Output is correct
15 Correct 16 ms 23884 KB Output is correct
16 Correct 15 ms 23860 KB Output is correct
17 Correct 15 ms 23796 KB Output is correct
18 Correct 16 ms 23840 KB Output is correct
19 Correct 16 ms 23884 KB Output is correct
20 Correct 15 ms 23896 KB Output is correct
21 Correct 15 ms 23900 KB Output is correct
22 Correct 16 ms 23884 KB Output is correct
23 Correct 16 ms 23884 KB Output is correct
24 Correct 16 ms 23848 KB Output is correct
25 Correct 16 ms 23816 KB Output is correct
26 Correct 16 ms 23908 KB Output is correct
27 Correct 16 ms 23892 KB Output is correct
28 Correct 15 ms 23884 KB Output is correct
29 Correct 16 ms 23884 KB Output is correct
30 Correct 180 ms 40824 KB Output is correct
31 Correct 181 ms 41284 KB Output is correct
32 Correct 157 ms 40316 KB Output is correct
33 Correct 399 ms 77968 KB Output is correct
34 Correct 392 ms 78032 KB Output is correct
35 Correct 348 ms 70560 KB Output is correct
36 Correct 256 ms 60468 KB Output is correct
37 Correct 177 ms 50660 KB Output is correct
38 Correct 199 ms 48344 KB Output is correct
39 Correct 161 ms 47832 KB Output is correct
40 Correct 165 ms 43440 KB Output is correct
41 Correct 174 ms 41540 KB Output is correct
42 Correct 171 ms 42000 KB Output is correct
43 Correct 461 ms 82248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23884 KB Output is correct
2 Correct 15 ms 23928 KB Output is correct
3 Correct 15 ms 23928 KB Output is correct
4 Correct 15 ms 23936 KB Output is correct
5 Correct 16 ms 23884 KB Output is correct
6 Incorrect 16 ms 23848 KB 3rd lines differ - on the 1st token, expected: '22140', found: '22156'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23756 KB Output is correct
2 Correct 16 ms 23848 KB Output is correct
3 Correct 16 ms 23780 KB Output is correct
4 Correct 16 ms 23788 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 15 ms 23756 KB Output is correct
7 Correct 16 ms 23756 KB Output is correct
8 Correct 15 ms 23820 KB Output is correct
9 Correct 15 ms 23972 KB Output is correct
10 Correct 17 ms 23788 KB Output is correct
11 Correct 16 ms 23884 KB Output is correct
12 Correct 16 ms 23816 KB Output is correct
13 Correct 16 ms 23816 KB Output is correct
14 Correct 16 ms 23908 KB Output is correct
15 Correct 16 ms 23884 KB Output is correct
16 Correct 15 ms 23860 KB Output is correct
17 Correct 15 ms 23796 KB Output is correct
18 Correct 16 ms 23840 KB Output is correct
19 Correct 16 ms 23884 KB Output is correct
20 Correct 15 ms 23896 KB Output is correct
21 Correct 15 ms 23900 KB Output is correct
22 Correct 16 ms 23884 KB Output is correct
23 Correct 16 ms 23884 KB Output is correct
24 Correct 16 ms 23848 KB Output is correct
25 Correct 16 ms 23816 KB Output is correct
26 Correct 16 ms 23908 KB Output is correct
27 Correct 16 ms 23892 KB Output is correct
28 Correct 15 ms 23884 KB Output is correct
29 Correct 16 ms 23884 KB Output is correct
30 Correct 180 ms 40824 KB Output is correct
31 Correct 181 ms 41284 KB Output is correct
32 Correct 157 ms 40316 KB Output is correct
33 Correct 399 ms 77968 KB Output is correct
34 Correct 392 ms 78032 KB Output is correct
35 Correct 348 ms 70560 KB Output is correct
36 Correct 256 ms 60468 KB Output is correct
37 Correct 177 ms 50660 KB Output is correct
38 Correct 199 ms 48344 KB Output is correct
39 Correct 161 ms 47832 KB Output is correct
40 Correct 165 ms 43440 KB Output is correct
41 Correct 174 ms 41540 KB Output is correct
42 Correct 171 ms 42000 KB Output is correct
43 Correct 461 ms 82248 KB Output is correct
44 Correct 15 ms 23884 KB Output is correct
45 Correct 15 ms 23928 KB Output is correct
46 Correct 15 ms 23928 KB Output is correct
47 Correct 15 ms 23936 KB Output is correct
48 Correct 16 ms 23884 KB Output is correct
49 Incorrect 16 ms 23848 KB 3rd lines differ - on the 1st token, expected: '22140', found: '22156'
50 Halted 0 ms 0 KB -