ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

E. Kefa and Watch 题解(线段树维护hash+循环节结论)

2021-07-19 10:32:25  阅读:207  来源: 互联网

标签:typedef hash int 题解 Watch long maxn ull define


题目链接

题目大意

对数组又2种操作

1、区间整体赋值

2、询问[l,r]是否有长度为d的循环节

题目思路

其实就是一个结论

如果s[l,r-d]==s[l+d,r],那么就有长度为d的循环节,自己画图演示即可发现

然后线段树维护hash即可

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define fi first
#define se second
#define debug printf("aaaaaaaaaaa\n");
const int maxn=1e5+5,inf=0x3f3f3f3f,mod=1e9+7;
const ll INF=0x3f3f3f3f3f3f3f3f;
int n,m,k;
char s[maxn];
ull tree[maxn<<2],lazy[maxn<<2];
ull pre[maxn],base=233,prepre[maxn];
void updown(int node,int l,int r){
    if(lazy[node]){
        int mid=(l+r)/2;
        lazy[node<<1]=lazy[node];
        lazy[node<<1|1]=lazy[node];
        tree[node<<1]=prepre[(mid-l+1)-1]*lazy[node]%mod;
        tree[node<<1|1]=prepre[(r-mid)-1]*lazy[node]%mod;
        lazy[node]=0;
    }
}

void pushup(int node,int l,int r){
    int mid=(l+r)/2;
    // [mid+1,r]
    tree[node]=(tree[node<<1]*pre[r-mid]+tree[node<<1|1])%mod;
}
void build(int node,int l,int r){
    if(l==r){
        tree[node]=s[l]-'0'+1;
        return ;
    }
    int mid=(l+r)/2;
    build(node<<1,l,mid);
    build(node<<1|1,mid+1,r);
    pushup(node,l,r);
}
void update(int node,int L,int R,int val,int l,int r){
    if(L<=l&&r<=R){
        tree[node]=prepre[(r-l+1)-1]*val;
        lazy[node]=val;
        return ;
    }
    updown(node,l,r);
    int mid=(l+r)/2;
    if(mid>=L) update(node<<1,L,R,val,l,mid);
    if(mid<R) update(node<<1|1,L,R,val,mid+1,r);
    pushup(node,l,r);
}
ull query(int node,int L,int R,int l,int r){
    if(R<L) return 0;
    if(L<=l&&r<=R){
        return tree[node];
    }
    updown(node,l,r);
    int mid=(l+r)/2;
    ull ans=0;
    if(mid>=L) ans+= pre[max(0,min(R , r)- mid)]*query(node<<1,L,R,l,mid);
    if(mid<R) ans+=query(node<<1|1,L,R,mid+1,r);
    return ans%mod;
}
int main(){
    cin>>n>>m>>k;
    cin>>s+1;
    pre[0]=1;
    prepre[0]=1;
    for(int i=1;i<=n;i++){
        pre[i]=pre[i-1]*base%mod;
        prepre[i]=(prepre[i-1]+pre[i])%mod;
    }
    build(1,1,n);
    m+=k;
    for(int i=1,opt,l,r,c;i<=m;i++){
        cin>>opt>>l>>r>>c;
        if(opt==1){
            update(1,l,r,c+1,1,n);
        }else{

            ull temp1=query(1,l,r-c,1,n);
            ull temp2=query(1,l+c,r,1,n);
            if(temp1==temp2){
                cout<<"YES\n";
            }else{
                cout<<"NO\n";
            }
        }
    }
    return 0;
}

标签:typedef,hash,int,题解,Watch,long,maxn,ull,define
来源: https://www.cnblogs.com/hunxuewangzi/p/15029036.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有