Submission #638818

# Submission time Handle Problem Language Result Execution time Memory
638818 2022-09-07T14:49:53 Z qwerasdfzxcl Ancient Books (IOI17_books) C++17
100 / 100
577 ms 173756 KB
#include "books.h"
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
int  n, s, ts, ans, a[1001000], cycL[1001000], cycR[1001000], L[1001000], R[1001000], dp[1001000];
bool visited[1001000];
ll cycsum;

struct Seg{
    int tree[2002000], sz;
    void init(int n){
        sz = n;
        fill(tree, tree+sz*2, 1e9);
    }
    void update(int p, int x){
        for (tree[p+=sz]=x;p>1;p>>=1) tree[p>>1] = min(tree[p], tree[p^1]);
    }
    int query(int l, int r){
        int ret = 1e9;
        ++r;
        for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
            if (l&1) ret = min(ret, tree[l++]);
            if (r&1) ret = min(ret, tree[--r]);
        }
        return ret;
    }
}tree1, tree2;

ll myabs(ll x){
    if (x<0) return -x;
    return x;
}


void dfs(int l, int r){
    //printf("dfs: %d %d\n", l, r);
    if (l==L[ts] && r==R[ts]) {ans += dp[L[ts]]; return;}

    vector<pair<int, int>> child;
    for (int i=l+1;i<r;){
        if (R[i]<r){
            child.emplace_back(i, R[i]);
            i = R[i] + 1;
        }
        else i++;
    }

    vector<int> gl = {1}, gr = {1};
    for (int i=1;i<(int)child.size();i++){
        if (child[i-1].second+1==child[i].first) gl.push_back(gl.back()+1);
        else gl.push_back(1);
    }
    for (int i=(int)child.size()-2;i>=0;i--){
        if (child[i].second+1==child[i+1].first) gr.push_back(gr.back()+1);
        else gr.push_back(1);
    }
    reverse(gr.begin(), gr.end());

    for (int i=0;i<(int)child.size();i++) dp[child[i].first] = min(gl[i], gr[i]) + dp[l];
    if (l==0) for (int i=0;i<(int)child.size();i++) dp[child[i].first] = (int)child.size()-1;

    for (auto &[nl, nr]:child) dfs(nl, nr);
}

map<pair<int, int>, int> mp;
void naive(int s){
    int ptl = s, ptr = s;
    while(true){
        int nl = tree1.query(ptl, ptr);
        int nr = -tree2.query(ptl, ptr);

        if (nl==ptl && nr==ptr) {L[s] = nl, R[s] = nr; break;}
        /*if (mp.find(make_pair(nl, nr))!=mp.end()) {
            int tmp = mp[make_pair(nl, nr)];
            L[s] = L[tmp], R[s] = R[tmp];
            break;
        }*/

        mp[make_pair(nl, nr)] = s;
        ptl = nl, ptr = nr;

    }
}

