题目大意

  给出两个\(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;
}

【CF472G】Design Tutorial 压位的更多相关文章

  1. cf 472G Design Tutorial: Increase the Constraints 分块+压位/FFT

    题目大意 给出两个\(01\)序列\(A\)和\(B\) 哈明距离定义为两个长度相同的序列中,有多少个对应位置上的数字不一样 "00111" 和 "10101" ...

  2. Codeforces #270 D. Design Tutorial: Inverse the Problem

    http://codeforces.com/contest/472/problem/D D. Design Tutorial: Inverse the Problem time limit per t ...

  3. codevs 3119 高精度练习之大整数开根 (各种高精+压位)

    /* codevs 3119 高精度练习之大整数开根 (各种高精+压位) 二分答案 然后高精判重 打了一个多小时..... 最后还超时了...压位就好了 测试点#1.in 结果:AC 内存使用量: 2 ...

  4. 压位加速-poj-2443-Set Operation

    题目链接: http://poj.org/problem?id=2443 题目意思: 有n个集合(n<=1000),每个集合有m个数ai(m<=10000,1=<ai<=100 ...

  5. cf472D Design Tutorial: Inverse the Problem

    D. Design Tutorial: Inverse the Problem time limit per test 2 seconds memory limit per test 256 mega ...

  6. cf472C Design Tutorial: Make It Nondeterministic

    C. Design Tutorial: Make It Nondeterministic time limit per test 2 seconds memory limit per test 256 ...

  7. cf472B Design Tutorial: Learn from Life

    B. Design Tutorial: Learn from Life time limit per test 1 second memory limit per test 256 megabytes ...

  8. cf472A Design Tutorial: Learn from Math

    A. Design Tutorial: Learn from Math time limit per test 1 second memory limit per test 256 megabytes ...

  9. Codeforces Round #270--B. Design Tutorial: Learn from Life

    Design Tutorial: Learn from Life time limit per test 1 second memory limit per test 256 megabytes in ...

随机推荐

  1. 剑指offer--4.重建二叉树

    题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2, ...

  2. Python—函数的名称空间

    名称空间 又名name space, 顾名思义就是存放名字的地方,存什么名字呢?举例说明,若变量x=1,1存放于内存中,那名字x存放在哪里呢?名称空间正是存放名字x与1绑定关系的地方 名称空间共3种, ...

  3. vuex原理

    Vuex 框架原理与源码分析 vuex状态管理到底是怎样一个原理? 状态管理 Vuex框架原理与源码分析 Vuex实现原理解析 Vue刚出不久,Vuex 就出来了,想请教下Vuex做了什么事情? 个人 ...

  4. Codeforces Round #481 (Div. 3)Petya's Exams CodeForces - 978G

    Petya studies at university. The current academic year finishes with nn special days. Petya needs to ...

  5. c#中用sql存储过程

    string connstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionStri ...

  6. html总结:文本框填满表格

    <style> input { width: 100%; }</style>

  7. fork分支与源分支同步代码

    最进软件工程课程要团队开发做个网站项目,于是我在团队里推了使用github这种网站来协同开发,但是出现了个问题:fork后的代码无法 与源分支代码同步,导致fork分支的代码只有自己写的那部分,而不是 ...

  8. [转帖]你所不知道的C和C++运行库

    [C-C++]你所不知道的C和C++运行库 https://blog.csdn.net/humanking7/article/details/85887884 原作者也是转的blog 最近一个物理机上 ...

  9. STL 序列容器

    转自时习之 STL中大家最耳熟能详的可能就是容器,容器大致可以分为两类,序列型容器(SequenceContainer)和关联型容器(AssociativeContainer)这里介绍STL中的各种序 ...

  10. Spring boot 将配置文件属性注入到一个bean中

    现在要做的就是将如下配置文件中的内容注入到一个bean 名为Properties中. Redis.properties配置文件中的内容如下: Properties java bean中代码如下,注意注 ...