【CF472G】Design Tutorial 压位

题目大意

  给出两个\(01\)序列\(A\)\(B\)

  汉明距离定义为两个长度相同的序列中,有多少个对应位置上的数字不一样

  \(00111\)\(10101\)的距离为\(2\)

  \(Q\)次询问,每次询问给出\(p_1,p_2,len\)

  求\(a_{p_1},a_{p_1+1}\ldots a_{p_1+len−1}\)\(b_{p_1},b_{p_1+1}\ldots b_{p_1+len−1}\)两个子串的汉明距离

  \(n\leq 2\times{10}^5,q\leq 4\times {10}^5\)

题解

  wys【挑战】弱化版

  暴力碾分块系列

  把\(a_x\ldots a_{x+63}\)压成一个\(64\)位整数,每次暴力统计。

  时间复杂度:\(O(\frac{nq}{64})\)

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
char s1[200010];
char s2[200010];
ull a[200010];
ull b[200010];
int cnt[100010];
int count(ull x)
{
	return cnt[x&0xffff]+cnt[(x>>16)&0xffff]+cnt[(x>>32)&0xffff]+cnt[(x>>48)&0xffff];
}
int main()
{
	int n,m,q;
	scanf("%s%s%d",s1+1,s2+1,&q);
	n=strlen(s1+1);
	m=strlen(s2+1);
	int i;
	for(i=n;i>=1;i--)
		a[i]=(a[i+1]>>1)|(s1[i]=='1'?(1ll<<63):0);
	for(i=m;i>=1;i--)
		b[i]=(b[i+1]>>1)|(s2[i]=='1'?(1ll<<63):0);
	cnt[0]=0;
	for(i=1;i<(1<<16);i++)
		cnt[i]=cnt[i>>1]+(i&1);
	int x,y,l;
	for(i=1;i<=q;i++)
	{
		scanf("%d%d%d",&x,&y,&l);
		x++;
		y++;
		int ans=0;
		while(l>=64)
		{
			ans+=count(a[x]^b[y]);
			x+=64;
			y+=64;
			l-=64;
		}
		if(l)
			ans+=count((a[x]^b[y])>>(64-l));
		printf("%d\n",ans);
	}
	return 0;
}
posted @ 2018-03-05 20:57  ywwyww  阅读(292)  评论(0编辑  收藏  举报