cf580E. Kefa and Watch

链接

点击跳转

题解

线段树维护区间哈希值

判断循环节的方法类似 k m p kmp kmp,只需判断是否 s [ l . . . r − x ] = s [ l + x . . . r ] s[l...r-x]=s[l+x...r] s[l...rx]=s[l+x...r]即可

代码

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 100010
#define mod 998244353ll
#define base 4894651ll
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<ll,ll> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
ll mi[maxn], n, _;
struct EasyMath
{
    ll prime[maxn], phi[maxn], mu[maxn];
    bool mark[maxn];
    ll fastpow(ll a, ll b, ll c)
    {
        ll t(a%c), ans(1ll);
        for(;b;b>>=1,t=t*t%c)if(b&1)ans=ans*t%c;
        return ans;
    }
    void shai(ll N)
    {
        ll i, j;
        for(i=2;i<=N;i++)mark[i]=false;
        *prime=0;
        phi[1]=mu[1]=1;
        for(i=2;i<=N;i++)
        {
            if(!mark[i])prime[++*prime]=i, mu[i]=-1, phi[i]=i-1;
            for(j=1;j<=*prime and i*prime[j]<=N;j++)
            {
                mark[i*prime[j]]=true;
                if(i%prime[j]==0)
                {
                    phi[i*prime[j]]=phi[i]*prime[j];
                    break;
                }
                mu[i*prime[j]]=-mu[i];
                phi[i*prime[j]]=phi[i]*(prime[j]-1);
            }
        }
    }
}em;
struct SegmentTree
{
    ll hash[maxn<<2], set[maxn<<2], L[maxn<<2], R[maxn<<2];
    void maketag_set(ll o, ll v)
    {
        set[o]=v;
        ll len=R[o]-L[o]+1;
        hash[o]=v*(1-mi[len])%mod*_%mod;
    }
    void pushdown(ll o)
    {
        if(L[o]==R[o])return;
        if(~set[o])
        {
            maketag_set(o<<1,set[o]);
            maketag_set(o<<1|1,set[o]);
            set[o]=-1;
        }
    }
    void pushup(ll o)
    {
        hash[o] = ( hash[o<<1] + hash[o<<1|1] * (mi[ R[o<<1]-L[o<<1]+1 ]) ) %mod;
    }
    void build(ll o, ll l, ll r, char* array)
    {
        ll mid(l+r>>1);
        L[o]=l, R[o]=r;
        set[o]=-1;
        if(l==r)
        {
            hash[o]=array[l];
            return;
        }
        build(o<<1,l,mid,array);
        build(o<<1|1,mid+1,r,array);
        pushup(o);
    }
    void segset(ll o, ll l, ll r, ll v)
    {
        ll mid(L[o]+R[o]>>1);
        if(l<=L[o] and r>=R[o]){maketag_set(o,v);return;}
        pushdown(o);
        if(l<=mid)segset(o<<1,l,r,v);
        if(r>mid)segset(o<<1|1,l,r,v);
        pushup(o);
    }
    void qhash(ll o, ll l, ll r, vector<ll> &v)
    {
        ll mid(L[o]+R[o]>>1);
        if(l<=L[o] and r>=R[o]){v.emb(o);return;}
        pushdown(o);
        if(l<=mid)qhash(o<<1,l,r,v);
        if(r>mid)qhash(o<<1|1,l,r,v);
    }
}segtree;
char s[maxn];
ll gethash(ll l, ll r)
{
    if(l>r)return 0;
    vector<ll> v;
    ll ans=0, len=0;
    segtree.qhash(1,l,r,v);
    for(auto o:v)
    {
        (ans+=mi[len]*segtree.hash[o])%=mod;
        len+=segtree.R[o]-segtree.L[o]+1;
    }
    return (ans+mod)%mod;
}
int main()
{
    ios::sync_with_stdio(false);
    ll n, q, type, l, r, x, m, k, i;
    cin>>n>>m>>k>>s+1;
    q=m+k;
    mi[0]=1; rep(i,n+5)mi[i]=(mi[i-1]*base)%mod;
    _ = em.fastpow(1-base,mod-2,mod);
    segtree.build(1,1,n,s);
    while(q--)
    {
        cin>>type>>l>>r>>x;
        if(type==1)
        {
            segtree.segset(1,l,r,0x30+x);
        }
        else
        {
            if(gethash(l+x,r)==gethash(l,r-x))cout<<"YES\n";
            else cout<<"NO\n";
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值