Submission #1048843

#TimeUsernameProblemLanguageResultExecution timeMemory
1048843becaidoAncient Books (IOI17_books)C++17
100 / 100
234 ms103252 KiB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifdef WAIMAI
#include "grader.cpp"
#else
#include "books.h"
#endif

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

#define lpos pos*2
#define rpos pos*2+1

const int INF = 1e9;
const int SIZE = 5e6 + 5;

int n, L, R;
int p[SIZE];
bool vs[SIZE];
int l[SIZE], r[SIZE];
int d[SIZE], in[SIZE], out[SIZE], dis[SIZE], pre[SIZE], g[SIZE];

int sz, id[SIZE];
deque<int> q;
pair<int, int> e[SIZE];
void build(int pos, int l, int r) {
    id[pos] = (l == r ? l : ++sz);
    debug(l, r, id[pos]);
    if (l < r) {
        int mid = (l + r) / 2;
        build(lpos, l, mid);
        build(rpos, mid + 1, r);
        e[id[pos]] = {id[lpos], id[rpos]};
    }
}
void add(int pos, int l, int r, int L, int R, int d, int k) {
    if (l == L && r == R) {
        if (d < dis[id[pos]]) {
            dis[id[pos]] = d;
            q.emplace_front(id[pos]);
            pre[id[pos]] = k;
        }
        return;
    }
    int mid = (L + R) / 2;
    if (r <= mid) add(lpos, l, r, L, mid, d, k);
    else if (l > mid) add(rpos, l, r, mid + 1, R, d, k);
    else {
        add(lpos, l, mid, L, mid, d, k);
        add(rpos, mid + 1, r, mid + 1, R, d, k);
    }
}

ll minimum_walk(vector<int> p_, int s) {
    n = p_.size();
    s++;
    FOR (i, 1, n) {
        p[i] = p_[i - 1] + 1;
        in[i] = out[i] = vs[i] = pre[i] = 0;
    }

    FOR (i, 1, n) {
        if (i < p[i]) {
            out[i]++, out[p[i]]--;
            in[i + 1]++, in[p[i] + 1]--;
            d[i]++, d[p[i]]--;
        } else {
            out[p[i] + 1]++, out[i + 1]--;
            in[p[i]]++, in[i]--;
            d[p[i]]++, d[i]--;
        }
    }
    FOR (i, 1, n) {
        in[i] += in[i - 1];
        out[i] += out[i - 1];
        d[i] += d[i - 1];
    }

    L = 1, R = n;
    while (R > s && d[R - 1] == 0) R--;
    while (L < s && d[L] == 0) L++;
    if (L == R) return 0;

    int cnt = 0;
    FOR (i, L, R) if (vs[i] == 0) {
        int mn = n, mx = 1, x = i;
        vector<int> v;
        while (vs[x] == 0) {
            mn = min(mn, x);
            mx = max(mx, x);
            v.pb(x), vs[x] = 1, x = p[x];
        }
        cnt++;
        for (int x : v) l[x] = mn, r[x] = mx, g[x] = cnt;
    }

    ll ans = 0;
    FOR (i, L, R) ans += out[i];

    sz = n;
    build(1, 1, n);
    fill(dis + 1, dis + sz + 1, INF);
    fill(vs + 1, vs + sz + 1, 0);
    q.clear();

    dis[s] = 0, q.pb(s);
    while (q.size()) {
        int pos = q.front();
        q.pop_front();
        if (vs[pos]) continue;
        vs[pos] = 1;
        debug(pos, dis[pos]);
        auto upd = [&](int i, int d, int t) {
            if (d < dis[i]) {
                dis[i] = d;
                if (t == 0) q.emplace_front(i);
                else q.pb(i);
                pre[i] = pos;
            }
        };
        if (pos > n) {
            auto [x, y] = e[pos];
            upd(x, dis[pos], 0);
            upd(y, dis[pos], 0);
            continue;
        }
        add(1, l[pos], r[pos], 1, n, dis[pos], pos);
        if (pos - 1 >= L) upd(pos - 1, dis[pos] + 1, 1);
        if (pos + 1 <= R) upd(pos + 1, dis[pos] + 1, 1);
    }
    debug("here");
    int x = L, y = R;
    while (pre[x] && dis[pre[x]] == dis[x]) x = pre[x];
    while (pre[y] && dis[pre[y]] == dis[y]) y = pre[y];
    while (g[x] != g[y]) {
        debug(x, y, g[x], g[y]);
        if (dis[x] < dis[y]) swap(x, y);
        x = pre[x];
        ans += 2;
        while (pre[x] && dis[pre[x]] == dis[x]) x = pre[x];
    }
    ans += 2 * dis[x];

    return ans;
}

/*
in1
4 0
0 2 3 1
out1
6
*/

Compilation message (stderr)

books.cpp: In function 'void build(int, int, int)':
books.cpp:18:20: warning: statement has no effect [-Wunused-value]
   18 | #define debug(...) 7122
      |                    ^~~~
books.cpp:45:5: note: in expansion of macro 'debug'
   45 |     debug(l, r, id[pos]);
      |     ^~~~~
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:76:41: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   76 |         in[i] = out[i] = vs[i] = pre[i] = 0;
      |                                  ~~~~~~~^~~
books.cpp:18:20: warning: statement has no effect [-Wunused-value]
   18 | #define debug(...) 7122
      |                    ^~~~
books.cpp:129:9: note: in expansion of macro 'debug'
  129 |         debug(pos, dis[pos]);
      |         ^~~~~
books.cpp:18:20: warning: statement has no effect [-Wunused-value]
   18 | #define debug(...) 7122
      |                    ^~~~
books.cpp:148:5: note: in expansion of macro 'debug'
  148 |     debug("here");
      |     ^~~~~
books.cpp:18:20: warning: statement has no effect [-Wunused-value]
   18 | #define debug(...) 7122
      |                    ^~~~
books.cpp:153:9: note: in expansion of macro 'debug'
  153 |         debug(x, y, g[x], g[y]);
      |         ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...