线段树(1)

Query

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
#include<cstdio>
#include<iostream>
#include<cstring>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 1e6+5;
using namespace std;
int s[maxn<<2];
void pushup(int l,int m,int rt){
    if(s[rt<<1]==m-l+1){
        s[rt] = s[rt<<1] +s[rt<<1|1];
    }
    else s[rt] = s[rt<<1];
}
char ch1[maxn],ch2[maxn];
void build(int l,int r,int rt){
    if(l==r){
        if(ch1[l]==ch2[l])
            s[rt] = 1;
        else s[rt] = 0;
        return;
    }
    int m = (l+r)>>1;
    build(lson);
    build(rson);
    pushup(l,m,rt);
}

void update(int l,int r,int rt,int a,int b,char ch){
    if(l==r){
        if(a==1)ch1[b] = ch;
        else ch2[b] = ch;
        if(ch1[b]==ch2[b])
            s[rt] = 1;
        else s[rt] = 0;
        return;
    }
    int m = (l+r)>>1;
    if(b <= m)update(lson,a,b,ch);
    else update(rson,a,b,ch);
    pushup(l,m,rt);
}

int query(int l,int r,int rt,int a){
    if(l==r)
        return s[rt];
    int m = (l+r)>>1;
    if(a <= m){
        int temp = query(lson,a);
        if(temp==m-a+1)
            return temp + s[rt<<1|1];
        return temp;
    }
    return query(rson,a);
}

int main(){
    int T,cas = 0;
    scanf("%d",&T);
    while(T--){
        scanf("%s",ch1);
        scanf("%s",ch2);
        int len1 = strlen(ch1);
        int len2 = strlen(ch2);
        int len = len1<len2?len1:len2;
        len--;
        build(0,len,1);
        int t;
        scanf("%d",&t);
        printf("Case %d:\n",++cas);
        while(t--){
            int c,a,b;
            char ch;
            scanf("%d",&c);
            if(c==1){
                scanf("%d%d",&a,&b);
                cin>>ch;
                if(b>len)continue;
                update(0,len,1,a,b,ch);
            }
            else {
                scanf("%d",&a);
                int ans = query(0,len,1,a);
                printf("%d\n",ans);
            }
        }
    }
    return 0;
}

 

posted @ 2015-08-25 21:18  Tobu  阅读(148)  评论(0)    收藏  举报