hdu4339 Query 树状数组

来源:互联网 发布:qq同步助手java版 编辑:程序博客网 时间:2024/04/28 08:41

Query

Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2087 Accepted Submission(s): 720


Problem Description
You are given two strings s1[0..l1], s2[0..l2] and Q - number of queries.
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].

Input
The first line contains T - number of test cases (T<=25).
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.

Output
For each test output "Case t:" in a single line, where t is number of test (numbered from 1 to T).
Then for each query "2 i" output in single line one integer j.

Sample Input
1aaabbaaabbaa72 02 12 22 31 1 2 b2 02 3

Sample Output
Case 1:210141
树状数组就是好啊,这题把两个串中相等的化成1不等的化成0,用树状数组,就可以快速查出从i到j的和,如果这个和等于区间的宽度,不就说明,是相等的串么?我们还可以用二分更快的查出,但这题,同样的原理,用线段树就是过不了!看来线段树常数,还是太大了啊!
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;#define MAXN 1000005char str[2][MAXN];int prime[MAXN];int l[MAXN];int all,len;int lowbit(int x){    return x&(-x);}void update(int x,int c){    int i;    while(x<MAXN)    {        l[x]+=c;        x=x+lowbit(x);    }}int query(int b){    int i,sum=0,x=b;    while(x>0)    {        sum+=l[x];        x=x-lowbit(x);    }    return sum;}int  twocut(int x){    int s,e,mid,num;    x++;    s=x;e=len;    num=s-1;    while(s<=e)//kdfljdkfljsdlkf    {        mid=(s+e)>>1;       if(query(mid)-query(num)==mid-num)//·?o?        {            s=mid+1;        }        else        {            e=mid-1;        }    }    return e-num;}int fmin(int a,int b){    if(a<b)    return a;    return b;}int main(){   int tcase,tt,i,asknum,tempask,strnum,index;   char c;   scanf("%d",&tcase);   for(tt=1;tt<=tcase;tt++)   {       memset(l,0,sizeof(l));       memset(prime,0,sizeof(prime));       printf("Case %d:\n",tt);       getchar();       gets(str[0]);       //getchar();       gets(str[1]);       len=fmin(strlen(str[0]),strlen(str[1]));       //printf("%d\n",len);       for(i=0;i<len;i++)       {           if(str[0][i]!=str[1][i])           {               prime[i]=0;           }           else           {               prime[i]=1;               update(i+1,1);           }        }       all=0;       scanf("%d",&asknum);       while(asknum--)       {           scanf("%d",&tempask);           if(tempask==1)           {               scanf("%d%d %c",&strnum,&index,&c);               strnum--;//               str[strnum][index]=c;               if(c!=str[strnum^1][index]&&prime[index])               {                   prime[index]=0;                   update(index+1,-1);               }               else if(c==str[strnum^1][index]&&!prime[index])               {                   prime[index]=1;                   update(index+1,1);               }           }           else           {               scanf("%d",&index);               printf("%d\n",twocut(index));           }       }   }    return 0;}

原创粉丝点击