hdu(4339)树状数组+二分查找

/*
   s1[i]与s2[i]匹配,树状数组i位置更新1,否则更新0。
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = 1000005;
int c[maxn];
char s1[maxn],s2[maxn];
int min(int a,int b){
    return a>b?b:a;
}
int lowbit(int x){
    return x&(-x);
}
void update(int x,int a){
    for(int i = x;i < maxn;i+=lowbit(i)){
        //printf("%d\n",i);
        c[i] += a;
    }
}
int getsum(int x){
    int ans = 0;
    for(int i = x;i > 0;i-=lowbit(i))
        ans += c[i];
    return ans;
}
int judge(int l,int mid){
    if(getsum(mid) - getsum(l-1) == mid - l + 1)
        return 1;
    return 0;
}
int bs(int l,int r){
    int low = l,high = r;
    while(low<=high){
        int mid = (low+high)>>1;
        if(judge(l,mid))
            low = mid+1;
        else
            high = mid-1;
    }
    return low - l;
}
int main(){
   // freopen("in.txt","r",stdin);
  //  freopen("out.txt","w",stdout);
    int t;
    scanf("%d",&t);
    for(int cas=1;cas<=t;cas++){
        printf("Case %d:\n",cas);
        scanf("%s",s1);
        scanf("%s",s2) ;
        memset(c,0,sizeof(c));
        int l1 = strlen(s1);
        int l2 = strlen(s2);
        int len = min(l1,l2);
        //printf("%d->\n",len);
        for(int i = 0;i < len;i++){
            if(s1[i] == s2[i])
                update(i+1,1);
        }
        //for(int i = 0;i < len;i++)
          //  printf("%d ",getsum(i+1));
        int q;
        scanf("%d",&q);
        while(q--){
            int ss;
            scanf("%d",&ss);
            if(ss == 1){
                int a,i;
                char ch;
                scanf("%d %d %c",&a,&i,&ch) ;
                int same=(s1[i]==s2[i]) ;
                if(a == 1){
                    s1[i] = ch;
                    if(same&&s1[i]!=s2[i])
                        update(i+1,-1) ;
                    else if(!same&&s1[i]==s2[i])
                        update(i+1,1) ;
                }
                else if(a == 2){
                    s2[i] = ch;
                    if(same&&s1[i]!=s2[i])
                        update(i+1,-1) ;
                    else if(!same&&s1[i]==s2[i])
                        update(i+1,1) ;
                }
            }
            else{
                int i;
                scanf("%d",&i);
                printf("%d\n",bs(i+1,maxn));
            }
        }
    }
    return 0;
}
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值