[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 ...
随机推荐
- bzoj千题计划254:bzoj2286: [Sdoi2011]消耗战
http://www.lydsy.com/JudgeOnline/problem.php?id=2286 虚树上树形DP #include<cmath> #include<cstdi ...
- Codeforces 666 B. World Tour
http://codeforces.com/problemset/problem/666/B 题意: 给定一张边权均为1的有向图,求四个不同的点A,B,C,D,使得dis[A][B]+dis[B][C ...
- bzoj千题计划189:bzoj1867: [Noi1999]钉子和小球
http://www.lydsy.com/JudgeOnline/problem.php?id=1867 dp[i][j] 落到(i,j)的方案数 dp[i][j]=0.5*dp[i-1][j] ...
- BZOJ2301:莫比乌斯反演+二维容斥解决GCD范围计数
这个题是刚才刷的第一道反演题的拓展版,加上一个容斥就可以了 #include<cstdio> #include<algorithm> using std::min; ; int ...
- 何凯文每日一句打卡||DAY9
- Brief History of Machine Learning
Brief History of Machine Learning My subjective ML timeline Since the initial standpoint of science, ...
- Python 入门基础11 --函数基础4 迭代器、生成器、枚举类型
今日目录: 1.迭代器 2.可迭代对象 3.迭代器对象 4.for循环迭代器 5.生成器 6.枚举对象 一.迭代器: 循环反馈的容器(集合类型) 每次重复即一次迭代,并且每次迭代的结果都是下一次迭代的 ...
- echo变量失败,提示:ECHO 处于关闭状态
检查变量值,变量值为空就会提示关闭
- Android Studio引用第三方jar包(例如:使用LitePal)
如何使用LitePal的教程网上很多,不过对于新手,这些教程中遗漏了将第三方jar包拷贝到app->libs路径下后,还需要add as library. 下载LitePal 这时操作步骤如下: ...
- nor flash 和 nand flash
NOR Flash是很常见的一种存储芯片,数据掉电不会丢失,支持Execut On Chip,即程序可以直接在FLASH片内执行(这意味着存储在NOR FLash上的程序不需要复制到RAM就可以直接运 ...