long long minimum_walk(std::vector<int> P, int S) {
    int si = 0, ei = (int)P.size()-1;
    for(;si<(int)P.size() && P[si]==si;si++);
    for(;ei>=0 && P[ei]==ei;ei--);

    if (si > ei) return 0;
    n = ei-si+1;
    for (int i=si;i<=ei;i++) a[i-si+1] = P[i] - si + 1;
    s = S-si+1;

    tree1.init(n+1);
    tree2.init(n+1);
    for (int i=1;i<=n;i++) if (!visited[i]){
        vector<int> cyc;

        for (int j=i;!visited[j];j=a[j]){
            cyc.push_back(j);
            visited[j] = 1;
            cycR[i] = max(cycR[i], j);
            cycsum += myabs(j - a[j]);
        }
        cycL[cycR[i]] = i;

        for (auto &x:cyc){
            tree1.update(x, i);
            tree2.update(x, -cycR[i]);
        }
    }

    ///calc range
    for (int i=1;i<=n;i++) naive(i);

    /*for (int i=1;i<=n;i++) printf("(%d, %d) ", cycL[i], cycR[i]);
    printf("\n");
    for (int i=1;i<=n;i++) printf("(%d, %d) ", L[i], R[i]);
    printf("\n");*/

    if (s<=0) ts = 1, ans = 1-s;
    else if (s>n) ts = n, ans = s-n;
    else ts = s, ans = 0;

    //printf("ok n = %d, s = %d\n", n, s);
    dfs(0, n+1);

    //printf("%lld + %d * 2\n", cycsum, ans);
    return cycsum + ans * 2;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 296 KB Output is correct
8 Correct 0 ms 300 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 304 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 308 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 296 KB Output is correct
8 Correct 0 ms 300 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 304 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 308 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 304 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 312 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 308 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 296 KB Output is correct
8 Correct 0 ms 300 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 304 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 308 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 304 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 312 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 308 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 577 ms 38976 KB Output is correct
31 Correct 512 ms 39200 KB Output is correct
32 Correct 83 ms 8652 KB Output is correct
33 Correct 315 ms 75568 KB Output is correct
34 Correct 308 ms 75512 KB Output is correct
35 Correct 324 ms 72660 KB Output is correct
36 Correct 349 ms 63336 KB Output is correct
37 Correct 290 ms 50980 KB Output is correct
38 Correct 291 ms 47296 KB Output is correct
39 Correct 320 ms 46336 KB Output is correct
40 Correct 285 ms 40092 KB Output is correct
41 Correct 421 ms 38444 KB Output is correct
42 Correct 343 ms 38824 KB Output is correct
43 Correct 342 ms 60544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 428 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 296 KB Output is correct
8 Correct 0 ms 300 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 304 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 308 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 304 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 312 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 308 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 577 ms 38976 KB Output is correct
31 Correct 512 ms 39200 KB Output is correct
32 Correct 83 ms 8652 KB Output is correct
33 Correct 315 ms 75568 KB Output is correct
34 Correct 308 ms 75512 KB Output is correct
35 Correct 324 ms 72660 KB Output is correct
36 Correct 349 ms 63336 KB Output is correct
37 Correct 290 ms 50980 KB Output is correct
38 Correct 291 ms 47296 KB Output is correct
39 Correct 320 ms 46336 KB Output is correct
40 Correct 285 ms 40092 KB Output is correct
41 Correct 421 ms 38444 KB Output is correct
42 Correct 343 ms 38824 KB Output is correct
43 Correct 342 ms 60544 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 308 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 428 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 1 ms 468 KB Output is correct
55 Correct 1 ms 468 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 1 ms 468 KB Output is correct
61 Correct 1 ms 468 KB Output is correct
62 Correct 1 ms 340 KB Output is correct
63 Correct 1 ms 308 KB Output is correct
64 Correct 312 ms 56564 KB Output is correct
65 Correct 297 ms 55660 KB Output is correct
66 Correct 288 ms 50272 KB Output is correct
67 Correct 304 ms 49580 KB Output is correct
68 Correct 32 ms 6556 KB Output is correct
69 Correct 30 ms 6136 KB Output is correct
70 Correct 32 ms 6284 KB Output is correct
71 Correct 30 ms 6596 KB Output is correct
72 Correct 27 ms 6356 KB Output is correct
73 Correct 32 ms 5068 KB Output is correct
74 Correct 310 ms 75376 KB Output is correct
75 Correct 332 ms 75380 KB Output is correct
76 Correct 392 ms 64576 KB Output is correct
77 Correct 398 ms 64420 KB Output is correct
78 Correct 438 ms 71768 KB Output is correct
79 Correct 437 ms 71736 KB Output is correct
80 Correct 83 ms 8524 KB Output is correct
81 Correct 564 ms 173756 KB Output is correct
82 Correct 572 ms 173704 KB Output is correct
83 Correct 390 ms 78028 KB Output is correct
84 Correct 375 ms 72212 KB Output is correct
85 Correct 331 ms 59004 KB Output is correct
86 Correct 300 ms 52528 KB Output is correct
87 Correct 427 ms 111308 KB Output is correct
88 Correct 442 ms 103916 KB Output is correct
89 Correct 460 ms 84100 KB Output is correct
90 Correct 296 ms 50036 KB Output is correct
91 Correct 340 ms 49356 KB Output is correct
92 Correct 278 ms 40620 KB Output is correct