[乱搞] BZOJ3689 异或之

%%hzwer:http://hzwer.com/3657.html

好可怕,暴力大法好

什么可持久化字典树,蒟蒻不会

#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long ll;

inline char nc()
{
	static char buf[100000],*p1=buf,*p2=buf;
	if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
	return *p1++;
}

inline void read(int &x)
{
	char c=nc(),b=1;
	for (;!(c>='0' &&  c<='9');c=nc()) if (c=='-') b=-1;
	for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

int n,K;
int num[100005];
int a[35][100005];ll cnt[35];
int b[2000005],pnt;

inline void solve1()
{
	for (int i=1;i<=n;i++)
		for (int j=i+1;j<=n;j++)
			b[++pnt]=num[i]^num[j];
	sort(b+1,b+pnt+1);
	for (int i=1;i<=K;i++)
		printf("%d ",b[i]);
}

inline void divi(int l,int r,int k)
{
	cnt[k+1]+=(ll)(r-l+1)*(r-l)/2;
	if (l>=r) return;
	int t=l,mid;
	for (int i=l;i<=r;i++)
		if (a[k+1][i]&(1<<k))
			a[k][t++]=a[k+1][i];
	mid=t-1;
	for (int i=l;i<=r;i++)
		if (~a[k+1][i]&(1<<k))
			a[k][t++]=a[k+1][i];
	if (k!=0)
		divi(l,mid,k-1),divi(mid+1,r,k-1);
}

inline void print(int l,int r,int k,int x)
{
	if (l>=r) return;
	if (x==k)
	{
		for (int i=l;i<=r;i++)
			for (int j=i+1;j<=r;j++)
				b[++pnt]=a[k][i]^a[k][j];
		return;
	}
	int mid=l;
	for (mid=l;mid<=r;mid++)
		if (~a[k][mid]&(1<<k))
			break;
	mid--;
	if (k!=0)
		print(l,mid,k-1,x),print(mid+1,r,k-1,x);
}

inline void solve2()
{
	for (int i=1;i<=n;i++) a[31][i]=num[i];
	divi(1,n,30);
	for (int i=31;i>=0;i--)
		if (cnt[i]<K)
		{
			print(1,n,31,i+1); break;
		}
	sort(b+1,b+pnt+1);
	for (int i=1;i<=K;i++)
		printf("%d ",b[i]);
}

int main()
{
	freopen("t.in","r",stdin);
	freopen("t.out","w",stdout);
	read(n); read(K);
	for (int i=1;i<=n;i++)
		read(num[i]);
	if (n<=1000)
		solve1();
	else
		solve2();
}


你可能感兴趣的:([乱搞] BZOJ3689 异或之)