题意:

维护一个长度为n的字符串,两种操作:

1.将 [l,r] 的字符变为 c

2.询问 d 是否为 $S(l,r)$ 的周期

解法:

首先分析如何令 [l,r] 的周期为d,利用循环串的性质得:

只需要保证 $S(l+d,r) = S(l,r-d)$ 即可。

注意$S(l,r)$周期为 d 的定义是对于$l<=i<=r-d$有$S(i+d) = S(i)$从而,只需要维护hash即可。

应用线段树可以$O(nlogn)$

注意hash一般都要用两个模数,不然极易被卡。

好久没写hash,WA了几下。

 #include <iostream>
#include <cstdio>
#include <cstring> #define l(x) ch[x][0]
#define r(x) ch[x][1]
#define N 100010
#define LL unsigned long long
#define P 1000000007ULL using namespace std; int totn,n,m,K;
int ch[N<<][];
char S[N];
int siz[N<<],setv[N<<];
LL power[N],power_mod[N];
LL Spower[N],Spower_mod[N];
LL hashv[N<<];
LL hash_mod[N<<]; void update(int x)
{
siz[x]=siz[l(x)]+siz[r(x)];
hashv[x] = hashv[l(x)]*power[siz[r(x)]] + hashv[r(x)];
hash_mod[x] = (hash_mod[l(x)]*power_mod[siz[r(x)]]%P + hash_mod[r(x)])%P;
} void push(int x)
{
if(setv[x]==-) return;
setv[l(x)]=setv[r(x)]=setv[x];
hashv[l(x)]=Spower[siz[l(x)]-] * (LL)setv[x];
hashv[r(x)]=Spower[siz[r(x)]-] * (LL)setv[x];
hash_mod[l(x)]=Spower_mod[siz[l(x)]-] * (LL)setv[x] % P;
hash_mod[r(x)]=Spower_mod[siz[r(x)]-] * (LL)setv[x] % P;
setv[x]=-;
} int build(int l,int r)
{
int x=++totn;
setv[x]=-;
if(l==r)
{
hash_mod[x]=hashv[x]=S[l-]-'';
siz[x]=;
return x;
}
int mid=(l+r)>>;
l(x)=build(l,mid);
r(x)=build(mid+,r);
update(x);
return x;
} LL ask_mod(int x,int l,int r,int ql,int qr,int &sizv)
{
if(ql<=l && r<=qr)
{
sizv=siz[x];
return hash_mod[x];
}
push(x);
int mid=(l+r)>>,tmp_sizv;
if(ql<=mid && mid<qr)
{
sizv=;
LL tmp1=ask_mod(l(x),l,mid,ql,qr,tmp_sizv);
sizv+=tmp_sizv;
LL tmp2=ask_mod(r(x),mid+,r,ql,qr,tmp_sizv);
sizv+=tmp_sizv;
update(x);
return (tmp1*power_mod[tmp_sizv]%P+tmp2)%P;
}
if(ql<=mid)
{
LL ans=ask_mod(l(x),l,mid,ql,qr,sizv);
update(x);
return ans;
}
else
{
LL ans=ask_mod(r(x),mid+,r,ql,qr,sizv);
update(x);
return ans;
}
} LL ask(int x,int l,int r,int ql,int qr,int &sizv)
{
if(ql<=l && r<=qr)
{
sizv=siz[x];
return hashv[x];
}
push(x);
int mid=(l+r)>>,tmp_sizv;
if(ql<=mid && mid<qr)
{
sizv=;
LL tmp1=ask(l(x),l,mid,ql,qr,tmp_sizv);
sizv+=tmp_sizv;
LL tmp2=ask(r(x),mid+,r,ql,qr,tmp_sizv);
sizv+=tmp_sizv;
update(x);
return tmp1*power[tmp_sizv]+tmp2;
}
if(ql<=mid)
{
LL ans=ask(l(x),l,mid,ql,qr,sizv);
update(x);
return ans;
}
else
{
LL ans=ask(r(x),mid+,r,ql,qr,sizv);
update(x);
return ans;
}
} void set(int x,int l,int r,int ql,int qr,int qv)
{
if(ql<=l && r<=qr)
{
setv[x] = qv;
hashv[x] = Spower[siz[x]-]*(LL)setv[x];
hash_mod[x] = (Spower_mod[siz[x]-] * (LL)setv[x]) % P;
return;
}
push(x);
int mid=(l+r)>>;
if(ql<=mid) set(l(x),l,mid,ql,qr,qv);
if(mid<qr) set(r(x),mid+,r,ql,qr,qv);
update(x);
} int main()
{
power[]=power_mod[]=;
for(int i=;i<N;i++)
{
power[i] = power[i-]*10ULL;
power_mod[i] = power_mod[i-]*10ULL%P;
}
Spower[]=Spower_mod[]=;
for(int i=;i<N;i++)
{
Spower[i] = Spower[i-] + power[i];
Spower_mod[i] = (Spower_mod[i-] + power_mod[i])%P;
}
while(~scanf("%d%d%d",&n,&m,&K))
{
scanf("%s",S);
totn=;
build(,n);
int cmd,l,r,x;
for(int i=;i<=m+K;i++)
{
scanf("%d%d%d%d",&cmd,&l,&r,&x);
if(cmd==) set(,,n,l,r,x);
else
{
int d=x;
if(r-l+==d)
{
puts("YES");
continue;
}
if(ask(,,n,l+d,r,x)==ask(,,n,l,r-d,x)
&& ask_mod(,,n,l+d,r,x)==ask_mod(,,n,l,r-d,x))
puts("YES");
else puts("NO");
}
}
}
return ;
}
/*
20 1 2
34075930750342906718
2 1 20 20
1 1 20 6
2 1 20 1
*/

