hdu 4339 Query
Query
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 531 Accepted Submission(s): 164
Your task is to answer next queries:
1) 1 a i c - you should set i-th character in a-th string to c;
2) 2 i - you should output the greatest j such that for all k (i<=k and k<i+j) s1[k] equals s2[k].
Next T blocks contain each test.
The first line of test contains s1.
The second line of test contains s2.
The third line of test contains Q.
Next Q lines of test contain each query:
1) 1 a i c (a is 1 or 2, 0<=i, i<length of a-th string, 'a'<=c, c<='z')
2) 2 i (0<=i, i<l1, i<l2)
All characters in strings are from 'a'..'z' (lowercase latin letters).
Q <= 100000.
l1, l2 <= 1000000.
Then for each query "2 i" output in single line one integer j.
AC代码:
#include <stdio.h>
#include <string.h>
char str[8][1000600];
int c[1000600];
int mx;
int lowbit( int x)
{
return x&(-x);
}
void updata( int x,int p)
{
while(x<=mx)
{
c[x]+=p;
x+=lowbit(x);
}
}
int sum( int x)
{
int s=0;
while(x>0)
{
s+=c[x];
x-=lowbit(x);
}
return s;
}
int main( )
{
int test,i,q,cnt;
int x,y,z,f,a;
char s[34];
scanf("%d",&test);
cnt=0;
while(test--)
{
memset(str,0,sizeof(str));
memset(c,0,sizeof(c));
scanf("%s",str[0]);
scanf("%s",str[1]);
for( mx=0;str[0][mx]&&str[1][mx];mx++);
for( i=0;i<mx;i++)
{
if(str[0][i]==str[1][i]) updata(i+1,1);
}
scanf("%d",&q);
printf("Case %d:\n",++cnt);
while(q--)
{
scanf("%d",&f);
if(f==2)
{
scanf("%d",&a);
++a;
int l=a,r=mx;
int mid;
while(l<=r)
{
mid=(l+r)>>1;
int k=sum(mid)-sum(a-1);
if(k>=mid-a+1) l=mid+1;
else r=mid-1;
}
printf("%d\n",r-a+1);
}
if(f==1)
{
scanf("%d%d%s",&x,&y,s);
x--;
char ch1=str[x^1][y],ch2=str[x][y];
str[x][y]=s[0];
if(ch1!=ch2&&ch1==s[0]) updata(y+1,1);
else if(ch1==ch2&&ch1!=s[0]) updata(y+1,-1);
}
}
}
return 0;
}