QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#586463#7937. Fast XORtingMath4Life2020TL 0ms3860kbC++232.9kb2024-09-24 12:53:562024-09-24 12:53:56

Judging History

你现在查看的是最新测评结果

  • [2024-09-24 12:53:56]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3860kb
  • [2024-09-24 12:53:56]
  • 提交

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;
	vector<pii> vlist;
	ll E;
	ntrie(ll Ec) {
		E=Ec;
	}
	ntrie(ll x0, ll Ec) {
		E=Ec;
		cnt[{0,-1}]=1;
		vlist.push_back({0,-1});
		for (ll j=0;j<E;j++) {
			ll xn = (x0>>(E-1-j))<<(E-1-j);
			cnt[{xn,j}]=1;
			vlist.push_back({xn,j});
			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; //speedup!
	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());*/
	ll L1 = t1.vlist.size();
	ll L2 = t2.vlist.size();
	ll i1 = 0; ll i2 = 0;
	while (i1<L1 && i2<L2) {
		pii a = t1.vlist[i1]; pii b = t2.vlist[i2];
		if (a.first>b.first) {
			t0.vlist.push_back(b); i2++;
		} else if (a.first<b.first) {
			t0.vlist.push_back(a); i1++;
		} else {
			if (a.second>b.second) {
				t0.vlist.push_back(b); i2++;
			} else if (a.second<b.second) {
				t0.vlist.push_back(a); i1++;
			} else {
				t0.vlist.push_back(a); i1++; i2++;
			}
		}
	}
	while (i1<L1) {
		t0.vlist.push_back(t1.vlist[i1++]);
	}
	while (i2<L2) {
		t0.vlist.push_back(t2.vlist[i2++]);
	} 
	for (pii p0: t0.vlist) {
		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: 3668kb

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: 3860kb

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...

output:


result: