Kefa and Watch
题意:
维护一个长度为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的更多相关文章
- codeforces 580D:Kefa and Dishes
Description When Kefa came to the restaurant and sat at a table, the waiter immediately brought him ...
- CF 321B Kefa and Company(贪心)
题目链接: 传送门 Kefa and Company time limit per test:2 second memory limit per test:256 megabytes Desc ...
- Kefa and Park
#include<bits/stdc++.h> #define max 100005 using namespace std; int cats[max]; vector<int&g ...
- B - Kefa and Company
B - Kefa and Company Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces 841A - Generous Kefa
题目链接:http://codeforces.com/problemset/problem/841/A One day Kefa found n baloons. For convenience, w ...
随机推荐
- sonar+Jenkins代码覆盖率检测
最近公司在搞代码覆盖率检查,简单看了一下结合Jenkins +jacoco + sonar做了一下主要涉及到项目层面和Jenkins层面的东西: 这里只讲一下集成,不讲解sonar的安装Jenkins ...
- C中參数个数可变的函数
一.什么是可变參数 我们在C语言编程中有时会遇到一些參数个数可变的函数,比如printf()函数,其函数原型为: int printf( const char* format, ...); 它除了有一 ...
- HDU 2102 A计划 (BFS)
A计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- idea设置提示不区分大小写
- Oracle中,将毫秒数转换为timestamp类型的两种方法
在许多场景中,开发人员习惯用1970-01-01 00:00:00.000以来的毫秒数来表示具体的时间,这样可以将数据以NUMBER类型存储到数据库中,在某些时候方便比较,同样,有些时候我们需要 把这 ...
- MVC入门——删除页
添加Action DeleteUserInfo using System; using System.Collections.Generic; using System.Linq; using Sys ...
- ios上视频与音乐合成后出现播放兼容问题的解决方法
近期EasyDarwin开源流媒体团队EasyVideoRecorder小组同学Carl在支持一款短视频应用上线时,遇到一个问题:我们在IOS上合成"图片+音乐"成为视频之后,在P ...
- wcf服务发布时,目录中没有文件生成
1.删除原有的配置文件
- Carriage-Return Line-Feed
Git 提交时报错warning: LF will be replaced by CRLF in - CSDN博客 https://blog.csdn.net/yan_less/article/det ...
- 在Visual Studio 2015中引用DLL的3种方法
1.把dll文件复制到可执行文件所在目录 2.将工程属性->配置属性->调试->工作目录更改为dll文件所在目录 3.将工程属性->配置属性->调试->环境设置为P ...