HDU4339 Query (线段树)

Query

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


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

2012 Multi-University Training Contest 4 

http://acm.split.hdu.edu.cn/showproblem.php?pid=4339

//题意有点坑爹

先是多少组样例

再给出两组字符串 分别是 1,2         下标从 0开始

给出操作次数

(2种操作) 2 查询 从下标 i开始两个字符串对应相等最长延伸的长度是多少

(从零开始    aabaaaaaaa   aacaaaaaaaaaaaaaaaaaaa  就是2)

1操作 就是把第a串下标为i的坐标变成c

好久没写线段树又被卡住了

从下标为i开始一直延续下去 只要不相同便中断

线段树节点保存的信息是 一节点最左端开始所能延续的最大长度 


#include<bits/stdc++.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int maxx=1e6+5;

char str[2][maxx];
int val[maxx*4];

void build(int l,int r,int rt)
{
    if(l==r)
    {
        if(str[0][l]==str[1][l])
            val[rt]=1;
        else val[rt]=0;
        return ;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    val[rt]=val[rt<<1];
    if(val[rt<<1]==mid-l+1) //重点
        val[rt]+=val[rt<<1|1];
}

void update(int a,int p,char v,int l,int r,int rt)
{
    if(l==r)
    {
        str[a][p]=v;
        val[rt]=str[0][p]==str[1][p];
        return ;
    }
    int mid=(l+r)>>1;
    if(p<=mid)
        update(a,p,v,lson);
    else
        update(a,p,v,rson);
    val[rt]=val[rt<<1];
    if(val[rt<<1]==mid-l+1)
        val[rt]+=val[rt<<1|1];
}

int query(int p,int l,int r,int rt)
{
    if(l==p)  return val[rt];//这个区间的左端点

    int mid=(l+r)>>1;
    int ans;
    if(p<=mid)
    {
        ans=query(p,lson);
        if(ans==mid-p+1)  //全部连续相等还得加上右边的情况
            ans+=query(mid+1,rson);//下一步必然直接返回
    }
    else    ans=query(p,rson);
    return ans;
}

int main()
{
    int tt;
    int T=0;
    scanf("%d",&tt);
    int i,len,q,x,p,a;
    char c[10];
    while(tt--)
    {
        scanf("%s%s",str[0]+1,str[1]+1);
        scanf("%d",&q);
        printf("Case %d:\n",++T);
        i=1,len=0;
        while(str[0][i]&&str[1][i])
        {
            len++;
            i++;
        }

        build(1,len,1);

        while(q--)
        {
            scanf("%d",&x);
            if(x==2)
            {
                scanf("%d",&p);
                if(p+1>len) printf("0\n");
                else printf("%d\n",query(p+1,1,len,1));
            }
            else
            {
                scanf("%d%d%s",&a,&p,c);
                if(p+1>len) continue;
                update(a-1,p+1,c[0],1,len,1);
            }
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值