HDU3973 AC's String

HDU3973 AC's String

 

 

 

#include<bits/stdc++.h>
#define ull unsigned long long
using namespace std;
const int N=500011,L=2000011;
const ull St=233;
int T,n,m;
ull jc[N];
char ss[L],s[L];
set<ull> q;

struct Node {
    int l,r;
    ull data;
};
Node t[N];

inline int re_ad() {
    char ch=getchar(); int x=0,f=1;
    while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while('0'<=ch && ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    return x*f;
}

inline void init() {
    jc[0]=1;
    for(int i=1;i<=strlen(s+1);++i) jc[i]=jc[i-1]*St;
}

inline ull get_hash() {
    ull res=0;
    for(int i=0;i<strlen(ss);++i) res=res*St+ss[i]-'a'+1;
    return res;
}

inline void pushup(int d) {
    int mid=(t[d].l+t[d].r)>>1;
    t[d].data=t[d<<1].data*(ull)(t[d].r-mid)*St+t[d<<1|1].data;
}

void build(int d,int x,int y) {
    t[d].l=x,t[d].r=y;
    if(x==y) {
        t[d].data=(ull)(s[x]-'a'+1);
        return ;
    }
    int mid=(x+y)>>1;
    build(d<<1,x,mid);
    build(d<<1|1,mid+1,y);
    pushup(d);
}

void update(int d,int x,ull y) {
    if(t[d].l==t[d].r) {
        t[d].data=y;
        return ;
    }
    int mid=(t[d].l+t[d].r)>>1;
    if(x<=mid) update(d<<1,x,y);
    else update(d<<1|1,x,y);
    pushup(d);
}

ull query(int d,int x,int y) {
    if(x<=t[d].l && t[d].r<=y) {
        return t[d].data;
    }
    int mid=(t[d].l+t[d].r)>>1;
    ull res=0;
    if(mid>=y) return query(d<<1,x,y);
    if(mid<x) return query(d<<1|1,x,y);
    return query(d<<1,x,mid)*jc[y-mid]+query(d<<1|1,mid+1,y);
}

inline void Query(int x,int y) {
    ull res=query(1,x,y);
//    printf("%llu ",res);
    set<ull>::iterator it=q.find(res);
    if(it==q.end()) printf("No\n");
    else printf("Yes\n");
}

int main()
{
    char goal,z;
    int x,y;
    init();
    T=re_ad();
    while(T--) {
        memset(t,0,sizeof(t));
        q.clear();
        n=re_ad();
        for(int i=1;i<=n;++i) scanf("%s",ss),q.insert(get_hash());
        scanf("%s",s+1);
        build(1,1,strlen(s+1));
        m=re_ad();
        for(int i=1;i<=m;++i) {
            goal='0';
            while(goal!='Q' && goal!='C') goal=getchar();
            if(goal=='C') {
                x=re_ad(); z='0';
                while(z<'a' || z>'z') z=getchar();
                update(1,x+1,(ull)(z-'a'+1));
            }
            else x=re_ad(),y=re_ad(),Query(x+1,y+1);
        }
    }
//    set<ull>::iterator iter;
//    for(iter=q.begin();iter!=q.end();++iter) printf("%llu ",*iter); puts("");
    return 0;
}

 

上一篇:CF 580E Kefa and Watch


下一篇:字符串哈希