[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 ...
随机推荐
- RESTful记录-RESTful服务
按照REST架构,一个RESTful Web服务不应该继续服务器的客户端的状态.这种限制被称为无状态.它负责客户以它的上下文传递给服务器,然后服务器可以存储这样的上下文,以处理客户端的进一步请求.例如 ...
- bzoj千题计划209:bzoj1185: [HNOI2007]最小矩形覆盖
http://www.lydsy.com/JudgeOnline/problem.php?id=1185 题解去看它 http://www.cnblogs.com/TheRoadToTheGold/p ...
- 单字节编码&双字节编码
单字节编码(WINDOWS-1252.ISO-8859-1.UTF-8) 双字节编码(UTF-16) Windows 记事本默认会将文件保存为单字节的 ANSI(ASCII).如果您选择 " ...
- 转自知乎大神----JS 的 new 到底是干什么的?
大部分讲 new 的文章会从面向对象的思路讲起,但是我始终认为,在解释一个事物的时候,不应该引入另一个更复杂的事物. 今天我从「省代码」的角度来讲 new. --------------------- ...
- (64位)本体学习程序(ontoEnrich)系统使用说明文档
系统运行:文件夹system下,可执行文件ontoEnrichment 概念学习 --------------------------------------------------------1.简 ...
- iOS问题#解决方案#之关于“application/x-www-form-urlencoded;charset=utf-8” not supported
http://www.cnblogs.com/ChenYilong http://www.cnblogs.com/ChenYilong 如果你用的是AFN/ASI,那得修改源代码了,因为AFN ...
- javascritpt创建对象
javascript添加对象示例: <script> person=new Object(); person.firstname="Bill"; person.last ...
- 【密码学】RSA算法过程-求解密钥
1.密钥的计算获取过程 密钥的计算过程为:首先选择两个质数p和q,令n=p*q. 令k=ϕ(n)=(p−1)(q−1),原理见2的分析 选择任意整数d,保证其与k互质 取整数e,使得[de]k=[1] ...
- phantomhs获取网页的高度
function heheda() { window.setTimeout(function () { console.log("---------------------Capture O ...
- Shiro:授权控制
对容易忽略的地方记录如下: 1.需要引入下面2个依赖,具体版本根据自身环境修改: <dependency> <groupId>org.apache.geronimo.bundl ...