题目大意:
维护一个由'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. Java迭代器用法

    public class Test01 { public static void main(String[] args) { List list = new ArrayList(); list.add ...

  2. Mac下安装zsh(Oh My ZSH)的shell,替代原有的bash

    说明:一开始装zsh我是拒绝的,因为这个东西装简单,卸载很难,并且装了之后默认Shell的配置文件不能用了,比如~/.bashrc这些.所以在装的时候要再三考虑好! 官网:http://ohmyz.s ...

  3. ASP.NET MVC学习(五)之MVC原理解析

    ASP.NET MVC 请求生命周期 生命周期步骤概览 当我们对ASP.NET MVC网站发出一个请求的时候,会发生5个主要步骤: 步骤1:创建RouteTable 当ASP.NET应用程序第一次启动 ...

  4. Docker学习笔记一 概念、安装、镜像加速

    本文地址:https://www.cnblogs.com/veinyin/p/10406378.html  Docker 是一个容器,可以想象成一个轻便的虚拟机,但不虚拟硬件和操作系统. 优点:启动快 ...

  5. 【转】VTL-vm模板的变量用法

    http://www.cnblogs.com/zengxlf/archive/2009/05/06/1451004.html 加载foot模块页 #parse("foot.vm") ...

  6. 在vue-cli下读取模拟数据请求服务器

    写此记录时vue脚手架的webpack是3.6.0 此文章方法亦可用于vue-cli3,直接在vue.config.js里面添加 本记录使用vue-resource,先安装: cnpm install ...

  7. python垃圾回收之分代回收

    可参考vamei的博客和https://www.jianshu.com/p/1e375fb40506

  8. gbk文件转为utf8文件

    convmv -f gbk -t utf- --notest -r ./

  9. mac安装ocr

    mac安装Tesserocr 安装 Imagemagick 和 Tesseract 库: brew install imagemagick brew install tesseract --all-l ...

  10. Linux umount的device is busy问题

    现象: [root@dbserver ~]# df -h文件系统 容量 已用 可用 已用%% 挂载点/dev/vda1 9.9G 3.9G 5.6G 41% /tmpfs 3.9G 100K 3.9G ...