树状数组,主要是抓住要求连续1的个数。这样,初始时,相同的加1,不同的加0.

查询时,用二分搜索右边界。就是比较当前mid-l+1的值与他们之间1的个数(这可以通过树状数组求区间和得出),记录右边界即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string.h>
#define lowbit(x) ((x)&(-(x))) using namespace std;
const int N= 1000015; char str1[N],str2[N];
int ptree[N]; int sum(int *a,int x){
if(x==0) return 0;
int s=0;
for(;x;x-=lowbit(x))s+=a[x];
return s;
} void update(int *a,int x,int w,int &n){
for(;x<=n;x+=lowbit(x))a[x]+=w;
} int ans[100005]; int binSearch(int l,int r){
int p=l-1,tmp;
int spre=sum(ptree,p);
int ans=l;
while(l<=r){
int mid=(l+r)/2;
tmp=sum(ptree,mid);
if(tmp-spre<mid-p)
r=mid-1;
else{
ans=mid;
l=mid+1;
}
}
return ans-p;
} int main(){
int T,Q,kase=0;
int ch,a,ii; char c;
scanf("%d",&T);
while(T--){
cin>>str1+1;
cin>>str2+1;
int len=min(strlen(str1+1),strlen(str2+1));
memset(ptree,0,sizeof(int)*(len+5));
for(int i=1;i<=len;i++){
if(str1[i]==str2[i])
update(ptree,i,1,len);
}
scanf("%d",&Q);
printf("Case %d:\n",++kase);
for(int i=1;i<=Q;i++){
cin>>ch;
if(ch==2){
cin>>a;
if(str1[a+1]!=str2[a+1]){
printf("%d\n",0);
}
else{
printf("%d\n",binSearch(a+1,len));
}
}
else{
cin>>a>>ii>>c;
// cout<<a<<ii<<c<<endl;
if(ii+1>len) continue;
if(a==1){
if(str1[ii+1]==str2[ii+1]){
str1[ii+1]=c;
if(str1[ii+1]==str2[ii+1])
continue;
else
update(ptree,ii+1,-1,len);
}
else{
str1[ii+1]=c;
if(str1[ii+1]==str2[ii+1])
update(ptree,ii+1,1,len);
}
}
else{
if(str1[ii+1]==str2[ii+1]){
str2[ii+1]=c;
if(str1[ii+1]==str2[ii+1])
continue;
else
update(ptree,ii+1,-1,len);
}
else{
str2[ii+1]=c;
if(str1[ii+1]==str2[ii+1])
update(ptree,ii+1,1,len);
}
}
}
}
}
return 0;
}

  

HDU 4339 Contest 4的更多相关文章

  1. HDU 5045 Contest(状压DP)

    Problem Description In the ACM International Collegiate Programming Contest, each team consist of th ...

  2. hdu - 5045 - Contest(国家压缩dp)

    意甲冠军:N个人M通过主打歌有自己的期望,每个问题发送人玩.它不能超过随机播放的次数1,追求最大业绩预期 (1 ≤ N ≤ 10,1 ≤ M ≤ 1000). 主题链接:pid=5045" ...

  3. [ACM] hdu 5045 Contest (减少国家Dp)

    Contest Problem Description In the ACM International Collegiate Programming Contest, each team consi ...

  4. hdu 4339 Query(两种思路求解)

    Query Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  5. HDU–5988-Coding Contest(最小费用最大流变形)

    Coding Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  6. hdu 5045 Contest(状态压缩DP)

    题解:我们使用一个二位数组dp[i][j]记录进行到第i个任务时,人组合为j时的最大和(这里的j我们用二进制的每位相应一个人). 详细见代码: #include <iostream> #i ...

  7. HDU 5045 Contest

    pid=5045">主题链接~~> 做题感悟:比赛时这题后来才写的,有点小尴尬.两个人商议着写写了非常久才写出来,I want to Powerful ,I believe me ...

  8. HDU - 4339: Query(bitset暴力找下一个为1的)

    题意:给定A,B长度相同的字符串,Q次操作,修改操作位单个字符修改,查询操作为询问从某点开始有多少连续相同的字符. 思路:我们把不相同的设为1,相同的设为0,那么询问就是找下一个为1的为位置,可以用线 ...

  9. HDU 4335 Contest 4

    利用降幂公式..呃,还是自己去搜题解吧.知道降幂公式后,就不难了. #include <iostream> #include <cstdio> #include <alg ...

随机推荐

  1. BA--干球温度、露点温度和湿球温度--概念

    1. 干球温度.露点温度和湿球温度 dry bulb temperature, dew temperature, and wet-bulb temperature 摘要:未饱和湿空气中水蒸汽处于过热状 ...

  2. linux下添加自定义脚本到开机自启动的方法

    原文链接:http://www.jb51.net/LINUXjishu/183462.html 我的机器有个coreseek服务,但是没加到开启启动中去,导致机房一旦重启了机器,我的服务便不能使用了. ...

  3. openwrt针对RT5350代码下载,配置和编译

    转载地址:http://blog.csdn.net/dean_gdp/article/details/37091685 近期买了块官方板的RT5350: 先介绍代码下载.下面命令都是用登录用户运行,无 ...

  4. 启用QNX系统,海尔智能冰箱或成业界“宝马”

        智能家电正处于迅猛发展的态势,国内眼下有非常多企业都在积极布局智能家电,当中又以海尔最为典型.作为家电领域的领头羊,海尔近年来在智能家电领域的动作不小.近期有消息透露.海尔也许会在IFA展会上 ...

  5. 【POJ 1733】 Parity Game

    [题目链接] http://poj.org/problem?id=1 [算法] 并查集 [代码] #include <algorithm> #include <bitset> ...

  6. 关于1-n任意的gcd的和

    gcd和 题目 GCD sum 公约数的和 大意是让你求1-n任意两个数的gcd的和之类的. 解法 显然你需要枚举对吧,不然你怎么可能求出gcd呢? 其次我们需要一些数学推理 令F(n)表示\(\su ...

  7. 子线程中刷新了UI

    This application is modifying the autolayout engine from a background thread, which can lead to engi ...

  8. BZOJ 3323 splay维护序列

    就第三个操作比较新颖 转化成 在l前插一个点 把r和r+1合并 //By SiriusRen #include <cstdio> #include <cstring> #inc ...

  9. BZOJ 4522 Pollard-rho+exgcd

    思路: N=P*Q 求出来P和Q 模拟就好- //By SiriusRen #include <cstdio> #include <algorithm> using names ...

  10. MacOS系统下简单安装以及配置MongoDB数据库(一)

    最近写了一个用node来操作MongoDB完成增.删.改.查.排序.分页功能的示例,并且已经放在了服务器上地址:http://39.105.32.180:3333. 项目一共四部分: 1.MacOS下 ...