HDU 4339 Query(线段树+搜索技巧)

Query

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


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   |   We have carefully selected several similar problems for you:   4338  4331  4332  4333  4334 
#include <queue>
#include <iostream>
#include<cstring>
#include<string>
#include<stdio.h>
#define maxn 1000000
using namespace std;
string s1,s2;
int ub,lb;
int q,a,i;
char c;
struct node
{
    int l,r;
    int maxl;
    node()
    {
        l=r=maxl=0;
    }
};
node seq[maxn<<2];
void pushup(int rt)
{
    if(seq[rt<<1].maxl==seq[rt<<1].r-seq[rt<<1].l+1)
        seq[rt].maxl=seq[rt<<1].maxl+seq[rt<<1|1].maxl;
    else
        seq[rt].maxl=seq[rt<<1].maxl;
}

void build(int l,int r,int rt)
{
    seq[rt].l=l;
    seq[rt].r=r;
    if(seq[rt].l==seq[rt].r)
    {
        seq[rt].maxl=0;
        if(l<=lb&&s1[l-1]==s2[l-1])
            seq[rt].maxl=1;
        return ;
    }
    int mid=(l+r)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    pushup(rt);
}

void update(int p,int type,int rt,char c)
{
    if(seq[rt].l==seq[rt].r)
    {
        if(type==1)
        {
            s1[seq[rt].l-1]=c;
            seq[rt].maxl=0;
            if(c==s2[seq[rt].l-1]) seq[rt].maxl=1;
        }
        else
        {
           s2[seq[rt].l-1]=c;
           seq[rt].maxl=0;
            if(c==s1[seq[rt].l-1]) seq[rt].maxl=1;
        }
        return ;
    }
    int mid=(seq[rt].l+seq[rt].r)>>1;
    if(mid>=p)
        update(p,type,rt<<1,c);
    else
       update(p,type,rt<<1|1,c);
    pushup(rt);
}

int query(int l,int rt)
{
    if(seq[rt].l==l)
        return seq[rt].maxl;
    int mid=(seq[rt].l+seq[rt].r)>>1;
    int ans=0;
    if(mid>=l)
    {
        ans=query(l,rt<<1);
        if(ans==mid-l+1)
            ans+=seq[rt<<1|1].maxl;
    }
    else
        ans=query(l,rt<<1|1);
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    int t;cin>>t;
    for(int ca=1;ca<=t;ca++)
    {
        cin>>s1>>s2;
        //ub=max(s1.size(),s2.size());
        lb=min(s1.size(),s2.size());
        build(1,lb,1);
        printf("Case %d:\n",ca);
        int m;cin>>m;
        while(m--)
        {
            cin>>q;
            if(q==1)
            {
                cin>>a>>i>>c;
                update(i+1,a,1,c);
            }
            else
            {
                cin>>a;
                a++;
               if(a>lb)  cout<<0<<endl;
               else  cout<<query(a,1)<<endl;
            }
        }
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值