HDOJ 4399 —— 线段树单点更新&单点查询

Query

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


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 x y d:把第x个字符串的第y个元素改成d。
2 x :输出从x位开始的最长公共字符串。
思路是把两字符串相等的部分记为1,不同记为0.然后我们每次去查询以该位置为左端点最大连续1的长度。
我们用线段树去维护两个域,suml和sumr,表示当前节点左边的连续1的数量和右边的连续1的数量。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>

using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
//#define mid ((l + r) >> 1)
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 1000000 + 500;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x3f3f3f3f;
const int IMIN = 0x80000000;
const double e = 2.718281828;
#define eps 1e-8
#define DEBUG 1
#define mod 1000000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
///#pragma comment(linker, "/STACK:102400000,102400000")__int64 a[10050];
char str[3][MAXN] ;
int n , lenmin , lenmax;
struct t
{
    int l , r,  sumr , suml ;
}t[MAXN << 2];
void PushUp(int rt , int num)
{
    t[rt].suml = t[rt << 1].suml;
    t[rt].sumr = t[rt << 1 | 1].sumr;
    if(t[rt].suml >= num - (num >> 1))
    {
        t[rt].suml += t[rt << 1 | 1].suml;
    }
    if(t[rt].sumr >= (num >> 1))
    {
        t[rt].sumr += t[rt << 1].sumr;
    }
}
void build(int l , int r , int rt)
{
    if(l == r)
    {
        if(l < lenmin)
        {
//            t[rt].l = l , t[rt].r = r;
            t[rt].suml = t[rt].sumr = (str[1][l] == str[2][r]) ? 1 : 0;

        }
        else
        {
//            t[rt].l = l , t[rt].r = r;
            t[rt].suml = t[rt].sumr = 0;
        }

        return ;
    }
    int mid = (l + r) >> 1;
    build(lson) , build(rson);
    PushUp(rt , r - l + 1);
}
void update(int x , int L , int R , int rt)
{
//    if(t[rt].l > R || t[rt].r < L)return ;
    if(L == R)
    {
        if(L < lenmin)
        {

            t[rt].suml = t[rt].sumr = (str[1][L] == str[2][R]) ? 1 : 0;
        }
        else t[rt].suml = t[rt].sumr = 0;
        return;
    }
    int mid = (L + R) >> 1;
    if(x <= mid)update(x , L , mid , rt << 1);
    else update(x , mid  + 1, R , rt  << 1 | 1);
    PushUp(rt , R - L + 1);
}

int query(int x , int L ,int R , int rt)
{
//    if(t[rt].l > R || t[rt].r < L)return 0;
    if(L  == R)
    {
        return 1;
    }
    int mid = (L + R) >> 1;
    if(x <= mid)
    {
        if(t[rt << 1].sumr > mid - x)
            return  mid - x + 1 + t[rt << 1 | 1].suml;
        else return query(x , L ,mid , rt << 1);
    }
    else return query(x , mid  + 1,R , rt << 1 | 1);
}

int main()
{
    int  t;
    cin >> t;
    FORR(kase , 1 , t)
    {
        scanf("%s%s" , str[1] , str[2]);
        scanf("%d" , &n);
        lenmin = min(strlen(str[1]) , strlen(str[2]));
        lenmax = max(strlen(str[1]) , strlen(str[2]));
        build(0 , lenmax - 1 , 1);
//        outstars
//        FOR(i ,0 , len)cout << "*************"<<w[i] << endl;
        printf("Case %d:\n" , kase);
        while(n--)
        {
            int op , x , y ;
            char ch;
            scanf("%d" , &op);
//            outstars
            if(op == 1)
            {
                scanf("%d%d %c" , &x , &y , &ch);
                str[x][y] = ch;
//                cout << "$$$$$$$$$$$$$$$$$" << str[x][y] << endl;
//                outstars
                update(y , 0 , lenmax - 1 , 1);
            }
            else
            {
                scanf("%d" , &x);
                if(x >= lenmin || str[1][x] != str[2][x])
                {
                    puts("0");
                    continue;
                }
//                outstars
                printf("%d\n" , query(x, 0 , lenmax - 1 , 1));
//                outstars
            }
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值