提出 #69672102


ソースコード 拡げる

#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif

template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = std::forward<U>(y), true); }

template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = std::forward<U>(y), true); }

template <class T> void mkuni(std::vector<T>& v) {
    std::ranges::sort(v);
    auto result = std::ranges::unique(v);
    v.erase(result.begin(), result.end());
}

template <class T> int lwb(const std::vector<T>& v, const T& x) {
    return std::distance(v.begin(), std::ranges::lower_bound(v, x));
}

using namespace std;

using ll = long long;

template <typename T, int MAX_LOG> struct BinaryTrie {
    struct Node {
        std::array<int, 2> nxt;
        int count;
        Node() : nxt{-1, -1}, count(0) {}
    };

    std::vector<Node> nodes;

    inline int& next(int i, int j) { return nodes[i].nxt[j]; }

    BinaryTrie() { nodes.emplace_back(); }

    void insert(const T& x) {
        int cur = 0;
        for (int i = MAX_LOG - 1; i >= 0; i--) {
            int f = x >> i & 1;
            int nxt = next(cur, f);
            if (nxt == -1) {
                nxt = nodes.size();
                next(cur, f) = nxt;
                nodes.emplace_back();
            }
            nodes[cur].count++;
            cur = nxt;
        }
        nodes[cur].count++;
    }
};

const int MAX_LOG = 30;

void solve() {
    int N, M;
    cin >> N >> M;
    vector<int> A(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }

    BinaryTrie<int, MAX_LOG> bt;
    for (int i = 0; i < N; i++) {
        bt.insert(A[i]);
    }

    auto g = [&](int k, int x) -> int {  // x 未満で k bit 目が 1 となる整数の個数
        int tmp = x;
        int cycle = 1 << (k + 1);
        int res = x / cycle * (1 << k);
        x %= cycle;
        res += min(1 << k, x);
        return tmp - res;
    };
    auto f = [&](int k, int l, int r) -> int { return g(k, r) - g(k, l); };
    auto dfs = [&](auto self, int cur, int d, int l, int r) -> ll {
        if (r <= l or d == -1) {
            return 0;
        }
        ll res = 0;
        auto L = bt.next(cur, 0), R = bt.next(cur, 1);
        if (L != -1 and R != -1) {
            assert(l == 0);
            int cycle = 1 << (d + 1);
            int weight = r / cycle;
            r %= cycle;
            auto xl = self(self, L, d - 1, 0, 1 << d);
            auto xr = self(self, R, d - 1, 0, 1 << d);
            res += (xl + xr) * weight;
            if (r <= cycle / 2) {
                res += self(self, L, d - 1, 0, r);
            } else {
                res += xl;
                res += self(self, R, d - 1, 0, r - (1 << d));
            }
        } else if (L != -1) {
            res += f(d, l, r) * (1LL << d);
            res += self(self, L, d - 1, l, r);
        } else {
            assert(R != -1);
            res += ((r - l) - f(d, l, r)) * (1LL << d);
            res += self(self, R, d - 1, l, r);
        }
        return res;
    };
    ll ans = dfs(dfs, 0, MAX_LOG - 1, 0, M);

    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    solve();
    return 0;
}

提出情報

提出日時
問題 G - Sum of Min of XOR
ユーザ rniya
言語 C++ 23 (gcc 12.2)
得点 575
コード長 3378 Byte
結果 AC
実行時間 215 ms
メモリ 53144 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 575 / 575
結果
AC × 3
AC × 45
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_handmade_00.txt, 01_handmade_01.txt, 01_handmade_02.txt, 01_handmade_03.txt, 01_handmade_04.txt, 01_handmade_05.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 02_random_10.txt, 02_random_11.txt, 02_random_12.txt, 02_random_13.txt, 02_random_14.txt, 02_random_15.txt, 02_random_16.txt, 02_random_17.txt, 02_random_18.txt, 02_random_19.txt, 02_random_20.txt, 02_random_21.txt, 02_random_22.txt, 02_random_23.txt, 02_random_24.txt, 02_random_25.txt, 02_random_26.txt, 02_random_27.txt, 02_random_28.txt, 02_random_29.txt, 02_random_30.txt, 02_random_31.txt, 02_random_32.txt, 02_random_33.txt, 02_random_34.txt, 02_random_35.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 1 ms 3400 KiB
00_sample_01.txt AC 1 ms 3308 KiB
00_sample_02.txt AC 1 ms 3412 KiB
01_handmade_00.txt AC 1 ms 3524 KiB
01_handmade_01.txt AC 1 ms 3528 KiB
01_handmade_02.txt AC 1 ms 3612 KiB
01_handmade_03.txt AC 1 ms 3552 KiB
01_handmade_04.txt AC 1 ms 3440 KiB
01_handmade_05.txt AC 128 ms 53032 KiB
02_random_00.txt AC 195 ms 53092 KiB
02_random_01.txt AC 195 ms 53032 KiB
02_random_02.txt AC 205 ms 53076 KiB
02_random_03.txt AC 204 ms 53052 KiB
02_random_04.txt AC 206 ms 53108 KiB
02_random_05.txt AC 86 ms 27980 KiB
02_random_06.txt AC 56 ms 15560 KiB
02_random_07.txt AC 112 ms 28104 KiB
02_random_08.txt AC 121 ms 28300 KiB
02_random_09.txt AC 160 ms 52896 KiB
02_random_10.txt AC 190 ms 53116 KiB
02_random_11.txt AC 189 ms 53112 KiB
02_random_12.txt AC 191 ms 53144 KiB
02_random_13.txt AC 192 ms 53084 KiB
02_random_14.txt AC 191 ms 53104 KiB
02_random_15.txt AC 50 ms 15620 KiB
02_random_16.txt AC 183 ms 53012 KiB
02_random_17.txt AC 167 ms 53084 KiB
02_random_18.txt AC 78 ms 27960 KiB
02_random_19.txt AC 188 ms 53032 KiB
02_random_20.txt AC 21 ms 4124 KiB
02_random_21.txt AC 21 ms 4168 KiB
02_random_22.txt AC 25 ms 4788 KiB
02_random_23.txt AC 25 ms 4820 KiB
02_random_24.txt AC 49 ms 10080 KiB
02_random_25.txt AC 51 ms 10076 KiB
02_random_26.txt AC 90 ms 16032 KiB
02_random_27.txt AC 88 ms 16112 KiB
02_random_28.txt AC 104 ms 16148 KiB
02_random_29.txt AC 96 ms 16164 KiB
02_random_30.txt AC 153 ms 53084 KiB
02_random_31.txt AC 125 ms 53072 KiB
02_random_32.txt AC 157 ms 53052 KiB
02_random_33.txt AC 175 ms 53116 KiB
02_random_34.txt AC 163 ms 53108 KiB
02_random_35.txt AC 215 ms 53036 KiB