Submission #302708

#TimeUsernameProblemLanguageResultExecution timeMemory
302708lohachoMechanical Doll (IOI18_doll)C++14
100 / 100
183 ms10644 KiB
#include "doll.h"
#include <bits/stdc++.h>

using namespace std;

using LL = long long;
const int INF = (int)1e9 + 7;
const int NS = (int)2e5 + 4;
int lr[NS * 2];
vector<int> C;
vector<int> X, Y;

void make_tree(int num, int now, int to, int cnt){
    X.push_back(0), Y.push_back(0);
    if(now == to){
        if(cnt - (1 << (to - now)) >= 0){
            X[-num - 1] = -1;
        }
        return;
    }
    if(cnt - (1 << (to - now)) >= 0){
        X[-num - 1] = -1;
        Y[-num - 1] = -(int)Y.size() - 1;
        make_tree(-(int)Y.size() - 1, now + 1, to, cnt - (1 << (to - now)));
    }
    else{
        X[-num - 1] = -(int)X.size() - 1;
        make_tree(-(int)X.size() - 1, now + 1, to, cnt);
        Y[-num - 1] = -(int)Y.size() - 1;
        make_tree(-(int)Y.size() - 1, now + 1, to, cnt - (1 << (to - now)));
    }
}

pair < int, int > get(int num, int now, int to){
    if(!lr[-num-1]){
        lr[-num-1] = 1 - lr[-num-1];
        if(X[-num-1] == -1) return make_pair(0, 0);
        if(now == to) return make_pair(-num-1, 0);
        return get(X[-num-1], now + 1, to);
    }
    else{
        lr[-num-1] = 1 - lr[-num-1];
        if(now == to) return make_pair(-num-1, 1);
        return get(Y[-num-1], now + 1, to);
    }
}

void create_circuit(int M, std::vector<int> A) {
    A.push_back(0);
    C.resize(M + 1);
    int N = A.size();
    if(N == 2){
        C[0] = A[0], C[A[0]] = 0;
        answer(C, X, Y);
        return;
    }
    for(auto&i:C) i = -1;
    int dep = 1;
    while((1 << dep) < N){
        dep += 1;
    }
    make_tree(-1, 1, dep, (1 << dep) - N);
    int j = 0;
    for(int i = 0; i < (1 << dep); ++i){
        pair < int, int > val = get(-1, 1, dep);
        if(val.first){
            if(!val.second) X[val.first] = A[j++];
            else Y[val.first] = A[j++];
        }
    }
    answer(C, X, 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...
#Verdict Execution timeMemoryGrader output
Fetching results...