Submission #390671

# Submission time Handle Problem Language Result Execution time Memory
390671 2021-04-16T13:33:28 Z Keshi Ancient Books (IOI17_books) C++17
70 / 100
2000 ms 152632 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 lg = 20;
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], e[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[lg][maxn];

void bld(ll n){
	for(ll i = 0; i < n; i++){
		seg[0][i] = b[i];
	}
	for(ll j = 1; j < lg; j++){
		for(ll i = 0; i + (1 << j) <= n; i++){
			seg[j][i] = max(seg[j - 1][i], seg[j - 1][i + (1 << (j - 1))]);
		}
	}
}
ll get(ll l, ll r){
	ll f = e[r - l];
	return max(seg[f][l], seg[f][r - (1 << f)]);
}

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;
		if(mnp[v] == 0){
//			cout << "$ " << u << "    " << mnp[u] << " " << mxp[u] << "\n";
		}
		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;
//	if(!val[g[v][x]]) cout << v << " [" << mnp[v] << ", " << mxp[v] << "]    ----->   " << ans << "\n";
	if(!val[g[v][x]]) return ans;
	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);
//	cout << "# " << v << " [" << mnp[v] << ", " << mxp[v] << "]    ----->   ";
	if(mnp[v] == -1){
//		cout << ans + Sz(g[v]) - 1 << "\n";
		return ans + Sz(g[v]) - 1;
	}
//	cout << ans + y << "\n";
	return ans + y;
}