Kefa and Watch的更多相关文章

  1. codeforces 580D:Kefa and Dishes

    Description When Kefa came to the restaurant and sat at a table, the waiter immediately brought him ...

  2. CF 321B Kefa and Company(贪心)

    题目链接: 传送门 Kefa and Company time limit per test:2 second     memory limit per test:256 megabytes Desc ...

  3. Kefa and Park

    #include<bits/stdc++.h> #define max 100005 using namespace std; int cats[max]; vector<int&g ...

  4. B - Kefa and Company

    B - Kefa and Company Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I6 ...

  5. Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压dp

    题目链接: 题目 D. Kefa and Dishes time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 W ...

  6. Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash

    E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...

  7. Codeforces Round #321 (Div. 2) C. Kefa and Park dfs

    C. Kefa and Park Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/probl ...

  8. Codeforces Round #321 (Div. 2) B. Kefa and Company 二分

    B. Kefa and Company Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/pr ...

  9. Codeforces Round #321 (Div. 2) A. Kefa and First Steps 水题

    A. Kefa and First Steps Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/58 ...

  10. Codeforces 841A - Generous Kefa

    题目链接:http://codeforces.com/problemset/problem/841/A One day Kefa found n baloons. For convenience, w ...

随机推荐

  1. C语言判断回文数

    #include<stdio.h> #include<stdlib.h> int main() { //1.得到这个数字 2.翻转 3.进行比较 4.如果相同 就输出 是 否则 ...

  2. HDU 4923 Room and Moor(推理+栈维护)

    HDU 4924 Room and Moor 题目链接 题意:给定一个01组成的a序列.要求一个b序列,b序列每一个数值为[0, 1]之间的数,而且b序列为非递减序列,要求∑(ai−bi)2最小,求这 ...

  3. kubernetes之PDB

    系列目录 上一节我们讲到了由于一些人为的或者不可避免的原因,pod可能会中断,而使用Pod Disruption Budget可以最大限度地保证在pod中断发生时集群仍然保持能够接受的状态. 一句话, ...

  4. java 回文字符串

    package string.string1_5; public class Palindrome { /** * 从两头向中间移动 * @param str * @return */ public ...

  5. poj 3233 Matrix Power Series(矩阵二分,高速幂)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 15739   Accepted:  ...

  6. Linux dnsmasq 服务

    在日常开发中,有这么一个需求: 大家在公司内网同一个网段下,一般情况上网会由网关(一般是路由器)的DHCP服务分配IP.公司内网里放了几台服务器,分别配置成静态IP,这些IP是DHCP配置时预留的.服 ...

  7. 安卓版本6.0打开uiautomator报错

    可能是appium打开了,被占用:或者是找不到手机

  8. python的id()函数的一个小方面(转载)

    >>> a = 2 >>> b = 2 >>> id(a) 21132060 >>> id(b) 21132060 >&g ...

  9. 二维码、条形码扫描——使用Google ZXing

    我在项目中用到了二维码扫描的技术,用的是Google提供的ZXing开源项目,它提供二维码和条形码的扫描.扫描条形码就是直接读取条形码的内容,扫描二维码是按照自己指定的二维码格式进行编码和解码. 可以 ...

  10. kbmmw 5.09 发布

    New stuff        =========        - Added kbmMWSmartBind.pas unit with optional kbmMWSmartBindVCL.pa ...