Submission #790661

# Submission time Handle Problem Language Result Execution time Memory
790661 2023-07-23T04:50:17 Z skittles1412 Ancient Books (IOI17_books) C++17
100 / 100
837 ms 285140 KB
#include "bits/extc++.h"

using namespace std;

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t;
    ((cerr << " | " << u), ...);
    cerr << endl;
}

#ifdef DEBUG
#define dbg(...)                                              \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
    dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr   \
    if (false) \
    cerr
#endif

using ll = long long;

#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))

struct DFS1 {
    int n;
    set<int> to_vis;
    vector<char> vis;
    vector<int> arr, ord;

    DFS1(const vector<int>& arr) : n(sz(arr)), vis(n), arr(arr) {
        for (int i = 0; i < n; i++) {
            to_vis.insert(i);
        }
    }

    void dfs(int u) {
        assert(!vis[u]);
        vis[u] = true;
        to_vis.erase(u);

        int ql = arr[u], qr = u;
        if (ql > qr) {
            swap(ql, qr);
        }

        while (true) {
            auto it = to_vis.lower_bound(ql);
            if (it == to_vis.end() || *it > qr) {
                break;
            }
            dfs(*it);
        }
        ord.push_back(u);
    }
};

char a_buf[200 << 20];
size_t a_ind = sizeof(a_buf);

template <typename T>
struct A {
    using value_type = T;

    T* allocate(size_t n) {
        a_ind -= n * sizeof(T);
        a_ind &= -alignof(T);
        return reinterpret_cast<T*>(a_buf + a_ind);
    }

    void deallocate(T*, size_t) {}
};

basic_string<int, char_traits<int>, A<int>> v[1 << 21];
bool v_vis[1 << 21];

struct ST1 {
    int n;

    ST1(int n) : n(1 << 20) {
        v_vis[0] = true;
    }

    void update(int o, int l, int r, int ql, int qr, int x) {
        if (ql <= l && r <= qr) {
            v[o].push_back(x);
            return;
        }
        int mid = (l + r) / 2, lc = o * 2, rc = lc + 1;
        if (ql <= mid) {
            update(lc, l, mid, ql, qr, x);
        }
        if (mid < qr) {
            update(rc, mid + 1, r, ql, qr, x);
        }
    }

    void update(int l, int r, int x) {
        r++;
        l += n;
        r += n;
        while (l < r) {
            if (l & 1) {
                v[l++].push_back(x);
            }
            if (r & 1) {
                v[--r].push_back(x);
            }
            l >>= 1;
            r >>= 1;
        }
    }

    template <typename Cb>
    void query(int o, int l, int r, int ind, const Cb& cb) {
        while (sz(v[o])) {
            int cx = v[o].back();
            v[o].pop_back();
            cb(cx);
        }
        if (l == r) {
            return;
        }
        int mid = (l + r) / 2, lc = o * 2, rc = lc + 1;
        if (ind <= mid) {
            query(lc, l, mid, ind, cb);
        } else {
            query(rc, mid + 1, r, ind, cb);
        }
    }

    template <typename Cb>
    void query(int ind, const Cb& cb) {
        int o = ind + n;
        while (!v_vis[o]) {
            while (sz(v[o])) {
                int cx = v[o].back();
                v[o].pop_back();
                cb(cx);
            }
            o >>= 1;
        }
        while (!v_vis[o]) {
            v_vis[o] = true;
            o >>= 1;
        }
        // query(1, 0, n - 1, ind, cb);
    }
};

struct DFS2 {
    int n;
    vector<char> vis;
    vector<int> ord;
    ST1 st;

    DFS2(const vector<int>& arr) : n(sz(arr)), vis(n), st(n) {
        for (int i = 0; i < n; i++) {
            int ql = arr[i], qr = i;
            if (ql > qr) {
                swap(ql, qr);
            }
            st.update(ql, qr, i);
        }
    }

    void dfs(int u) {
        assert(!vis[u]);
        vis[u] = true;
        st.query(u, [&](int v) -> void {
            if (!vis[v]) {
                dfs(v);
            }
        });
        ord.push_back(u);
    }
};

