hdu 4339 Query(线段树+二分,4级)

Query

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


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
1 aaabba aabbaa 7 2 0 2 1 2 2 2 3 1 1 2 b 2 0 2 3
 

Sample Output
Case 1: 2 1 0 1 4 1
 

Source
 

Recommend
zhoujiaqi2010

思路:线段树单点更新,区间求和,把位置相同元素相同的置1,不同为0 ,答案就是连续1的个数,因此可以用区间求和来实现,当区间的和等于区间大小时就说明整个区间都是1,这个查询用二分就很容易搞定了。

惨痛的教训:编码一定一定要认真,宁可慢一些,也一定设法1A

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define lson t<<1
#define rson t<<1|1
#define mid (l+r)/2
using namespace std;
const int mm=1e6+9;
class node
{
public:
    int l,r,sum;
} rt[mm*4];
char s[3][mm];
int ls[3];
int p[mm],num;
void build(int t,int l,int r)
{
    rt[t].l=l;
    rt[t].r=r;
    rt[t].sum=0;
    if(l==r)
    {
        rt[t].sum=p[l];
        return;
    }
    build(lson,l,mid);
    build(rson,mid+1,r);
    rt[t].sum=rt[lson].sum+rt[rson].sum;
}
void update(int t,int pos,int dec)
{
    if(rt[t].l==rt[t].r&&rt[t].l==pos){rt[t].sum+=dec; return ;}
    if(rt[lson].r>=pos)update(lson,pos,dec);
    else update(rson,pos,dec);
    rt[t].sum=rt[lson].sum+rt[rson].sum;
}
int get_query(int t,int l,int r)
{
    if(rt[t].l==l&&rt[t].r==r)return rt[t].sum;
    if(rt[lson].r>=r)return get_query(lson,l,r);
    else if(rt[rson].l<=l)return get_query(rson,l,r);
    else return get_query(lson,l,rt[lson].r)+get_query(rson,rt[rson].l,r);
}
int query(int t,int l,int r)
{
    int ans=0,sl=l,ret,sr=r,midx;
    while(sl<=sr)
    {
        midx=(sl+sr)/2;
        ret=get_query(1,l,midx);
        if(ret==midx-l+1)
        {
            sl=midx+1;
            ans=max(ans,ret);
        }
        else sr=midx-1;
    }
    return ret;
}
int main()
{
    int cas;
    int a,b,c;
    char d;
    while(~scanf("%d",&cas))
    {
        for(int ca=1; ca<=cas; ++ca)
        {  printf("Case %d:\n",ca);
            scanf("%s%s",s[1],s[2]);
            ls[1]=strlen(s[1]);
            ls[2]=strlen(s[2]);
            memset(p,0,sizeof(p));
            int len=min(ls[1],ls[2]);
            for(int i=len-1; i>=0; --i)
                if(s[1][i]==s[2][i])p[i]=1;
                else p[i]=0;
                build(1,0,len-1);
            scanf("%d",&num);
            while(num--)
            {
                scanf("%d%d",&a,&b);
                if(a==1)
                {
                    scanf("%d %c",&c,&d);
                    if(c>len-1)continue;
                    if(d==s[3-b][c]&&s[3-b][c]!=s[b][c])
                        update(1,c,1);
                    else if(s[b][c]==s[3-b][c]&&d!=s[3-b][c])
                    {
                        update(1,c,-1);
                    }
                    s[b][c]=d;
                }
                else
                {
                    if(b>len-1)printf("0\n");
                    else printf("%d\n",query(1,b,len-1));
                }
            }
        }
    }
    return 0;
}






posted @ 2013-06-22 18:58  剑不飞  阅读(190)  评论(0编辑  收藏  举报