题目大意:
维护一个由'0'~'9'构成的字符串,支持以下两种操作:
1.将指定区间内的所有字符修改为同一指定字符。
2.询问$x$是否为指定区间内的循环节。

思路:
建立一棵线段树,维护每个子串的哈希值。

实现细节:
1.Lazy-tag需要初始化成$-1$,因为$0$也是字符串中的元素,会混淆。
2.预处理出seed的$n$次幂及其前缀和,可以优化常数。
3.查询时需要合并两个区间的哈希值,需要考虑$mid<r$和$mid\geq r$两种情况。

其它:
这题调不出的时候打了一个暴力对拍,加过发现暴力能直接AC。

标算代码:

 #include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
char ch;
while(!isdigit(ch=getchar()));
int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
inline int getdigit() {
char ch;
while(!isdigit(ch=getchar()));
return ch^'';
}
const int N=;
int n;
class SegmentTree {
#define mid ((b+e)>>1)
#define _left <<1
#define _right <<1|1
private:
int val[N<<],tag[N<<],pow[N],pre[N];
static const int seed=,mod=;
void push_up(const int p,const int b,const int e) {
val[p]=((long long)val[p _left]*pow[e-mid]%mod+val[p _right])%mod;
}
void push_down(const int p,const int b,const int e) {
if(tag[p]==-) return;
tag[p _left]=tag[p _right]=tag[p];
val[p _left]=(long long)tag[p]*pre[mid-b]%mod;
val[p _right]=(long long)tag[p]*pre[e-mid-]%mod;
tag[p]=-;
}
int query(const int p,const int b,const int e,const int l,const int r) {
if((b==l)&&(e==r)) return val[p];
push_down(p,b,e);
int ret=;
if(l<=mid) ret=(long long)(ret+query(p _left,b,mid,l,std::min(mid,r)))*pow[std::max(r-mid,)]%mod;
if(r>mid) ret=(ret+query(p _right,mid+,e,std::max(mid+,l),r))%mod;
return ret;
}
public:
SegmentTree() {
pre[]=pow[]=;
for(int i=;i<N;i++) {
pow[i]=(long long)pow[i-]*seed%mod;
pre[i]=(pow[i]+pre[i-])%mod;
}
}
void build(const int p,const int b,const int e) {
tag[p]=-;
if(b==e) {
val[p]=getdigit();
return;
}
build(p _left,b,mid);
build(p _right,mid+,e);
push_up(p,b,e);
}
void modify(const int p,const int b,const int e,const int l,const int r,const int x) {
if((b==l)&&(e==r)) {
val[p]=x*pre[e-b];
tag[p]=x;
return;
}
push_down(p,b,e);
if(l<=mid) modify(p _left,b,mid,l,std::min(mid,r),x);
if(r>mid) modify(p _right,mid+,e,std::max(mid+,l),r,x);
push_up(p,b,e);
}
bool check(const int l,const int r,const int x) {
if(x<=||x>=r-l+) return true;
return query(,,n,l,r-x)==query(,,n,l+x,r);
}
};
SegmentTree t;
int main() {
n=getint();
int m=getint()+getint();
t.build(,,n);
while(m--) {
int d=getint(),l=getint(),r=getint(),c=getint();
if(d==) t.modify(,,n,l,r,c);
if(d==) puts(t.check(l,r,c)?"YES":"NO");
}
return ;
}

暴力代码:

 #include<cstdio>