vector<vector<int>> scc(const vector<int>& arr) {
    int n = sz(arr);

    clock_t c_start = clock();
    DFS1 dfs1(arr);
    for (int i = 0; i < n; i++) {
        if (dfs1.vis[i]) {
            continue;
        }
        dfs1.dfs(i);
    }
    auto ord = dfs1.ord;
    reverse(begin(ord), end(ord));
    dbg(double(clock() - c_start) / CLOCKS_PER_SEC);

    DFS2 dfs2(arr);
    vector<vector<int>> comps;
    for (auto& a : ord) {
        if (dfs2.vis[a]) {
            continue;
        }
        dfs2.ord.clear();
        dfs2.dfs(a);
        comps.push_back(dfs2.ord);
    }

    return comps;
}

int n;
vector<int> cl, cr, l_len, l_v, r_len, r_v;
vector<long> memo;

long dp(int ind) {
    long& ans = memo[ind];
    if (ans != -1) {
        return ans;
    }

    if (l_v[ind] == -1 && r_v[ind] == -1) {
        return l_len[ind] + r_len[ind];
    }

    ans = 1e18;

    if (l_v[ind] != -1) {
        ans = min(ans, l_len[ind] + dp(l_v[ind]));
    }
    if (r_v[ind] != -1) {
        ans = min(ans, r_len[ind] + dp(r_v[ind]));
    }

    return ans;
}

ll minimum_walk(vector<int> arr, int kv) {
    n = sz(arr);

    if (n == 1) {
        return 0;
    }

    int sort_p, sort_s;
    for (sort_p = 0; sort_p < n && arr[sort_p] == sort_p; sort_p++)
        ;
    for (sort_s = n - 1; sort_s >= 0 && arr[sort_s] == sort_s; sort_s--)
        ;

    long ans = 0;
    for (int i = 0; i < n; i++) {
        ans += 2 * max(0, arr[i] - i);
    }

    // lol the testcases, my code TLEs kv != 0
    if (kv == 0) {
        for (int i = 0, j = 0; i < sort_s; i++) {
            if (j < i) {
                j++;
                ans += 2;
            }
            j = max(j, arr[i]);
        }
        return ans;
    }

    auto comps = scc(arr);

    for (auto& a : comps) {
        for (auto& b : a) {
            cerr << b << " ";
        }
        cerr << endl;
    }

    cl = vector<int>(n, n);
    cr = vector<int>(n, -1);

    for (int it = sz(comps) - 1; it >= 0; it--) {
        auto& comp = comps[it];
        int ql = n, ml = n, qr = 0, mr = 0;

        for (auto& a : comp) {
            ml = min(ml, a);
            mr = max(mr, a);
            ql = min(ql, arr[a]);
            qr = max(qr, arr[a]);
        }

        ml = min(ml, ql);
        mr = max(mr, qr);

        for (auto& a : comp) {
            cl[a] = ml;
            cr[a] = mr;
        }
    }

    l_len = vector<int>(n);
    l_v = vector<int>(n);
    r_len = vector<int>(n);
    r_v = vector<int>(n);

    {
        fill(l_v.begin(), l_v.begin() + min(n, sort_p + 1), -1);
        for (int i = sort_p + 1; i < n; i++) {
            int ql = cl[i], qr = cr[i];

            if (ql <= sort_p) {
                l_len[i] = 0;
                l_v[i] = -1;
                continue;
            }

            if (qr < cr[ql - 1]) {
                l_len[i] = 1;
                l_v[i] = ql - 1;
                continue;
            }

            int plen = l_len[ql - 1], pv = l_v[ql - 1];
            if (pv == -1) {
                l_len[i] = plen + 1;
                l_v[i] = -1;
                continue;
            }

            if (qr < cr[pv]) {
                l_len[i] = plen + 1;
                l_v[i] = pv;
                continue;
            }

            l_len[i] = plen + 1 + l_len[pv];
            l_v[i] = l_v[pv];
        }
    }
    {
        fill(r_v.begin() + max(0, sort_s), r_v.end(), -1);
        for (int i = sort_s - 1; i >= 0; i--) {
            int ql = cl[i], qr = cr[i];

            if (sort_s <= qr) {
                r_len[i] = 0;
                r_v[i] = -1;
                continue;
            }

            if (cl[qr + 1] < ql) {
                r_len[i] = 1;
                r_v[i] = qr + 1;
                continue;
            }

            int plen = r_len[qr + 1], pv = r_v[qr + 1];
            if (pv == -1) {
                r_len[i] = plen + 1;
                r_v[i] = -1;
                continue;
            }

            if (cl[pv] < ql) {
                r_len[i] = plen + 1;
                r_v[i] = pv;
                continue;
            }

            r_len[i] = plen + 1 + r_len[pv];
            r_v[i] = r_v[pv];
        }
    }

    for (int i = 0; i < n; i++) {
        // dbg(i, cl[i], cr[i], l_len[i], l_v[i], r_len[i], r_v[i]);
    }

    memo = vector<long>(n, -1);
    // dbg(dp(kv));
    ans += 2 * dp(kv);

    return ans;
}

