Submission #1014328

#TimeUsernameProblemLanguageResultExecution timeMemory
1014328c2zi6Mechanical Doll (IOI18_doll)C++14
100 / 100
126 ms24488 KiB
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#include "doll.h"

namespace TEST4 {
    typedef VI::iterator VIT;
    void solve(int M, VI A) { int N = A.size();
        VI C, X, Y;
        X.reserve(2*A.size());
        Y.reserve(2*A.size());
        C = VI(M+1);
        replr(u, 0, M) C[u] = -1;
        auto trans = [&](int x) {
            return -(x+1);
        };
        auto flip = [&](int x, int s) {
            rep(i, s/2) {
                int ch = bool(x&(1<<i))^bool(x&(1<<(s-1-i)));
                x ^= (ch<<i);
                x ^= (ch<<(s-1-i));
            }
            return x;
        };
        auto index = [&](VIT it) {
            if (X.begin() <= it && it < X.end()) {
                return it-X.begin();
            }
            return it-Y.begin();
        };
        int sz = 0;
        while ((1<<sz) < N+1) sz++;
        sz--;
        int l = (1<<sz)-1;
        int r = l+(1<<sz)-1;
        rep(i, r+1) {
            X.pb(-1), Y.pb(-1);
        }
        replr(i, 0, l-1) {
            X[i] = trans(2*i+1);
            Y[i] = trans(2*i+2);
        }
        vector<pair<int, VIT>> order;
        int hmin = N+1;
        reprl(i, r, l) {
            order.pb({flip(2*(i-l)+1, sz+1), Y.begin() + i});
            if (!--hmin) break;
            order.pb({flip(2*(i-l),   sz+1), X.begin() + i});
            if (!--hmin) break;
        }
        sort(all(order));
        A.pb(0);
        VI ka(r+1);
        ka[0] = true;
        rep(u, A.size()) {
            *(order[u].ss) = A[u];
            int v = index(order[u].ss);
            while (v != 0) {
                ka[v] = true;
                v = (v-1)/2;
            }
        }
        map<int, int> compress;
        int c = -1;
        VI xtmp, ytmp;
        rep(u, r+1) if (ka[u]) {
            xtmp.pb(X[u]), ytmp.pb(Y[u]);
            compress[trans(u)] = c--;
        }
        X = xtmp;
        Y = ytmp;
        rep(u, X.size()) {
            if (X[u] < 0) {
                if (compress.count(X[u])) {
                    X[u] = compress[X[u]];
                } else {
                    X[u] = -1;
                }
            }
            if (Y[u] < 0) {
                if (compress.count(Y[u])) {
                    Y[u] = compress[Y[u]];
                } else {
                    Y[u] = -1;
                }
            }
        }
        answer(C, X, Y);
    }
};

void create_circuit(int M, VI A) {
    TEST4::solve(M, A);
}
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...