Kefa and Watch
题意:
维护一个长度为n的字符串,两种操作:
1.将 [l,r] 的字符变为 c
2.询问 d 是否为 $S(l,r)$ 的周期
解法:
首先分析如何令 [l,r] 的周期为d,利用循环串的性质得:
只需要保证 $S(l+d,r) = S(l,r-d)$ 即可。
注意$S(l,r)$周期为 d 的定义是对于$l<=i<=r-d$有$S(i+d) = S(i)$从而,只需要维护hash即可。
应用线段树可以$O(nlogn)$
注意hash一般都要用两个模数,不然极易被卡。
好久没写hash,WA了几下。
#include <iostream>
#include <cstdio>
#include <cstring> #define l(x) ch[x][0]
#define r(x) ch[x][1]
#define N 100010
#define LL unsigned long long
#define P 1000000007ULL using namespace std; int totn,n,m,K;
int ch[N<<][];
char S[N];
int siz[N<<],setv[N<<];
LL power[N],power_mod[N];
LL Spower[N],Spower_mod[N];
LL hashv[N<<];
LL hash_mod[N<<]; void update(int x)
{
siz[x]=siz[l(x)]+siz[r(x)];
hashv[x] = hashv[l(x)]*power[siz[r(x)]] + hashv[r(x)];
hash_mod[x] = (hash_mod[l(x)]*power_mod[siz[r(x)]]%P + hash_mod[r(x)])%P;
} void push(int x)
{
if(setv[x]==-) return;
setv[l(x)]=setv[r(x)]=setv[x];
hashv[l(x)]=Spower[siz[l(x)]-] * (LL)setv[x];
hashv[r(x)]=Spower[siz[r(x)]-] * (LL)setv[x];
hash_mod[l(x)]=Spower_mod[siz[l(x)]-] * (LL)setv[x] % P;
hash_mod[r(x)]=Spower_mod[siz[r(x)]-] * (LL)setv[x] % P;
setv[x]=-;
} int build(int l,int r)
{
int x=++totn;
setv[x]=-;
if(l==r)
{
hash_mod[x]=hashv[x]=S[l-]-'';
siz[x]=;
return x;
}
int mid=(l+r)>>;
l(x)=build(l,mid);
r(x)=build(mid+,r);
update(x);
return x;
} LL ask_mod(int x,int l,int r,int ql,int qr,int &sizv)
{
if(ql<=l && r<=qr)
{
sizv=siz[x];
return hash_mod[x];
}
push(x);
int mid=(l+r)>>,tmp_sizv;
if(ql<=mid && mid<qr)
{
sizv=;
LL tmp1=ask_mod(l(x),l,mid,ql,qr,tmp_sizv);
sizv+=tmp_sizv;
LL tmp2=ask_mod(r(x),mid+,r,ql,qr,tmp_sizv);
sizv+=tmp_sizv;
update(x);
return (tmp1*power_mod[tmp_sizv]%P+tmp2)%P;
}
if(ql<=mid)
{
LL ans=ask_mod(l(x),l,mid,ql,qr,sizv);
update(x);
return ans;
}
else
{
LL ans=ask_mod(r(x),mid+,r,ql,qr,sizv);
update(x);
return ans;
}
} LL ask(int x,int l,int r,int ql,int qr,int &sizv)
{
if(ql<=l && r<=qr)
{
sizv=siz[x];
return hashv[x];
}
push(x);
int mid=(l+r)>>,tmp_sizv;
if(ql<=mid && mid<qr)
{
sizv=;
LL tmp1=ask(l(x),l,mid,ql,qr,tmp_sizv);
sizv+=tmp_sizv;
LL tmp2=ask(r(x),mid+,r,ql,qr,tmp_sizv);
sizv+=tmp_sizv;
update(x);
return tmp1*power[tmp_sizv]+tmp2;
}
if(ql<=mid)
{
LL ans=ask(l(x),l,mid,ql,qr,sizv);
update(x);
return ans;
}
else
{
LL ans=ask(r(x),mid+,r,ql,qr,sizv);
update(x);
return ans;
}
} void set(int x,int l,int r,int ql,int qr,int qv)
{
if(ql<=l && r<=qr)
{
setv[x] = qv;
hashv[x] = Spower[siz[x]-]*(LL)setv[x];
hash_mod[x] = (Spower_mod[siz[x]-] * (LL)setv[x]) % P;
return;
}
push(x);
int mid=(l+r)>>;
if(ql<=mid) set(l(x),l,mid,ql,qr,qv);
if(mid<qr) set(r(x),mid+,r,ql,qr,qv);
update(x);
} int main()
{
power[]=power_mod[]=;
for(int i=;i<N;i++)
{
power[i] = power[i-]*10ULL;
power_mod[i] = power_mod[i-]*10ULL%P;
}
Spower[]=Spower_mod[]=;
for(int i=;i<N;i++)
{
Spower[i] = Spower[i-] + power[i];
Spower_mod[i] = (Spower_mod[i-] + power_mod[i])%P;
}
while(~scanf("%d%d%d",&n,&m,&K))
{
scanf("%s",S);
totn=;
build(,n);
int cmd,l,r,x;
for(int i=;i<=m+K;i++)
{
scanf("%d%d%d%d",&cmd,&l,&r,&x);
if(cmd==) set(,,n,l,r,x);
else
{
int d=x;
if(r-l+==d)
{
puts("YES");
continue;
}
if(ask(,,n,l+d,r,x)==ask(,,n,l,r-d,x)
&& ask_mod(,,n,l+d,r,x)==ask_mod(,,n,l,r-d,x))
puts("YES");
else puts("NO");
}
}
}
return ;
}
/*
20 1 2
34075930750342906718
2 1 20 20
1 1 20 6
2 1 20 1
*/
Kefa and Watch的更多相关文章
- 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 ...
- Codeforces Round #321 (Div. 2) B. Kefa and Company 二分
B. Kefa and Company Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/pr ...
- Codeforces Round #321 (Div. 2) A. Kefa and First Steps 水题
A. Kefa and First Steps Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/58 ...
- Codeforces 841A - Generous Kefa
题目链接:http://codeforces.com/problemset/problem/841/A One day Kefa found n baloons. For convenience, w ...
随机推荐
- javascript 高级编程系列 - 函数
一.函数创建 1. 函数声明 (出现在全局作用域,或局部作用域) function add (a, b) { return a + b; } function add(a, b) { return a ...
- caffe2--ubuntu16.04--14.04--install
Install Welcome to Caffe2! Get started with deep learning today by following the step by step guide ...
- Java 语法清单
Java 语法清单 Java 语法清单翻译自 egek92 的 JavaCheatSheet,从属于笔者的 Java 入门与实践系列.时间仓促,笔者只是简单翻译了些标题与内容整理,支持原作者请前往 ...
- STL源代码剖析——基本算法stl_algobase.h
前言 在STL中.算法是常常被使用的,算法在整个STL中起到很关键的数据.本节介绍的是一些基本算法,包括equal.fill.fill_n,iter_swap.lexicographical_comp ...
- thttpd源代码解析 定时器模块
thttpd源代码解析 定时器模块 thttpd是很轻量级的httpserver,可运行文件仅50kB.名称中的第一个t表示tiny, turbo, 或throttling 与lighttpd.mem ...
- erlang取列表中某个值的位置
有个需求,比如在一个列表中,取出一个元素的位置,如果出现重复都取出.例如:List = [2,3,10,324,88,29,12],可以求大于某个值的位置,也可以取某个值的位置. 废话少说,直接上代码 ...
- NGUI研究之3D模型坐标转2D屏幕坐标-血条
刚好今天有朋友问我,比較典型的样例就是游戏里面人物的血条. 原理非常easy就是把3D点换算成2D的点.可是因为NGUI自身是3D所以我们须要先把NGUI下的点转成2D点.然后在把他转成3D的点 ...
- C# 打开指定的目录 记住路径中 / 与 \ 的使用方法
老生常谈的问题了,C#在指定目录时,路径中要使用 \\.直接看实例 using System; namespace OpenFile{ class OpenFile{ static void Main ...
- DirectShow音频采集pcm,实时编码AAC,附源码
定期送福利,今天给大家送上Windows中利用DirectShow采集microphone音频,并将采集到的pcm数据,利用FAAC库编码成AAC,进行本地存储或者网络传输. 直接贴代码,解析看注释: ...
- EasyDarwin自动停止推流
原文转自:http://blog.csdn.net/ss00_2012/article/details/51441753 我们使用EasyDarwin的推流转发来进行媒体直播的时候,有时会有这样一个需 ...