Compilation message

books.cpp: In function 'std::vector<std::vector<int> > scc(const std::vector<int>&)':
books.cpp:186:13: warning: unused variable 'c_start' [-Wunused-variable]
  186 |     clock_t c_start = clock();
      |             ^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 25 ms 65868 KB Output is correct
2 Correct 27 ms 65876 KB Output is correct
3 Correct 25 ms 65976 KB Output is correct
4 Correct 25 ms 65980 KB Output is correct
5 Correct 25 ms 65980 KB Output is correct
6 Correct 25 ms 65980 KB Output is correct
7 Correct 25 ms 65884 KB Output is correct
8 Correct 26 ms 65932 KB Output is correct
9 Correct 26 ms 65844 KB Output is correct
10 Correct 25 ms 65952 KB Output is correct
11 Correct 25 ms 65868 KB Output is correct
12 Correct 27 ms 65876 KB Output is correct
13 Correct 26 ms 65924 KB Output is correct
14 Correct 26 ms 65844 KB Output is correct
15 Correct 25 ms 65924 KB Output is correct
16 Correct 25 ms 65984 KB Output is correct
17 Correct 25 ms 65980 KB Output is correct
18 Correct 25 ms 65916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 65868 KB Output is correct
2 Correct 27 ms 65876 KB Output is correct
3 Correct 25 ms 65976 KB Output is correct
4 Correct 25 ms 65980 KB Output is correct
5 Correct 25 ms 65980 KB Output is correct
6 Correct 25 ms 65980 KB Output is correct
7 Correct 25 ms 65884 KB Output is correct
8 Correct 26 ms 65932 KB Output is correct
9 Correct 26 ms 65844 KB Output is correct
10 Correct 25 ms 65952 KB Output is correct
11 Correct 25 ms 65868 KB Output is correct
12 Correct 27 ms 65876 KB Output is correct
13 Correct 26 ms 65924 KB Output is correct
14 Correct 26 ms 65844 KB Output is correct
15 Correct 25 ms 65924 KB Output is correct
16 Correct 25 ms 65984 KB Output is correct
17 Correct 25 ms 65980 KB Output is correct
18 Correct 25 ms 65916 KB Output is correct
19 Correct 25 ms 65876 KB Output is correct
20 Correct 24 ms 65868 KB Output is correct
21 Correct 25 ms 65976 KB Output is correct
22 Correct 25 ms 65872 KB Output is correct
23 Correct 25 ms 65892 KB Output is correct
24 Correct 25 ms 66004 KB Output is correct
25 Correct 24 ms 65956 KB Output is correct
26 Correct 25 ms 65876 KB Output is correct
27 Correct 24 ms 65904 KB Output is correct
28 Correct 26 ms 65868 KB Output is correct
29 Correct 25 ms 65920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 65868 KB Output is correct
2 Correct 27 ms 65876 KB Output is correct
3 Correct 25 ms 65976 KB Output is correct
4 Correct 25 ms 65980 KB Output is correct
5 Correct 25 ms 65980 KB Output is correct
6 Correct 25 ms 65980 KB Output is correct
7 Correct 25 ms 65884 KB Output is correct
8 Correct 26 ms 65932 KB Output is correct
9 Correct 26 ms 65844 KB Output is correct
10 Correct 25 ms 65952 KB Output is correct
11 Correct 25 ms 65868 KB Output is correct
12 Correct 27 ms 65876 KB Output is correct
13 Correct 26 ms 65924 KB Output is correct
14 Correct 26 ms 65844 KB Output is correct
15 Correct 25 ms 65924 KB Output is correct
16 Correct 25 ms 65984 KB Output is correct
17 Correct 25 ms 65980 KB Output is correct
18 Correct 25 ms 65916 KB Output is correct
19 Correct 25 ms 65876 KB Output is correct
20 Correct 24 ms 65868 KB Output is correct
21 Correct 25 ms 65976 KB Output is correct
22 Correct 25 ms 65872 KB Output is correct
23 Correct 25 ms 65892 KB Output is correct
24 Correct 25 ms 66004 KB Output is correct
25 Correct 24 ms 65956 KB Output is correct
26 Correct 25 ms 65876 KB Output is correct
27 Correct 24 ms 65904 KB Output is correct
28 Correct 26 ms 65868 KB Output is correct
29 Correct 25 ms 65920 KB Output is correct
30 Correct 97 ms 80488 KB Output is correct
31 Correct 99 ms 80488 KB Output is correct
32 Correct 105 ms 80568 KB Output is correct
33 Correct 101 ms 80492 KB Output is correct
34 Correct 101 ms 80492 KB Output is correct
35 Correct 108 ms 80460 KB Output is correct
36 Correct 98 ms 80452 KB Output is correct
37 Correct 106 ms 80588 KB Output is correct
38 Correct 117 ms 80492 KB Output is correct
39 Correct 100 ms 80496 KB Output is correct
40 Correct 111 ms 80492 KB Output is correct
41 Correct 98 ms 80496 KB Output is correct
42 Correct 101 ms 80436 KB Output is correct
43 Correct 97 ms 80492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 66000 KB Output is correct
2 Correct 25 ms 66004 KB Output is correct
3 Correct 25 ms 65964 KB Output is correct
4 Correct 26 ms 65972 KB Output is correct
5 Correct 25 ms 65956 KB Output is correct
6 Correct 25 ms 65996 KB Output is correct
7 Correct 29 ms 65928 KB Output is correct
8 Correct 29 ms 66004 KB Output is correct
9 Correct 25 ms 65968 KB Output is correct
10 Correct 25 ms 66028 KB Output is correct
11 Correct 26 ms 66092 KB Output is correct
12 Correct 24 ms 65996 KB Output is correct
13 Correct 24 ms 65936 KB Output is correct
14 Correct 25 ms 66000 KB Output is correct
15 Correct 25 ms 66020 KB Output is correct
16 Correct 25 ms 65968 KB Output is correct
17 Correct 25 ms 65952 KB Output is correct
18 Correct 25 ms 65976 KB Output is correct
19 Correct 24 ms 66036 KB Output is correct
20 Correct 25 ms 66060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 65868 KB Output is correct
2 Correct 27 ms 65876 KB Output is correct
3 Correct 25 ms 65976 KB Output is correct
4 Correct 25 ms 65980 KB Output is correct
5 Correct 25 ms 65980 KB Output is correct
6 Correct 25 ms 65980 KB Output is correct
7 Correct 25 ms 65884 KB Output is correct
8 Correct 26 ms 65932 KB Output is correct
9 Correct 26 ms 65844 KB Output is correct
10 Correct 25 ms 65952 KB Output is correct
11 Correct 25 ms 65868 KB Output is correct
12 Correct 27 ms 65876 KB Output is correct
13 Correct 26 ms 65924 KB Output is correct
14 Correct 26 ms 65844 KB Output is correct
15 Correct 25 ms 65924 KB Output is correct
16 Correct 25 ms 65984 KB Output is correct
17 Correct 25 ms 65980 KB Output is correct
18 Correct 25 ms 65916 KB Output is correct
19 Correct 25 ms 65876 KB Output is correct
20 Correct 24 ms 65868 KB Output is correct
21 Correct 25 ms 65976 KB Output is correct
22 Correct 25 ms 65872 KB Output is correct
23 Correct 25 ms 65892 KB Output is correct
24 Correct 25 ms 66004 KB Output is correct
25 Correct 24 ms 65956 KB Output is correct
26 Correct 25 ms 65876 KB Output is correct
27 Correct 24 ms 65904 KB Output is correct
28 Correct 26 ms 65868 KB Output is correct
29 Correct 25 ms 65920 KB Output is correct
30 Correct 97 ms 80488 KB Output is correct
31 Correct 99 ms 80488 KB Output is correct
32 Correct 105 ms 80568 KB Output is correct
33 Correct 101 ms 80492 KB Output is correct
34 Correct 101 ms 80492 KB Output is correct
35 Correct 108 ms 80460 KB Output is correct
36 Correct 98 ms 80452 KB Output is correct
37 Correct 106 ms 80588 KB Output is correct
38 Correct 117 ms 80492 KB Output is correct
39 Correct 100 ms 80496 KB Output is correct
40 Correct 111 ms 80492 KB Output is correct
41 Correct 98 ms 80496 KB Output is correct
42 Correct 101 ms 80436 KB Output is correct
43 Correct 97 ms 80492 KB Output is correct
44 Correct 26 ms 66000 KB Output is correct
45 Correct 25 ms 66004 KB Output is correct
46 Correct 25 ms 65964 KB Output is correct
47 Correct 26 ms 65972 KB Output is correct
48 Correct 25 ms 65956 KB Output is correct
49 Correct 25 ms 65996 KB Output is correct
50 Correct 29 ms 65928 KB Output is correct
51 Correct 29 ms 66004 KB Output is correct
52 Correct 25 ms 65968 KB Output is correct
53 Correct 25 ms 66028 KB Output is correct
54 Correct 26 ms 66092 KB Output is correct
55 Correct 24 ms 65996 KB Output is correct
56 Correct 24 ms 65936 KB Output is correct
57 Correct 25 ms 66000 KB Output is correct
58 Correct 25 ms 66020 KB Output is correct
59 Correct 25 ms 65968 KB Output is correct
60 Correct 25 ms 65952 KB Output is correct
61 Correct 25 ms 65976 KB Output is correct
62 Correct 24 ms 66036 KB Output is correct
63 Correct 25 ms 66060 KB Output is correct
64 Correct 590 ms 157288 KB Output is correct
65 Correct 549 ms 158704 KB Output is correct
66 Correct 686 ms 179500 KB Output is correct
67 Correct 736 ms 191572 KB Output is correct
68 Correct 70 ms 74164 KB Output is correct
69 Correct 71 ms 74788 KB Output is correct
70 Correct 68 ms 74124 KB Output is correct
71 Correct 66 ms 74692 KB Output is correct
72 Correct 63 ms 75708 KB Output is correct
73 Correct 91 ms 83368 KB Output is correct
74 Correct 513 ms 151680 KB Output is correct
75 Correct 522 ms 151628 KB Output is correct
76 Correct 600 ms 164952 KB Output is correct
77 Correct 626 ms 167136 KB Output is correct
78 Correct 582 ms 154896 KB Output is correct
79 Correct 587 ms 154968 KB Output is correct
80 Correct 486 ms 170452 KB Output is correct
81 Correct 685 ms 285140 KB Output is correct
82 Correct 693 ms 285112 KB Output is correct
83 Correct 617 ms 151176 KB Output is correct
84 Correct 588 ms 144012 KB Output is correct
85 Correct 615 ms 144464 KB Output is correct
86 Correct 671 ms 156948 KB Output is correct
87 Correct 706 ms 212980 KB Output is correct
88 Correct 590 ms 183988 KB Output is correct
89 Correct 584 ms 156312 KB Output is correct
90 Correct 683 ms 174900 KB Output is correct
91 Correct 708 ms 192628 KB Output is correct
92 Correct 837 ms 231656 KB Output is correct