200 行代码,调不动了玄关。(hash+线段树)
查看原帖
200 行代码,调不动了玄关。(hash+线段树)
542009
ThisIsLu楼主2024/4/5 20:11
#include <bits/stdc++.h>
using namespace std;
int n,q;
const int hashmod[3]={998244353,1000000007,1000000033};
const int hashb=11451,operrere=0;
int hash_mod(const char str[],int b,int p){
    int res=0;
    for(int i=0;i<strlen(str);i++){
        res=(1ll*res*b+str[i]-operrere)%p;
    }
    return res;
}
struct hash_struct{
    int hash_value[3];
    hash_struct(){
        for(int i=0;i<3;i++){
            hash_value[i]=0;
        }
    }
    hash_struct(int hashvalue0,int hashvalue1,int hashvalue2){
        hash_value[0]=hashvalue0;
        hash_value[1]=hashvalue1;
        hash_value[2]=hashvalue2;
    }
    hash_struct(const string str){
        for(int i=0;i<3;i++) hash_value[i]=hash_mod(str.c_str(),hashb,hashmod[i]);
    }
    hash_struct(const char str[]){
        for(int i=0;i<3;i++) hash_value[i]=hash_mod(str,hashb,hashmod[i]);
    }
    friend hash_struct operator+(hash_struct x,const int y){
        for(int i=0;i<3;i++){
            x.hash_value[i]=(x.hash_value[i]+y)%hashmod[i];
        }
        return x;
    }
    friend hash_struct operator-(hash_struct x,const int y){
        for(int i=0;i<3;i++){
            x.hash_value[i]=(x.hash_value[i]-y%hashmod[i]+hashmod[i])%hashmod[i];
        }
        return x;
    }
    friend hash_struct operator*(hash_struct x,const int y){
        for(int i=0;i<3;i++){
            x.hash_value[i]=(1ll*x.hash_value[i]*y)%hashmod[i];
        }
        return x;
    }
    friend hash_struct operator+(const int y,hash_struct x){
        for(int i=0;i<3;i++){
            x.hash_value[i]=(x.hash_value[i]+y)%hashmod[i];
        }
        return x;
    }
    friend hash_struct operator-(const int y,hash_struct x){
        for(int i=0;i<3;i++){
            x.hash_value[i]=(x.hash_value[i]-y%hashmod[i]+hashmod[i])%hashmod[i];
        }
        return x;
    }
    friend hash_struct operator*(const int y,hash_struct x){
        for(int i=0;i<3;i++){
            x.hash_value[i]=(1ll*x.hash_value[i]*y)%hashmod[i];
        }
        return x;
    }
    friend hash_struct operator+(hash_struct x,hash_struct y){
        for(int i=0;i<3;i++){
            x.hash_value[i]=(x.hash_value[i]+y.hash_value[i])%hashmod[i];
        }
        return x;
    }
    friend hash_struct operator-(hash_struct x,hash_struct y){
        for(int i=0;i<3;i++){
            x.hash_value[i]=(x.hash_value[i]-y.hash_value[i]+hashmod[i])%hashmod[i];
        }
        return x;
    }
    friend hash_struct operator*(hash_struct x,hash_struct y){
        for(int i=0;i<3;i++){
            x.hash_value[i]=(1ll*x.hash_value[i]*y.hash_value[i])%hashmod[i];
        }
        return x;
    }
    friend bool operator<(hash_struct x,hash_struct y){
        for(int i=0;i<3;i++){
            if(x.hash_value[i]!=y.hash_value[i]){
                return x.hash_value[i]<y.hash_value[i];
            }
        }
        return false;
    }
    friend bool operator==(hash_struct x,hash_struct y){
        for(int i=0;i<3;i++){
            if(x.hash_value[i]!=y.hash_value[i]){
                return false;
            }
        }
        return true;
    }
    friend bool operator!=(hash_struct x,hash_struct y){
        for(int i=0;i<3;i++){
            if(x.hash_value[i]!=y.hash_value[i]){
                return true;
            }
        }
        return false;
    }
};
hash_struct powb[1000005];
hash_struct mergehash(hash_struct x,hash_struct y,int len){
    return x*powb[len]+y;
}
struct node{
    hash_struct pre,diff;
    node(){
        pre=hash_struct(0,0,0);
        diff=hash_struct(0,0,0);
    }
    node(hash_struct pre,hash_struct diff):pre(pre),diff(diff){}
} trees[4000005];
int ls(int x){
    return x<<1;
}
int rs(int x){
    return x<<1|1;
}
node mergenode(node x,node y,int xlen,int ylen){
    return node(mergehash(x.pre,y.pre,ylen),mergehash(y.diff,x.diff,xlen));
}
void push_up(int x,int l,int r){
    int mid=(l+r)>>1;
    trees[x]=mergenode(trees[ls(x)],trees[rs(x)],mid-l+1,r-mid);
}
void build(int x,int l,int r){
    if(l==r){
        char ch;
        cin>>ch;
        trees[x]=node(hash_struct(ch,ch,ch),hash_struct(ch,ch,ch));
        return;
    }
    int mid=(l+r)>>1;
    build(ls(x),l,mid);
    build(rs(x),mid+1,r);
    push_up(x,l,r);
}
void update(int x,int l,int r,int nl,char k){
    if(l==r){
        trees[x]=node(hash_struct((int)k,(int)k,(int)k),hash_struct((int)k,(int)k,(int)k));
        return;
    }
    int mid=(l+r)>>1;
    if(nl<=mid) update(ls(x),l,mid,nl,k);
    else update(rs(x),mid+1,r,nl,k);
    push_up(x,l,r);
}
node query(int x,int l,int r,int nl,int nr){
    if(nl<=l&&r<=nr){
        return trees[x];
    }
    int mid=(l+r)>>1;
    node res;
    int len=0;
    if(nl<=mid){
        res=query(ls(x),l,mid,nl,nr);
        len=mid-nl+1;
    }
    if(mid<nr){
        res=mergenode(res,query(rs(x),mid+1,r,nl,nr),len,nr-mid);
    }
    return res;
}
int main(){
    cin>>n>>q;
    powb[0]=hash_struct(1,1,1);
    for(int i=1;i<=n;i++){
        powb[i]=powb[i-1]*hashb;
    }
    build(1,1,n);
    for(int i=1;i<=q;i++){
        int op;
        cin>>op;
        if(op==1){
            int x;
            char c;
            cin>>x>>c;
            update(1,1,n,x,c);
        }
        else{
            int l,r;
            cin>>l>>r;
            node tmp=query(1,1,n,l,r);
            if(tmp.pre==tmp.diff){
                cout<<"Yes\n";
            }
            else{
                cout<<"No\n";
            }
        }
    }
    return 0;
}
2024/4/5 20:11
加载中...