Submission #295734

# Submission time Handle Problem Language Result Execution time Memory
295734 2020-09-09T20:59:07 Z shayan_p Ancient Books (IOI17_books) C++17
70 / 100
677 ms 93216 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 7 ms 12032 KB Output is correct
2 Correct 7 ms 12032 KB Output is correct
3 Correct 7 ms 12060 KB Output is correct
4 Correct 7 ms 12032 KB Output is correct
5 Correct 7 ms 12032 KB Output is correct
6 Correct 7 ms 12032 KB Output is correct
7 Correct 8 ms 12032 KB Output is correct
8 Correct 7 ms 12032 KB Output is correct
9 Correct 7 ms 12032 KB Output is correct
10 Correct 8 ms 12032 KB Output is correct
11 Correct 7 ms 12032 KB Output is correct
12 Correct 7 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 7 ms 12032 KB Output is correct
17 Correct 7 ms 12032 KB Output is correct
18 Correct 7 ms 12032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12032 KB Output is correct
2 Correct 7 ms 12032 KB Output is correct
3 Correct 7 ms 12060 KB Output is correct
4 Correct 7 ms 12032 KB Output is correct
5 Correct 7 ms 12032 KB Output is correct
6 Correct 7 ms 12032 KB Output is correct
7 Correct 8 ms 12032 KB Output is correct
8 Correct 7 ms 12032 KB Output is correct
9 Correct 7 ms 12032 KB Output is correct
10 Correct 8 ms 12032 KB Output is correct
11 Correct 7 ms 12032 KB Output is correct
12 Correct 7 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 7 ms 12032 KB Output is correct
17 Correct 7 ms 12032 KB Output is correct
18 Correct 7 ms 12032 KB Output is correct
19 Correct 8 ms 12032 KB Output is correct
20 Correct 7 ms 12032 KB Output is correct
21 Correct 8 ms 12160 KB Output is correct
22 Correct 7 ms 12160 KB Output is correct
23 Correct 7 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 7 ms 12088 KB Output is correct
27 Correct 7 ms 12032 KB Output is correct
28 Correct 7 ms 12032 KB Output is correct
29 Correct 9 ms 12160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12032 KB Output is correct
2 Correct 7 ms 12032 KB Output is correct
3 Correct 7 ms 12060 KB Output is correct
4 Correct 7 ms 12032 KB Output is correct
5 Correct 7 ms 12032 KB Output is correct
6 Correct 7 ms 12032 KB Output is correct
7 Correct 8 ms 12032 KB Output is correct
8 Correct 7 ms 12032 KB Output is correct
9 Correct 7 ms 12032 KB Output is correct
10 Correct 8 ms 12032 KB Output is correct
11 Correct 7 ms 12032 KB Output is correct
12 Correct 7 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 7 ms 12032 KB Output is correct
17 Correct 7 ms 12032 KB Output is correct
18 Correct 7 ms 12032 KB Output is correct
19 Correct 8 ms 12032 KB Output is correct
20 Correct 7 ms 12032 KB Output is correct
21 Correct 8 ms 12160 KB Output is correct
22 Correct 7 ms 12160 KB Output is correct
23 Correct 7 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 7 ms 12088 KB Output is correct
27 Correct 7 ms 12032 KB Output is correct
28 Correct 7 ms 12032 KB Output is correct
29 Correct 9 ms 12160 KB Output is correct
30 Correct 288 ms 26672 KB Output is correct
31 Correct 282 ms 26744 KB Output is correct
32 Correct 677 ms 93216 KB Output is correct
33 Correct 527 ms 72288 KB Output is correct
34 Correct 524 ms 72288 KB Output is correct
35 Correct 439 ms 62368 KB Output is correct
36 Correct 303 ms 42152 KB Output is correct
37 Correct 223 ms 28592 KB Output is correct
38 Correct 221 ms 27000 KB Output is correct
39 Correct 220 ms 27000 KB Output is correct
40 Correct 219 ms 26616 KB Output is correct
41 Correct 242 ms 26616 KB Output is correct
42 Correct 232 ms 26616 KB Output is correct
43 Correct 594 ms 76000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12160 KB Output is correct
2 Correct 8 ms 12160 KB Output is correct
3 Correct 7 ms 12160 KB Output is correct
4 Correct 7 ms 12160 KB Output is correct
5 Correct 7 ms 12160 KB Output is correct
6 Correct 7 ms 12160 KB Output is correct
7 Correct 7 ms 12160 KB Output is correct
8 Correct 7 ms 12160 KB Output is correct
9 Correct 7 ms 12288 KB Output is correct
10 Correct 7 ms 12160 KB Output is correct
11 Correct 7 ms 12160 KB Output is correct
12 Correct 7 ms 12032 KB Output is correct
13 Correct 7 ms 12032 KB Output is correct
14 Correct 7 ms 12032 KB Output is correct
15 Correct 7 ms 12032 KB Output is correct
16 Correct 7 ms 12032 KB Output is correct
17 Correct 9 ms 12160 KB Output is correct
18 Correct 8 ms 12160 KB Output is correct
19 Correct 7 ms 12032 KB Output is correct
20 Correct 7 ms 12032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12032 KB Output is correct
2 Correct 7 ms 12032 KB Output is correct
3 Correct 7 ms 12060 KB Output is correct
4 Correct 7 ms 12032 KB Output is correct
5 Correct 7 ms 12032 KB Output is correct
6 Correct 7 ms 12032 KB Output is correct
7 Correct 8 ms 12032 KB Output is correct
8 Correct 7 ms 12032 KB Output is correct
9 Correct 7 ms 12032 KB Output is correct
10 Correct 8 ms 12032 KB Output is correct
11 Correct 7 ms 12032 KB Output is correct
12 Correct 7 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 7 ms 12032 KB Output is correct
17 Correct 7 ms 12032 KB Output is correct
18 Correct 7 ms 12032 KB Output is correct
19 Correct 8 ms 12032 KB Output is correct
20 Correct 7 ms 12032 KB Output is correct
21 Correct 8 ms 12160 KB Output is correct
22 Correct 7 ms 12160 KB Output is correct
23 Correct 7 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 7 ms 12088 KB Output is correct
27 Correct 7 ms 12032 KB Output is correct
28 Correct 7 ms 12032 KB Output is correct
29 Correct 9 ms 12160 KB Output is correct
30 Correct 288 ms 26672 KB Output is correct
31 Correct 282 ms 26744 KB Output is correct
32 Correct 677 ms 93216 KB Output is correct
33 Correct 527 ms 72288 KB Output is correct
34 Correct 524 ms 72288 KB Output is correct
35 Correct 439 ms 62368 KB Output is correct
36 Correct 303 ms 42152 KB Output is correct
37 Correct 223 ms 28592 KB Output is correct
38 Correct 221 ms 27000 KB Output is correct
39 Correct 220 ms 27000 KB Output is correct
40 Correct 219 ms 26616 KB Output is correct
41 Correct 242 ms 26616 KB Output is correct
42 Correct 232 ms 26616 KB Output is correct
43 Correct 594 ms 76000 KB Output is correct
44 Correct 9 ms 12160 KB Output is correct
45 Correct 8 ms 12160 KB Output is correct
46 Correct 7 ms 12160 KB Output is correct
47 Correct 7 ms 12160 KB Output is correct
48 Correct 7 ms 12160 KB Output is correct
49 Correct 7 ms 12160 KB Output is correct
50 Correct 7 ms 12160 KB Output is correct
51 Correct 7 ms 12160 KB Output is correct
52 Correct 7 ms 12288 KB Output is correct
53 Correct 7 ms 12160 KB Output is correct
54 Correct 7 ms 12160 KB Output is correct
55 Correct 7 ms 12032 KB Output is correct
56 Correct 7 ms 12032 KB Output is correct
57 Correct 7 ms 12032 KB Output is correct
58 Correct 7 ms 12032 KB Output is correct
59 Correct 7 ms 12032 KB Output is correct
60 Correct 9 ms 12160 KB Output is correct
61 Correct 8 ms 12160 KB Output is correct
62 Correct 7 ms 12032 KB Output is correct
63 Correct 7 ms 12032 KB Output is correct
64 Correct 460 ms 74776 KB Output is correct
65 Correct 570 ms 80156 KB Output is correct
66 Correct 229 ms 28660 KB Output is correct
67 Correct 219 ms 27640 KB Output is correct
68 Correct 36 ms 15232 KB Output is correct
69 Correct 31 ms 14200 KB Output is correct
70 Correct 38 ms 15732 KB Output is correct
71 Incorrect 47 ms 17776 KB 3rd lines differ - on the 1st token, expected: '1845260', found: '1845262'
72 Halted 0 ms 0 KB -