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; }
原文地址:https://www.cnblogs.com/Roly/p/3050516.html