Codeforces Round #626 (Div. 2) D. Present 按位贡献 + 快排新姿势

传送门

文章目录

题意:

给你一个长度为 n n n的序列 a a a,让你计算
在这里插入图片描述
n ≤ 4 e 5 , a ≤ 1 e 7 n\le 4e5,a\le 1e7 n4e5,a1e7

思路:

首先这个式子是 n 2 n^2 n2的,显然不能直接算,并且异或没有分配律,所以碰到这种情况我们自然的就想到按位考虑。
分开考虑每一位,假设当前为第 k k k位,当这一位是 1 1 1的时候,两个数的和一定在 [ 2 k , 2 k + 1 − 1 ] [2^k,2^{k+1}-1] [2k,2k+11]之间,由于有可能会溢出,比如全是 1 1 1的情况相加,所以还有可能在 [ 2 k + 2 k + 1 , 2 k + 2 − 2 ] [2^k+2^{k+1},2^{k+2}-2] [2k+2k+1,2k+22]之间,我们可以取出来每个数模上 2 k + 1 2^{k+1} 2k+1之后的数,将他们排个序,之后双指针扫一遍,注意要倒着扫才能保证正确性,算一下有多少对在这一位有贡献的,让后看奇偶性就好啦。
直接做的复杂度显然是 n l o g n l o g a nlognloga nlognloga,这个题显然是能过去的,但是我们可以再优化一下它。
考虑到枚举每一位是不能优化掉的,我们对排序进行优化。
观察可以知道我们每次都只考虑某一位之前的数,并且每次都增加一位,所以我们每次只执行一次快排,因为对于新增加的一位我们能将其分成两部分,这一位如果是 1 1 1就放在右边,否则放在左边,让后双指针扫一下就可以排出序来了。
复杂度 n l o g a nloga nloga

// Problem: D. Present
// Contest: Codeforces - Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)
// URL: https://codeforces.com/contest/1323/problem/D
// Memory Limit: 512 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n;
int a[N],b[N];
int ans=0;
int L[N],R[N],Lid[N],Rid[N],id[N];

int get(int L,int R) {
	if(L>R) return 0;
	int ans=0;
	for(int i=n,l=1,r=1;i>=1;i--) {
		while(l<=n&&b[l]+b[i]<L) l++;
		while(r<=n&&b[r]+b[i]<=R) r++;
		ans+=r-l-(l<=i&&i<r);
	}
	return ans>>1&1;
}

void merge(int k) {
	int l,r; l=r=0;
	for(int i=1;i<=n;i++) {
		if(a[id[i]]>>k&1) R[++r]=b[i]|(1<<k),Rid[r]=id[i];
		else L[++l]=b[i],Lid[l]=id[i];
	}
	int p1=1,p2=1,p=1;
	while(p1<=l&&p2<=r) {
		if(L[p1]<=R[p2]) b[p]=L[p1],id[p++]=Lid[p1++];
		else b[p]=R[p2],id[p++]=Rid[p2++];
	}
	while(p1<=l) b[p]=L[p1],id[p++]=Lid[p1++];
	while(p2<=r) b[p]=R[p2],id[p++]=Rid[p2++];
}

int calc(int x) {
	merge(x);
	return (get(1<<x,(1<<(x+1))-1)+get(3<<x,(1<<(x+2))-2))%2;
}

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);
	
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]),id[i]=i;
	for(int i=0;i<25;i++) ans+=calc(i)<<i;
	printf("%d\n",ans);



	return 0;
}
/*

*/









  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值