HDU 4339 (line segment tree Character Mark)

Source: Internet
Author: User

The two strings S1 and S2 are given several queries.

Query operation:

1 a B c: replace character B of character a with character C

2. A queries the number of S1 [k] = S2 [k] starting from the character. For example, aaa aab queries 2 0 and the result is 2.


Idea: the leaf value of a line segment tree is set to I + 1 with different positions, initially Len + 1. the minimum value of the interval is not queried, that is, the first position of the Line Segment tree;


#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<algorithm>#define N 1000010#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;char s[2][N];int val[N<<2];int INF;void pushup(int rt){    val[rt]=min(val[rt<<1],val[rt<<1|1]);}void build(int l,int r,int rt){    val[rt]=INF;    if(l==r)        return;    int m=(l+r)>>1;    build(lson);    build(rson);}void update(int p,int v,int l,int r,int rt){    if(l==r)    {        val[rt]=v;        return;    }    int m=(l+r)>>1;    if(p<=m) update(p,v,lson);    else update(p,v,rson);    pushup(rt);}int query(int L,int R,int l,int r,int rt){    //cout<<"---"<<endl;    int ans=INF;    if(L<=l&&r<=R)    {        return val[rt];    }    int m=(l+r)>>1;    if(L<=m) ans=min(ans,query(L,R,lson));    if(R>m) ans=min(ans,query(L,R,rson));    return ans;}int main(){    int T;    scanf("%d",&T);    for(int cas=1;cas<=T;cas++)    {        scanf("%s%s",s[0],s[1]);        int len=min(strlen(s[0]),strlen(s[1]));        INF=len+1;        build(1,len,1);        for(int i=0;i<len;i++)        {            if(s[0][i]==s[1][i]) update(i+1,INF,1,len,1);            else update(i+1,i+1,1,len,1);        }        int n,op;        int a,b;        char c[2];        scanf("%d",&n);        printf("Case %d:\n",cas);        for(int i=0;i<n;i++)        {            scanf("%d",&op);            if(op==2)            {                scanf("%d",&a);                if(a>=len)                    printf("0\n");                else                    printf("%d\n",query(a+1,len,1,len,1)-a-1);            }            else            {                scanf("%d%d%s",&b,&a,c);                if(a>=len)                    continue;                b--;                if(s[b][a]==s[b^1][a]&&s[b^1][a]!=c[0]) update(a+1,a+1,1,len,1);                if(s[b][a]!=s[b^1][a]&&s[b^1][a]==c[0]) update(a+1,INF,1,len,1);                s[b][a]=c[0];            }        }    }    return 0;}


HDU 4339 (line segment tree Character Mark)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.