long long minimum_walk(vector<int> p, int s){
	for(ll i = 2; i < maxn; i++){
		e[i] = e[i / 2] + 1;
	}
	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--;
	if(r <= l) return 0;
	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{
				cout.flush();
				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(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(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 17 ms 27724 KB Output is correct
2 Correct 19 ms 27724 KB Output is correct
3 Correct 19 ms 27724 KB Output is correct
4 Correct 18 ms 27676 KB Output is correct
5 Correct 18 ms 27724 KB Output is correct
6 Correct 20 ms 27784 KB Output is correct
7 Correct 19 ms 27828 KB Output is correct
8 Correct 19 ms 27812 KB Output is correct
9 Correct 19 ms 27716 KB Output is correct
10 Correct 19 ms 27768 KB Output is correct
11 Correct 18 ms 27724 KB Output is correct
12 Correct 18 ms 27724 KB Output is correct
13 Correct 18 ms 27724 KB Output is correct
14 Correct 18 ms 27664 KB Output is correct
15 Correct 19 ms 27740 KB Output is correct
16 Correct 18 ms 27812 KB Output is correct
17 Correct 20 ms 27724 KB Output is correct
18 Correct 19 ms 27764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 27724 KB Output is correct
2 Correct 19 ms 27724 KB Output is correct
3 Correct 19 ms 27724 KB Output is correct
4 Correct 18 ms 27676 KB Output is correct
5 Correct 18 ms 27724 KB Output is correct
6 Correct 20 ms 27784 KB Output is correct
7 Correct 19 ms 27828 KB Output is correct
8 Correct 19 ms 27812 KB Output is correct
9 Correct 19 ms 27716 KB Output is correct
10 Correct 19 ms 27768 KB Output is correct
11 Correct 18 ms 27724 KB Output is correct
12 Correct 18 ms 27724 KB Output is correct
13 Correct 18 ms 27724 KB Output is correct
14 Correct 18 ms 27664 KB Output is correct
15 Correct 19 ms 27740 KB Output is correct
16 Correct 18 ms 27812 KB Output is correct
17 Correct 20 ms 27724 KB Output is correct
18 Correct 19 ms 27764 KB Output is correct
19 Correct 20 ms 27896 KB Output is correct
20 Correct 18 ms 27852 KB Output is correct
21 Correct 19 ms 27624 KB Output is correct
22 Correct 20 ms 27852 KB Output is correct
23 Correct 21 ms 27852 KB Output is correct
24 Correct 19 ms 27852 KB Output is correct
25 Correct 19 ms 27852 KB Output is correct
26 Correct 18 ms 27816 KB Output is correct
27 Correct 19 ms 27852 KB Output is correct
28 Correct 18 ms 27896 KB Output is correct
29 Correct 23 ms 27940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 27724 KB Output is correct
2 Correct 19 ms 27724 KB Output is correct
3 Correct 19 ms 27724 KB Output is correct
4 Correct 18 ms 27676 KB Output is correct
5 Correct 18 ms 27724 KB Output is correct
6 Correct 20 ms 27784 KB Output is correct
7 Correct 19 ms 27828 KB Output is correct
8 Correct 19 ms 27812 KB Output is correct
9 Correct 19 ms 27716 KB Output is correct
10 Correct 19 ms 27768 KB Output is correct
11 Correct 18 ms 27724 KB Output is correct
12 Correct 18 ms 27724 KB Output is correct
13 Correct 18 ms 27724 KB Output is correct
14 Correct 18 ms 27664 KB Output is correct
15 Correct 19 ms 27740 KB Output is correct
16 Correct 18 ms 27812 KB Output is correct
17 Correct 20 ms 27724 KB Output is correct
18 Correct 19 ms 27764 KB Output is correct
19 Correct 20 ms 27896 KB Output is correct
20 Correct 18 ms 27852 KB Output is correct
21 Correct 19 ms 27624 KB Output is correct
22 Correct 20 ms 27852 KB Output is correct
23 Correct 21 ms 27852 KB Output is correct
24 Correct 19 ms 27852 KB Output is correct
25 Correct 19 ms 27852 KB Output is correct
26 Correct 18 ms 27816 KB Output is correct
27 Correct 19 ms 27852 KB Output is correct
28 Correct 18 ms 27896 KB Output is correct
29 Correct 23 ms 27940 KB Output is correct
30 Correct 286 ms 110808 KB Output is correct
31 Correct 273 ms 111804 KB Output is correct
32 Correct 135 ms 36520 KB Output is correct
33 Correct 310 ms 148412 KB Output is correct
34 Correct 311 ms 148492 KB Output is correct
35 Correct 311 ms 141088 KB Output is correct
36 Correct 255 ms 130856 KB Output is correct
37 Correct 241 ms 121076 KB Output is correct
38 Correct 221 ms 118856 KB Output is correct
39 Correct 221 ms 118340 KB Output is correct
40 Correct 228 ms 113900 KB Output is correct
41 Correct 234 ms 111944 KB Output is correct
42 Correct 229 ms 112400 KB Output is correct
43 Correct 346 ms 152632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 27852 KB Output is correct
2 Correct 21 ms 27920 KB Output is correct
3 Correct 22 ms 27852 KB Output is correct
4 Correct 20 ms 27888 KB Output is correct
5 Correct 20 ms 27852 KB Output is correct
6 Correct 19 ms 27880 KB Output is correct
7 Correct 19 ms 27852 KB Output is correct
8 Correct 19 ms 27852 KB Output is correct
9 Correct 20 ms 27872 KB Output is correct
10 Correct 19 ms 27620 KB Output is correct
11 Correct 18 ms 27864 KB Output is correct
12 Correct 19 ms 27904 KB Output is correct
13 Correct 19 ms 27852 KB Output is correct
14 Correct 21 ms 27896 KB Output is correct
15 Correct 19 ms 27852 KB Output is correct
16 Correct 19 ms 27820 KB Output is correct
17 Correct 20 ms 27892 KB Output is correct
18 Correct 19 ms 27840 KB Output is correct
19 Correct 18 ms 27904 KB Output is correct
20 Correct 19 ms 27852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 27724 KB Output is correct
2 Correct 19 ms 27724 KB Output is correct
3 Correct 19 ms 27724 KB Output is correct
4 Correct 18 ms 27676 KB Output is correct
5 Correct 18 ms 27724 KB Output is correct
6 Correct 20 ms 27784 KB Output is correct
7 Correct 19 ms 27828 KB Output is correct
8 Correct 19 ms 27812 KB Output is correct
9 Correct 19 ms 27716 KB Output is correct
10 Correct 19 ms 27768 KB Output is correct
11 Correct 18 ms 27724 KB Output is correct
12 Correct 18 ms 27724 KB Output is correct
13 Correct 18 ms 27724 KB Output is correct
14 Correct 18 ms 27664 KB Output is correct
15 Correct 19 ms 27740 KB Output is correct
16 Correct 18 ms 27812 KB Output is correct
17 Correct 20 ms 27724 KB Output is correct
18 Correct 19 ms 27764 KB Output is correct
19 Correct 20 ms 27896 KB Output is correct
20 Correct 18 ms 27852 KB Output is correct
21 Correct 19 ms 27624 KB Output is correct
22 Correct 20 ms 27852 KB Output is correct
23 Correct 21 ms 27852 KB Output is correct
24 Correct 19 ms 27852 KB Output is correct
25 Correct 19 ms 27852 KB Output is correct
26 Correct 18 ms 27816 KB Output is correct
27 Correct 19 ms 27852 KB Output is correct
28 Correct 18 ms 27896 KB Output is correct
29 Correct 23 ms 27940 KB Output is correct
30 Correct 286 ms 110808 KB Output is correct
31 Correct 273 ms 111804 KB Output is correct
32 Correct 135 ms 36520 KB Output is correct
33 Correct 310 ms 148412 KB Output is correct
34 Correct 311 ms 148492 KB Output is correct
35 Correct 311 ms 141088 KB Output is correct
36 Correct 255 ms 130856 KB Output is correct
37 Correct 241 ms 121076 KB Output is correct
38 Correct 221 ms 118856 KB Output is correct
39 Correct 221 ms 118340 KB Output is correct
40 Correct 228 ms 113900 KB Output is correct
41 Correct 234 ms 111944 KB Output is correct
42 Correct 229 ms 112400 KB Output is correct
43 Correct 346 ms 152632 KB Output is correct
44 Correct 19 ms 27852 KB Output is correct
45 Correct 21 ms 27920 KB Output is correct
46 Correct 22 ms 27852 KB Output is correct
47 Correct 20 ms 27888 KB Output is correct
48 Correct 20 ms 27852 KB Output is correct
49 Correct 19 ms 27880 KB Output is correct
50 Correct 19 ms 27852 KB Output is correct
51 Correct 19 ms 27852 KB Output is correct
52 Correct 20 ms 27872 KB Output is correct
53 Correct 19 ms 27620 KB Output is correct
54 Correct 18 ms 27864 KB Output is correct
55 Correct 19 ms 27904 KB Output is correct
56 Correct 19 ms 27852 KB Output is correct
57 Correct 21 ms 27896 KB Output is correct
58 Correct 19 ms 27852 KB Output is correct
59 Correct 19 ms 27820 KB Output is correct
60 Correct 20 ms 27892 KB Output is correct
61 Correct 19 ms 27840 KB Output is correct
62 Correct 18 ms 27904 KB Output is correct
63 Correct 19 ms 27852 KB Output is correct
64 Correct 309 ms 147540 KB Output is correct
65 Execution timed out 2090 ms 150300 KB Time limit exceeded
66 Halted 0 ms 0 KB -