Submission #895371

#TimeUsernameProblemLanguageResultExecution timeMemory
895371boxChameleon's Love (JOI20_chameleon)C++17
100 / 100
39 ms696 KiB
#include <bits/stdc++.h>
#include "chameleon.h"
using namespace std;

#define ar array
#define sz(v) int(std::size(v))
using i64 = long long;
using vi = vector<int>;

const int MAXN = 505 * 2;

vi adj[MAXN];
int who[MAXN], nex[MAXN], pre[MAXN];
vector<int> v[MAXN];
int color[MAXN];

void dfs(int i) {
    v[color[i]].push_back(i);
    for (int j : adj[i]) if (color[j] == -1) {
        color[j] = color[i] ^ 1;
        dfs(j);
    }
}

void gen(int k) {
    fill(color + 1, color + 1 + k, -1);
    exchange(v[0], {});
    exchange(v[1], {});
    for (int i = 1; i <= k; i++) if (color[i] == -1) {
        color[i] = 0;
        dfs(i);
    }
}

vi operator+(vi one, const vi two) {
    one.insert(end(one), begin(two), end(two));
    return one;
}
bool interesting(vi one) {
    if (sz(one) == 1) return false;
    return Query(one) != sz(one);
}

void Solve(int N) {
    if (N <= 50) {
        for (int i = 1; i <= N * 2; i++)
            for (int j = i + 1; j <= N * 2; j++)
                if (Query({i, j}) == 1) {
                    adj[i].push_back(j);
                    adj[j].push_back(i);
                }
    } else {
        for (int i = 1; i <= N * 2; i++) {
            gen(i - 1);
            for (int c : {0, 1})
                while (interesting(v[c] + vi{{i}})) {
                    int low = 0, hi = sz(v[c]) - 1;
                    while (low < hi) {
                        int m = (low + hi) / 2;
                        interesting(vi(begin(v[c]), begin(v[c]) + m + 1) + vi{{i}}) ? hi = m : low = m + 1;
                    }
                    adj[v[c][low]].push_back(i);
                    adj[i].push_back(v[c][low]);
                    v[c] = vi(begin(v[c]) + low + 1, end(v[c]));
                }
        }
    }
    for (int i = 1; i <= N * 2; i++) {
        assert(sz(adj[i]) == 1 || sz(adj[i]) == 3);
        if (sz(adj[i]) == 1) {
            who[i] = adj[i][0];
        } else {
            nex[i] = [&]() {
                if (Query({i, adj[i][0], adj[i][1]}) == 1) return adj[i][2];
                if (Query({i, adj[i][0], adj[i][2]}) == 1) return adj[i][1];
                return adj[i][0];
            }();
            pre[nex[i]] = i;
        }
    }
    for (int i = 1; i <= N * 2; i++) {
        if (!who[i]) {
            assert(sz(adj[i]) == 3);
            assert(pre[i] && nex[i]);
            who[i] = adj[i][0] ^ adj[i][1] ^ adj[i][2] ^ pre[i] ^ nex[i];
        }
        if (who[i] < i) Answer(who[i], i);
    }
}
#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...