QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#586429 | #7937. Fast XORting | Math4Life2020 | TL | 0ms | 3840kb | C++23 | 2.2kb | 2024-09-24 12:11:38 | 2024-09-24 12:11:39 |
Judging History
answer
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
using ll = int; using pii = pair<ll,ll>;
ll e2(ll x) {
return (1LL<<x);
}
struct hashPii {
ll operator()(const pii &p) const { return p.first + ((p.second+1)<<18LL); }
};
struct ntrie {
gp_hash_table<pair<ll,ll>,ll,hashPii> cnt;
vector<ll> nf,nr;
ll E;
ntrie(ll Ec) {
E=Ec;
}
ntrie(ll x0, ll Ec) {
E=Ec;
cnt[{0,-1}]=1;
for (ll j=0;j<E;j++) {
ll xn = (x0>>(E-1-j))<<(E-1-j);
cnt[{xn,j}]=1;
nf.push_back(0);
nr.push_back(0);
}
}
ll extract() {
ll ans = 0;
for (ll j=0;j<E;j++) {
ans += min(nf[j],nr[j]);
}
return ans;
}
ll extractR() {
ll ans = 0;
for (ll j=0;j<E;j++) {
ans += nr[j];
}
return ans;
}
};
ntrie merge(ntrie t1, ntrie t2, ll E) {
ntrie t0 = ntrie(E);
for (ll i=0;i<E;i++) {
t0.nr.push_back(t1.nr[i]+t2.nr[i]);
t0.nf.push_back(t1.nf[i]+t2.nf[i]);
}
vector<pii> verts;
for (auto A: t1.cnt) {
verts.push_back(A.first);
}
for (auto A: t2.cnt) {
verts.push_back(A.first);
}
sort(verts.begin(),verts.end());
auto it = unique(verts.begin(),verts.end());
verts.erase(it,verts.end());
for (pii p0: verts) {
t0.cnt[p0]=0;
if (t1.cnt.find(p0)!=t1.cnt.end()) {
t0.cnt[p0]+=t1.cnt[p0];
}
if (t2.cnt.find(p0)!=t2.cnt.end()) {
t0.cnt[p0]+=t2.cnt[p0];
}
ll x = p0.first; ll j = 1+p0.second;
if (j>=E) {
continue;
}
ll xl = x; ll xh = x+e2(E-1-j);
t0.nf[j] += t1.cnt[{xl,j}]*t2.cnt[{xh,j}];
t0.nr[j] += t1.cnt[{xh,j}]*t2.cnt[{xl,j}];
}
return t0;
}
ntrie build(vector<ll> a, ll E) {
ll N = a.size();
if (N==1) {
return ntrie(a[0],E);
}
vector<ll> b,c;
for (ll i=0;i<(N/2);i++) {
b.push_back(a[i]);
}
for (ll i=(N/2);i<N;i++) {
c.push_back(a[i]);
}
return merge(build(b,E),build(c,E),E);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
ll N; cin >> N;
ll E = __builtin_ctz(N);
vector<ll> a;
for (ll i=0;i<N;i++) {
ll x; cin >> x;
a.push_back(x);
}
ntrie troot = build(a,E);
ll ans = troot.extractR();
ans = min(ans,1+troot.extract());
cout << ans << "\n";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3840kb
input:
8 0 1 3 2 5 4 7 6
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
8 2 0 1 3 4 5 6 7
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: -100
Time Limit Exceeded
input:
262144 47482 131703 90418 122675 166494 247529 196154 16950 66501 50357 246808 25929 10418 50538 26955 151884 63776 58023 20073 26544 74785 44064 41836 148543 87920 54172 3270 131495 130960 112122 167229 215767 77499 195004 21391 11039 168999 256346 109690 180904 172679 157200 78594 201857 52784 147...