結果

問題 No.2977 Kth Xor Pair
ユーザー t98slider
提出日時 2024-12-01 04:16:05
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,774 ms / 3,000 ms
コード長 3,113 bytes
コンパイル時間 2,155 ms
コンパイル使用メモリ 198,904 KB
最終ジャッジ日時 2025-02-26 09:55:31
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <int LOG = 30> struct BinaryTrie {
struct Node {
long long cnt = 0;
int ch[2] = {-1, -1};
};
std::vector<Node> ns;
BinaryTrie() : ns(1) {}
long long size() const { return ns[0].cnt; }
long long operator[](int k) const { return find_kth(k, 0); }
long long find_kth(long long k, long long xor_add = 0) const {
assert(0 <= k && k < size());
long long idx = 0, val = 0;
for(int i=LOG-1; i>=0; i--) {
long long c = xor_add >> i & 1;
long long low_ch = ns[idx].ch[c];
long long low_cnt = (low_ch >= 0 ? ns[low_ch].cnt : 0);
if (k < low_cnt) {
idx = low_ch;
} else {
k -= low_cnt;
idx = ns[idx].ch[c ^ 1];
val ^= 1LL << i;
}
assert(idx >= 0);
}
return val;
}
void add(long long val, long long cnt = 1) {
assert(0 <= val && val < (1LL << LOG));
int idx = 0;
for(int i=LOG-1; i>=0; i--) {
ns[idx].cnt += cnt;
assert(ns[idx].cnt >= 0);
int &nxt = ns[idx].ch[val >> i & 1];
if (nxt == -1) {
idx = nxt = ns.size();
ns.emplace_back();
} else {
idx = nxt;
}
}
ns[idx].cnt += cnt;
assert(ns[idx].cnt >= 0);
}
long long lower_bound(long long val, long long xor_add = 0) {
assert(0 <= val);
if (val >= (1LL << LOG)) return size();
int idx = 0;
long long cnt = 0;
for(int i=LOG-1; i>=0; i--) {
int b = val >> i & 1, c = xor_add >> i & 1;
int ch = ns[idx].ch[c];
cnt += (b & (ch >= 0) ? ns[ch].cnt : 0);
idx = ns[idx].ch[b ^ c];
if (idx < 0 or ns[idx].cnt == 0) break;
}
return cnt;
}
long long count(long long val) const {
assert(0 <= val && val < (1LL << LOG));
int idx = 0;
for(int i=LOG-1; i>=0; i--) {
idx = ns[idx].ch[val >> i & 1];
if (idx < 0 or ns[idx].cnt == 0) return 0;
}
return ns[idx].cnt;
}
long long count(long long L, long long R, long long xor_add = 0) {
assert(0 <= L && L <= R && R <= (1LL << LOG));
return lower_bound(R, xor_add) - lower_bound(L, xor_add);
}
long long xor_min(long long xor_add = 0) { return find_kth(0, xor_add); }
long long xor_max(long long xor_add = 0) { return find_kth(size() - 1, xor_add); }
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
ll k;
cin >> n >> k;
vector<int> a(n);
BinaryTrie<30> trie;
for(auto &&v : a){
cin >> v;
trie.add(v);
}
int ng = -1, ok = 1 << 30;
while(ng + 1 < ok){
int mid = (ok + ng) / 2;
ll tot = -n;
for(auto &&v : a) tot += trie.lower_bound(mid + 1, v);
tot /= 2;
(tot >= k ? ok : ng) = mid;
}
cout << ok << '\n';
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0