[CF580E]Kefa and Watch
题目大意:
维护一个由'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的更多相关文章
- cf580E. Kefa and Watch(线段树维护字符串hash)
题意 $n$个数的序列,$m + k$种操作 1.$l , r, k$把$l - r$赋值为$k$ 2.$l, r, d$询问$l - r$是否有长度为$d$的循环节 Sol 首先有个神仙结论:若询问 ...
- 线段树+哈希【CF580E】Kefa and Watch
线段树+哈希[CF580E]Kefa and Watch Description \(n\)个数的字符串,\(m + k\)个操作 1 l r k把\(l - r\)赋值为\(k\) 2 l r d询 ...
- 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 ...
随机推荐
- linux服务器安装mysql并配置外网访问
linux服务器安装mysql并配置外网访问 更新系统,如果不运行该命令,直接安装mysql,会出现"有几个软件包无法下载 sudo apt-get update 安装mysql sudo ...
- AttributeError: 'module' object has no attribute 'X509_up_ref'
主要报错: AttributeError: 'module' object has no attribute 'X509_up_ref' 1 解决办法 卸载再重装pyOpenSSL pip unins ...
- 转 -----那些年总也记不牢的IO
关于资源关闭: 一般情况下是:先打开的后关闭,后打开的先关闭 另一种情况:看依赖关系,如果流a依赖流b,应该先关闭流a,再关闭流b 例如处理流a依赖节点流b,应该先关闭处理流a,再关闭节点流b 当然完 ...
- php拾遗: 类型约束
突然间什么都不想干,感觉就像来大姨夫一样..但是又不能断了每个工作日都写博客的习惯..所以今天水一下吧. PHP用了快2年了,但是这东西竟然第一次看到,突然间,觉得自己有掉回战五渣的行列了.翻开官方文 ...
- 菜鸟学习Spring Web MVC之一
---恢复内容开始--- 当当当!!沉寂两日,学习Spring Web MVC去了.吐槽:近日跟同行探讨了下,前端攻城师,左肩担着设计师绘图,右肩担着JAVA代码?!我虽设计过UI,但这只算是PS技巧 ...
- 用Canvas做动画
之前看过不少HTML5动画的书,讲解的是如何去做,对于其中的数学原理讲解的不详细,常有困惑.最近看的<HTML5+JavaScript 动画基础>这个是译本,Keith Peters曾写过 ...
- docker之容器访问和网络连接(三)
前言 当一台服务器上部署了多个应用容器,它们直接可能需要相互通信,比如web应用容器需要访问mysql数据库容器. 主机访问容器 通过映射端口的形式我们可以在外部访问容器内的服务 # 将主机的127. ...
- MySQL Dual-Master 双向同步
本文介绍的Mysql Dual-Master 复制实施方法可能不是最完美.最强大的.但是在我的应用环境下能很好的满足各项需求. 本文基于我们仅仅使用两台MySQL服务器的情况下,但是你会发现文章中介绍 ...
- springcloud Eureka自我保护机制
自我保护背景 首先对Eureka注册中心需要了解的是Eureka各个节点都是平等的,没有ZK中角色的概念, 即使N-1个节点挂掉也不会影响其他节点的正常运行. 默认情况下,如果Eureka Serve ...
- Java类的继承与多态特性-入门笔记
相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...