Submission #295735

# Submission time Handle Problem Language Result Execution time Memory
295735 2020-09-09T21:00:29 Z shayan_p Ancient Books (IOI17_books) C++17
50 / 100
680 ms 86500 KB
// Oh damn! Suddenly you're free to fly...
 
#include<bits/stdc++.h>
#include "books.h"
 
#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> pii;
 
const int maxn = 1e6 + 10, mod = 1e9 + 7, inf = 1e9 + 10;
 
int pr[maxn], L[maxn], R[maxn];

int fnd(int u){
    return pr[u] < 0 ? u : pr[u] = fnd(pr[u]);
}
void mrg(int a, int b){
    if( (a = fnd(a)) == (b = fnd(b)) )
	return;
    if(pr[a] > pr[b])
	swap(a, b);
    L[a] = min(L[a], L[b]);
    R[a] = max(R[a], R[b]);
    pr[a]+= pr[b];
    pr[b] = a;	    
}
 
ll minimum_walk(vector<int> p, int s){
    memset(pr, -1, sizeof pr);
    
    iota(L, L + maxn, 0);
    iota(R, R + maxn, 0);
    
    int n = sz(p);
    ll ans = 0;
 
    for(int i = 0; i < n; i++){
	int l = i, r = p[i];
	if(l > r)
	    swap(l, r);
	ans+= r-l;
	mrg(l, r);
    }
    vector<int> vec;
    for(int i = 0; i < n; i++){
	if(fnd(i) == i)
	    vec.PB(i);
    }
    sort(vec.begin(), vec.end(), [&](int i, int j){ return L[fnd(i)] > L[fnd(j)]; });
    map<pii, int> segs;
    
    for(int id : vec){
	int pos = R[fnd(id)];
	while(true){
	    auto it = segs.upper_bound({pos, inf});
	    if(it == segs.begin())
		break;
	    --it;
	    if(pos <= (it->F.S)){
		mrg(id, it->S);
		segs.erase(it);
	    }
	    else{
		break;
	    }
	}
	segs[{L[fnd(id)], R[fnd(id)]}] = id;
    }
    
    int l = s, r = s;
    while(true){
	while(true){
	    bool is = 0;
	    while(r < R[fnd(s)])
		mrg(s, ++r), is = 1;
	    while(l > L[fnd(s)])
		mrg(s, --l), is = 1;
	    if(!is)
		break;
	}
	int d1 = 0, d2 = 0;
	while(r < n && L[fnd(s)] <= L[fnd(r)]){
	    d2+= R[fnd(s)] < r;
	    mrg(s, r);
	    r++;
	}
	while(l >= 0 && R[fnd(l)] <= R[fnd(s)]){
	    d1+= L[fnd(s)] > l;
	    mrg(s, l);
	    l--;
	}
	if(l == -1 || r == n){
	    assert(l == -1 && r == n);
	    ans+= 2 * (d1 + d2);
	    break;
	}
	else{
	    ans+= 2 * min(d1 + 1, d2 + 1);
	    assert(fnd(l) == fnd(r));
	    mrg(s, l);
	    mrg(s, r);
	}	    
    }
    for(int i = 0; i < s && p[i] == i; i++){
	ans-= 2;
    }
    for(int i = n-1; i > s && p[i] == i; i--){
	ans-= 2;
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12032 KB Output is correct
2 Correct 8 ms 12032 KB Output is correct
3 Correct 8 ms 12032 KB Output is correct
4 Correct 9 ms 12032 KB Output is correct
5 Correct 8 ms 12032 KB Output is correct
6 Correct 8 ms 12032 KB Output is correct
7 Correct 8 ms 12032 KB Output is correct
8 Correct 8 ms 12032 KB Output is correct
9 Correct 8 ms 12032 KB Output is correct
10 Correct 8 ms 12032 KB Output is correct
11 Correct 8 ms 12032 KB Output is correct
12 Correct 8 ms 12032 KB Output is correct
13 Correct 8 ms 12032 KB Output is correct
14 Correct 8 ms 12032 KB Output is correct
15 Correct 8 ms 12032 KB Output is correct
16 Correct 8 ms 12032 KB Output is correct
17 Correct 9 ms 12032 KB Output is correct
18 Correct 8 ms 12032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12032 KB Output is correct
2 Correct 8 ms 12032 KB Output is correct
3 Correct 8 ms 12032 KB Output is correct
4 Correct 9 ms 12032 KB Output is correct
5 Correct 8 ms 12032 KB Output is correct
6 Correct 8 ms 12032 KB Output is correct
7 Correct 8 ms 12032 KB Output is correct
8 Correct 8 ms 12032 KB Output is correct
9 Correct 8 ms 12032 KB Output is correct
10 Correct 8 ms 12032 KB Output is correct
11 Correct 8 ms 12032 KB Output is correct
12 Correct 8 ms 12032 KB Output is correct
13 Correct 8 ms 12032 KB Output is correct
14 Correct 8 ms 12032 KB Output is correct
15 Correct 8 ms 12032 KB Output is correct
16 Correct 8 ms 12032 KB Output is correct
17 Correct 9 ms 12032 KB Output is correct
18 Correct 8 ms 12032 KB Output is correct
19 Correct 8 ms 12032 KB Output is correct
20 Correct 9 ms 12032 KB Output is correct
21 Correct 9 ms 12160 KB Output is correct
22 Correct 9 ms 12160 KB Output is correct
23 Correct 8 ms 12160 KB Output is correct
24 Correct 8 ms 12032 KB Output is correct
25 Correct 8 ms 12032 KB Output is correct
26 Correct 8 ms 12032 KB Output is correct
27 Correct 8 ms 12032 KB Output is correct
28 Correct 8 ms 12032 KB Output is correct
29 Correct 8 ms 12160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12032 KB Output is correct
2 Correct 8 ms 12032 KB Output is correct
3 Correct 8 ms 12032 KB Output is correct
4 Correct 9 ms 12032 KB Output is correct
5 Correct 8 ms 12032 KB Output is correct
6 Correct 8 ms 12032 KB Output is correct
7 Correct 8 ms 12032 KB Output is correct
8 Correct 8 ms 12032 KB Output is correct
9 Correct 8 ms 12032 KB Output is correct
10 Correct 8 ms 12032 KB Output is correct
11 Correct 8 ms 12032 KB Output is correct
12 Correct 8 ms 12032 KB Output is correct
13 Correct 8 ms 12032 KB Output is correct
14 Correct 8 ms 12032 KB Output is correct
15 Correct 8 ms 12032 KB Output is correct
16 Correct 8 ms 12032 KB Output is correct
17 Correct 9 ms 12032 KB Output is correct
18 Correct 8 ms 12032 KB Output is correct
19 Correct 8 ms 12032 KB Output is correct
20 Correct 9 ms 12032 KB Output is correct
21 Correct 9 ms 12160 KB Output is correct
22 Correct 9 ms 12160 KB Output is correct
23 Correct 8 ms 12160 KB Output is correct
24 Correct 8 ms 12032 KB Output is correct
25 Correct 8 ms 12032 KB Output is correct
26 Correct 8 ms 12032 KB Output is correct
27 Correct 8 ms 12032 KB Output is correct
28 Correct 8 ms 12032 KB Output is correct
29 Correct 8 ms 12160 KB Output is correct
30 Correct 293 ms 19960 KB Output is correct
31 Correct 320 ms 19904 KB Output is correct
32 Correct 680 ms 86500 KB Output is correct
33 Correct 534 ms 65504 KB Output is correct
34 Correct 525 ms 65760 KB Output is correct
35 Correct 444 ms 55540 KB Output is correct
36 Correct 305 ms 35564 KB Output is correct
37 Correct 230 ms 21876 KB Output is correct
38 Correct 220 ms 20344 KB Output is correct
39 Correct 220 ms 20216 KB Output is correct
40 Correct 222 ms 19960 KB Output is correct
41 Correct 247 ms 19960 KB Output is correct
42 Correct 237 ms 19832 KB Output is correct
43 Correct 601 ms 69216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12160 KB Output is correct
2 Runtime error 24 ms 24320 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12032 KB Output is correct
2 Correct 8 ms 12032 KB Output is correct
3 Correct 8 ms 12032 KB Output is correct
4 Correct 9 ms 12032 KB Output is correct
5 Correct 8 ms 12032 KB Output is correct
6 Correct 8 ms 12032 KB Output is correct
7 Correct 8 ms 12032 KB Output is correct
8 Correct 8 ms 12032 KB Output is correct
9 Correct 8 ms 12032 KB Output is correct
10 Correct 8 ms 12032 KB Output is correct
11 Correct 8 ms 12032 KB Output is correct
12 Correct 8 ms 12032 KB Output is correct
13 Correct 8 ms 12032 KB Output is correct
14 Correct 8 ms 12032 KB Output is correct
15 Correct 8 ms 12032 KB Output is correct
16 Correct 8 ms 12032 KB Output is correct
17 Correct 9 ms 12032 KB Output is correct
18 Correct 8 ms 12032 KB Output is correct
19 Correct 8 ms 12032 KB Output is correct
20 Correct 9 ms 12032 KB Output is correct
21 Correct 9 ms 12160 KB Output is correct
22 Correct 9 ms 12160 KB Output is correct
23 Correct 8 ms 12160 KB Output is correct
24 Correct 8 ms 12032 KB Output is correct
25 Correct 8 ms 12032 KB Output is correct
26 Correct 8 ms 12032 KB Output is correct
27 Correct 8 ms 12032 KB Output is correct
28 Correct 8 ms 12032 KB Output is correct
29 Correct 8 ms 12160 KB Output is correct
30 Correct 293 ms 19960 KB Output is correct
31 Correct 320 ms 19904 KB Output is correct
32 Correct 680 ms 86500 KB Output is correct
33 Correct 534 ms 65504 KB Output is correct
34 Correct 525 ms 65760 KB Output is correct
35 Correct 444 ms 55540 KB Output is correct
36 Correct 305 ms 35564 KB Output is correct
37 Correct 230 ms 21876 KB Output is correct
38 Correct 220 ms 20344 KB Output is correct
39 Correct 220 ms 20216 KB Output is correct
40 Correct 222 ms 19960 KB Output is correct
41 Correct 247 ms 19960 KB Output is correct
42 Correct 237 ms 19832 KB Output is correct
43 Correct 601 ms 69216 KB Output is correct
44 Correct 8 ms 12160 KB Output is correct
45 Runtime error 24 ms 24320 KB Execution killed with signal 11
46 Halted 0 ms 0 KB -