HDU 4339 Query(线段树)

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
 
并不难的线段树题目,然而比赛的时候没有做出来。。。准确的说是没有做。。。
首先是时间不够,看了一眼觉得区间合并会很麻烦,再加上线段树写得不是很溜,就没写
赛后仔细一看发现其实这题的区间合并蛮简单的。。。。。
保存区间[l,r]以l为起点的最长段为s
合并的时候只需要判断左子的s是否覆盖了左子的整个区间
若覆盖,则加上右子的s即可
另外,注意两个的串的长度没有说相等。。。
具体操作见代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<set>
#include<stack>
#include<cmath>
#include<map>
#include<stdlib.h>
#include<cctype>
#define mem(a,x) memset(a,x,sizeof(a))
#define esp 1e-8
using namespace std;
typedef long long ll;
const int N = 1000000;
struct Node
{
    int l,r;
    int s;//区间[l,r]内以l为起点的最长相等段
}q[N*4];
string a,b;
void build(int i,int l,int r)
{
    q[i].l = l;q[i].r = r;
    if (l == r)
    {
        if (a[l] == b[l]) q[i].s = 1;
        else q[i].s = 0;
        return;
    }
    int mid = (l+r)>>1;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
    q[i].s = q[i<<1].s;//先等于左子的最大长度
    if (q[i<<1].s == mid - l + 1)//如果左子的整个区间都相等
    {
        q[i].s += q[i<<1|1].s;//那么还可以加上右子
    }
}
void update(int i,int x)//更改了第x个字符
{
    if (q[i].l == q[i].r)
    {
        if (a[q[i].l] == b[q[i].l]) q[i].s = 1;
        else q[i].s = 0;
        return ;
    }
    int mid = (q[i].l + q[i].r)>>1;
    if (x <= mid) update(i<<1,x);
    else update(i<<1|1,x);
    q[i].s = q[i<<1].s;//先等于左子的最大长度
    if (q[i<<1].s == mid - q[i].l + 1)//如果左子的整个区间都相等
    {
        q[i].s += q[i<<1|1].s;//那么还可以加上右子
    }
}
int query(int i,int x)
{
    if (q[i].l == q[i].r)
    {
        return q[i].s;
    }
    int mid = (q[i].l + q[i].r)>>1;
    if (x <= mid)
    {
        int tmp = query(i<<1,x);
        if (tmp == mid - x + 1) return tmp + q[i<<1|1].s;
        else return tmp;
    }
    else return query(i<<1|1,x);
}
int main()
{
    int T;scanf("%d",&T);int kas = 0;
    while (T--)
    {
        cin>>a>>b;
        int la = a.size(),lb = b.size();
        int n = min(la,lb)-1;
        build(1,0,n);
        int q;scanf("%d",&q);
        printf("Case %d:\n",++kas);int op,x,i;char c;
        while (q--)
        {
            scanf("%d",&op);
            if (op == 1)
            {
                scanf("%d %d %c",&x,&i,&c);
                if (i > n) continue;//越界的不用更新了
                if (x == 1) a[i] = c;
                else b[i] = c;
                update(1,i);
            }
            else
            {
                scanf("%d",&i);
                if (i > n) puts("0");
                else printf("%d\n",query(1,i));
            }
        }
    }
    return 0;
}

线段树还是不够熟练啊。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值