#include<cctype>
inline int getint() {
char ch;
while(!isdigit(ch=getchar()));
int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
inline int getdigit() {
char ch;
while(!isdigit(ch=getchar()));
return ch^'';
}
int main() {
int n=getint(),m=getint()+getint();
int a[n+];
for(int i=;i<=n;i++) a[i]=getdigit();
while(m--) {
int d=getint(),l=getint(),r=getint(),c=getint();
if(d==) {
for(int i=l;i<=r;i++) a[i]=c;
}
if(d==) {
for(int i=;i<=c;i++) {
for(int j=l-+i+c;j<=r;j+=c) {
if(a[j]!=a[j-c]) {
puts("NO");
goto Next;
}
}
}
puts("YES");
}
Next:;
}
return ;
}

数据生成器:

 #include<ctime>
#include<cstdio>
#include<cstdlib>
int main() {
srand(time(NULL));
int n=,m=;
printf("%d %d %d\n",n,m,);
for(int i=;i<=n;i++) {
int d=rand()%;
printf("%d",d);
}
puts("");
for(int i=;i<=m;i++) {
int d=rand()%+,l=rand()%n+,r=rand()%(n-l+)+l;
int c=(d==)?(rand()%):(rand()%(r-l+));
printf("%d %d %d %d\n",d,l,r,c);
}
return ;
}

对拍程序:

 @echo off
:loop
data.exe >input.txt
SimpleOJ206.exe <input.txt >hash.txt
SimpleOJ206scx.exe <input.txt >scx.txt
fc hash.txt scx.txt
if errorlevel 1 pause
goto loop

用来手算哈希的计算器:

 while 1:
import os
print(eval(input()))
os.system("pause")

[CF580E]Kefa and Watch的更多相关文章

  1. cf580E. Kefa and Watch(线段树维护字符串hash)

    题意 $n$个数的序列,$m + k$种操作 1.$l , r, k$把$l - r$赋值为$k$ 2.$l, r, d$询问$l - r$是否有长度为$d$的循环节 Sol 首先有个神仙结论:若询问 ...

  2. 线段树+哈希【CF580E】Kefa and Watch

    线段树+哈希[CF580E]Kefa and Watch Description \(n\)个数的字符串,\(m + k\)个操作 1 l r k把\(l - r\)赋值为\(k\) 2 l r d询 ...

  3. codeforces 580D:Kefa and Dishes

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

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

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

  5. Kefa and Park

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

  6. B - Kefa and Company

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

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. CM记录-配置Hive on Spark

    默认hive on spark是禁用的,需要在Cloudera Manager中启用.1.登录CM界面,打开hive服务.2.单击 配置标签,查找enable hive on spark属性.3.勾选 ...

  2. bzoj千题计划180:bzoj4411: [Usaco2016 Feb]Load balancing

    http://www.lydsy.com/JudgeOnline/problem.php?id=4411 用树状数组维护扫描线 一个树状数组维护扫描线之上的y<=i点,另一个维护扫描线之下y&l ...

  3. 用matplotlib制作的比较满意的蜡烛图

    用matplotlib制作的比较满意的蜡烛图 2D图形制作包, 功能强大, 习练了很久, 终于搞定了一个比较满意的脚本. 特点: 使用方面要非常简单 绘制出来的图要非常的满意, 具有如下的特点 时间和 ...

  4. .NET面试题系列(九)C# 结构体与类的区别

    谈一下什么时候使用结构,什么使用类. 我们知道,结构存储在栈中,而栈有1个特点,就是空间较小,但是访问速度较快,堆空间较大,但是访问速度相对较慢.所以当我们描述1个轻量级对象的时候,可以将其定义为结构 ...

  5. html5 canvas 奇怪的形状垂直渐变

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. java学习第04天(语句、函数、数组)

    (3)循环结构 格式: for(初始化表达式,循环条件表达式,循环后的操作变大时){ 执行语句,循环体: } 注: a. for循环里面的连个表达式运行的顺序,初始化表达式只读一次,判断循环条件,为真 ...

  7. 如何用Procmon.exe来监视SQLSERVER的logwrite大小

    如何用Procmon.exe来监视SQLSERVER的logwrite大小 在微软亚太区数据库技术支持组官方博客里面,你会发现很多篇文章都用到了Procmon.exe这个工具 今天我也介绍一下这个工具 ...

  8. Linux 网络操作

    Linux 基础网路操作  ifconfig eth0 down # 禁用网卡 ifconfig eth0 up # 启用网卡 ifup eth0: # 启用网卡 mii-tool em1 # 查看网 ...

  9. requests下载文件并重新上传

    import re import requests from io import BytesIO from django.core.files.uploadedfile import InMemory ...

  10. Ubuntu14.04搭建Android O编译环境

    一.搭建环境 官方参考文档: 1.代号.标签和版本号 2.Factory Images 3.Driver Binaries 4.工具链  软硬件版本: 1.系统平台:I5-8500T+8G+1T,Ub ...