其他分享
首页 > 其他分享> > A - Subpalindromes URAL - 1989 (线段树)

A - Subpalindromes URAL - 1989 (线段树)

作者:互联网

一个字符串可以表示为: S[1]*P[1]+S[2]*P[2]+S[3]*P[3]+......,故当哈希冲突率低的时候,可以将字符串唯一表示为一个数字,本题线段树就是维护区间和。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<queue>
#include<stack>
using namespace std;

const int inf=0x3f3f3f3f;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=1e5+7;
ull sum1[maxn<<2|1],sum2[maxn<<2|1];
ull p=13331;
ull mi[maxn];
char s1[maxn],s2[maxn];

void pushdown(int k){
    sum1[k]=sum1[k<<1]+sum1[k<<1|1];
    sum2[k]=sum2[k<<1]+sum2[k<<1|1];
}

void build(int l,int r,int k){
    if(l==r){
        sum1[k]=(s1[l]-'a'+1)*mi[l];
        sum2[k]=(s2[l]-'a'+1)*mi[l];
        return ;
    }
    int mid=(l+r)>>1;
    build(l,mid,k<<1);
    build(mid+1,r,k<<1|1);
    pushdown(k);
}

void updata(int l,int r,int k,int id,char c,int f){//1改sum1,2改sum2;
    if(l==r){
        if(f==1){
            sum1[k]=(c-'a'+1)*mi[l];
            return ;
        }
        else{
            sum2[k]=(c-'a'+1)*mi[l];
            return ;
        }
    }
    int mid=(l+r)>>1;
    if(id<=mid) updata(l,mid,k<<1,id,c,f);
    else updata(mid+1,r,k<<1|1,id,c,f);
    pushdown(k);
}


ull myfind(int L,int R,int l,int r,int k,int f){
    if(l>=L&&r<=R){
        if(f==1) return sum1[k];
        else return sum2[k];
    }
    int mid=(l+r)>>1;
    ull res=0;
    if(L<=mid) res+=myfind(L,R,l,mid,k<<1,f);
    if(R>mid) res+=myfind(L,R,mid+1,r,k<<1|1,f);
    return res;
}
char s[99];
int main(){
    scanf("%s",s1+1);
    int n=strlen(s1+1);

    mi[0]=1;
    for(int i=1;i<=n;++i)
        mi[i]=mi[i-1]*p,s2[i]=s1[n+1-i];
    build(1,n,1);
    int l,r,id;
    int q;
    scanf("%d",&q);
    while(q--){
        scanf("%s",s);
        if(s[0]=='p'){
            scanf("%d%d",&l,&r);
            ull res1=myfind(l,r,1,n,1,1);
            ull res2=myfind(n+1-r,n+1-l,1,n,1,2);
            if(l<n+1-r) res1*=mi[n+1-r-l];
            else res2*=mi[l-n-1+r];
            //cout<<res1<<" "<<res2<<endl;

            printf("%s\n",(res1==res2?"Yes":"No"));

        }
        else{
            scanf("%d%s",&id,s);
            updata(1,n,1,id,s[0],1);
            updata(1,n,1,n-id+1,s[0],2);
        }

    }

    return 0;
}

 

标签:typedef,int,mid,long,1989,ull,URAL,include,Subpalindromes
来源: https://blog.csdn.net/chenyume/article/details/89553165