题目:http://codeforces.com/contest/580/problem/E
题意:给定长度为n的字符串,然后有修改和查询操作。修改:将区间[l,r]的字符改为c。查询:查询区间[l,r]的子串的周期是否d。
分析:Obviously, the substring from l to r have a d-period, if a substring from l + d to r is equal to substring from l to r - d.首先要知道串s[l.....r]的周期是否为d,如果s[l,r-d]==s[l+d,r],那么s[l.....r]的周期为d。那么用线段树维护区间字符串的hash值就行了。
ps:有卡自然溢出的数据。我是随便找的一个数来模的。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef unsigned long long ULL;
typedef long long LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const ULL MOD = ((1ull<<31)-777);
const int seed = 131;
const int maxn = 1e5+6;
char str[maxn];
ULL s[10][maxn],p[maxn];
void pre()
{
int i,j;
p[0]=1;
for(i=1;i<maxn;i++)
p[i]=p[i-1]*seed%MOD;//
for(i=0;i<=9;i++)
{
s[i][1]=i;
for(j=2;j<=100000;j++)
{
s[i][j]=s[i][j-1]*seed+i;
s[i][j]%=MOD;//
}
}
}
struct node
{
ULL v;
int len;
node()=default;
node(ULL val,int l)
:v(val),len(l){}
};
struct segtree
{
ULL tree[maxn<<2];
int lazy[maxn<<2];
void pushdown(int l,int r,int rt)
{
if(lazy[rt]>=0)
{
int m=(l+r)>>1;
lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
tree[rt<<1]=s[lazy[rt]][m-l+1];
tree[rt<<1|1]=s[lazy[rt]][r-(m+1)+1];
lazy[rt]=-1;
}
}
void pushup(int l,int r,int rt)
{
tree[rt]=tree[rt<<1]*p[(r-l+1)>>1]+tree[rt<<1|1];
tree[rt]%=MOD;//
}
void build(int l,int r,int rt)
{
lazy[rt]=-1;
if(l==r)
{
tree[rt]=str[l]-'0';
return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
pushup(l,r,rt);
}
void update(int L,int R,int v,int l,int r,int rt)
{
if(L<=l && r<=R)
{
tree[rt]=s[v][r-l+1];
lazy[rt]=v;
return ;
}
pushdown(l,r,rt);
int m=(l+r)>>1;
if(L<=m)
update(L,R,v,lson);
if(R>m)
update(L,R,v,rson);
pushup(l,r,rt);
}
node query(int L,int R,int l,int r,int rt)
{
if(L<=l && r<=R)
return node(tree[rt],r-l+1);
pushdown(l,r,rt);
int m=(l+r)>>1;
node p1(-1,-1),p2(-1,-1);
if(L<=m)
p1=query(L,R,lson);
if(R>m)
p2=query(L,R,rson);
pushup(l,r,rt);
if(p1.len==-1)
return p2;
if(p2.len==-1)
return p1;
return node((p1.v*p[p2.len]+p2.v)%MOD,p1.len+p2.len);
}
void debug(int l,int r,int rt)
{
printf("[%d %d] %llu \n",l,r,tree[rt]);
if(l==r)
return ;
int m=(l+r)>>1;
debug(lson);
debug(rson);
}
}T;
int main()
{
pre();
int n,m,k,tp,x,y,v;
scanf("%d%d%d%s",&n,&m,&k,str+1);
T.build(1,n,1);
m+=k;
while(m--)
{
scanf("%d%d%d%d",&tp,&x,&y,&v);
if(tp==1)
T.update(x,y,v,1,n,1);
else
{
if((y-x+1)<=v)
{
puts("YES");
continue ;
}
node temp1=T.query(x,y-v,1,n,1);
node temp2=T.query(x+v,y,1,n,1);
temp1.v==temp2.v?puts("YES"):puts("NO");
}
// printf("------------------------------------------------------\n");
// T.debug(1,n,1);
// printf("------------------------------------------------------\n");
}
return 0;
}