E. Kefa and Watch
time limit per test
1 second
memory limit per test

256 megabytes

input

standard input

output

standard output

One day Kefa the parrot was walking down the street as he was on the way home from the restaurant when he saw something glittering by the road. As he came nearer he understood that it was a watch. He decided to take it to the pawnbroker to earn some money.

The pawnbroker said that each watch contains a serial number represented by a string of digits from 0 to 9, and the more quality checks this number passes, the higher is the value of the watch. The check is defined by three positive integers lr and d. The watches pass a check if a substring of the serial number from l to r has period d. Sometimes the pawnbroker gets distracted and Kefa changes in some substring of the serial number all digits to c in order to increase profit from the watch.

The seller has a lot of things to do to begin with and with Kefa messing about, he gave you a task: to write a program that determines the value of the watch.

Let us remind you that number x is called a period of string s (1 ≤ x ≤ |s|), if si  =  si + x for all i from 1 to |s|  -  x.

Input

The first line of the input contains three positive integers nm and k (1 ≤ n ≤ 105, 1 ≤ m + k ≤ 105) — the length of the serial number, the number of change made by Kefa and the number of quality checks.

The second line contains a serial number consisting of n digits.

Then m + k lines follow, containing either checks or changes.

The changes are given as 1 l r с (1 ≤ l ≤ r ≤ n, 0 ≤ c ≤ 9). That means that Kefa changed all the digits from the l-th to the r-th to be c.

The checks are given as 2 l r d (1 ≤ l ≤ r ≤ n, 1 ≤ d ≤ r - l + 1).

Output

For each check on a single line print "YES" if the watch passed it, otherwise print "NO".

Sample test(s)
input
3 1 2
112
2 2 3 1
1 1 3 8
2 1 2 1
output
NO
YES
input
6 2 3
334934
2 2 5 2
1 4 4 3
2 1 6 3
1 2 3 8
2 3 6 1
output
NO
YES
NO
Note

In the first sample test two checks will be made. In the first one substring "12" is checked on whether or not it has period 1, so the answer is "NO". In the second one substring "88", is checked on whether or not it has period 1, and it has this period, so the answer is "YES".

In the second statement test three checks will be made. The first check processes substring "3493", which doesn't have period 2. Before the second check the string looks as "334334", so the answer to it is "YES". And finally, the third check processes substring "8334", which does not have period 1.

这里的hash函数定义H[i]=s[n-1]*x^(n-1-i)+s[n-2]*x^(n-2-i)+...+s[i],那么对于一段长度为L的子串s[i]~s[i+L-1],它的hash值就可以表示为

H[i]-H[i+L]这个值只和它本身有关。

现在是在线段树上维护的话,按照定义有H[root] = H[lch]+H[rch]*x^(len(lch))。

查询一个子串的hash值类似处理。

题目的要求有一个全部设置为c,这个操作只需要预处理出各种长度下的1+x+x^2+..+x^len值。

第一次写,各种写挂。没模素数,单hash挂在75组数据,双hash换了几组key才过。。。事实证明还是模素数稳。

这题还可以memset memcmp 可以水过去。。。这种常数很小的复杂度算出来除个10和一般的差不多。

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull; const int Key[] = {,};//{799817,451309};//{ 1000003,999997 }; const int maxn = 1e5+;
char s[maxn];//定义成了char 型的slen 导致RE
int slen; struct Seg
{
int len;
ull Hash[];
int Set;
}tr[maxn<<]; ull sHash[maxn][];//set related
ull pKey[maxn][]; inline void push_down(Seg&u,Seg&c1,Seg&c2)
{
if(~u.Set){
c1.Set = c2.Set = u.Set;
c1.Hash[] = c1.Set*sHash[c1.len][];
c2.Hash[] = c2.Set*sHash[c2.len][];
c1.Hash[] = c1.Set*sHash[c1.len][];
c2.Hash[] = c2.Set*sHash[c2.len][];
u.Set = -;
}
} inline void push_up(Seg&u,Seg&c1,Seg&c2)
{
u.Hash[] = c1.Hash[] + c2.Hash[]*pKey[c1.len][];
u.Hash[] = c1.Hash[] + c2.Hash[]*pKey[c1.len][];
} #define lid (id<<1)
#define rid (id<<1|1)
void build(int id = ,int l = ,int r = slen)
{
tr[id].Set = -;
if(l == r) {
tr[id].len = ; tr[id].Hash[] = tr[id].Hash[] = s[l]-'';
}else {
int M = (l+r)>>,lc = lid, rc = rid;
build(lc,l,M);
build(rc,M+,r);
tr[id].len = tr[lc].len+tr[rc].len;
push_up(tr[id],tr[lc],tr[rc]);
}
} int ql,qr,val;
void updata(int id = ,int l = , int r = slen)
{
if(ql<=l&&r<=qr) {
tr[id].Set = val;//设置标记的同时就应该把值修改好
tr[id].Hash[] = val*sHash[tr[id].len][];
tr[id].Hash[] = val*sHash[tr[id].len][];
}else {
int M = (l+r)>>, lc = lid, rc = rid;
push_down(tr[id],tr[lc],tr[rc]);
if(ql<=M) updata(lc,l,M);
if(qr>M) updata(rc,M+,r);
push_up(tr[id],tr[lc],tr[rc]);
}
} #define PB push_back
#define MP make_pair
#define fi first
#define se second pair<ull,ull> query(int id = ,int l = , int r = slen)
{
if(ql<=l&&r<=qr) {
return pair<ull,ull>(tr[id].Hash[],tr[id].Hash[]);
}else {
int M = (l+r)>>, lc = lid, rc = rid;
push_down(tr[id],tr[lc],tr[rc]);
if(qr<=M) return query(lc,l,M);
if(ql>M) return query(rc,M+,r);
auto lq = query(lc,l,M), rq = query(rc,M+,r);
int llen = M-max(ql,l)+;//取决于左边,要取max。。。
return pair<ull,ull>(lq.fi+rq.fi*pKey[llen][],lq.se+rq.se*pKey[llen][]);
}
} void init()
{
pKey[][] = pKey[][] = 1ull;
for(int i = ; i < slen; i++){
pKey[i][] = pKey[i-][]*Key[];
pKey[i][] = pKey[i-][]*Key[];
}
sHash[][] = sHash[][] = 1ull;
for(int i = ; i <= slen; i++){
sHash[i][] = sHash[i-][]+pKey[i-][];
sHash[i][] = sHash[i-][]+pKey[i-][];
}
build();
} int main()
{
//freopen("in.txt","r",stdin);
int m,k; scanf("%d%d%d",&slen,&m,&k);
if(!k) return ;
scanf("%s",s+);
init();
k += m;
while(k--){
int op,l,r,x; scanf("%d%d%d%d",&op,&l,&r,&x);
if(op == ){
ql = l; qr = r; val = x;
updata();
}else {
int dlen = r-l-x;
if(dlen<) { puts("YES"); continue; }//长度为0
ql = l; qr = l+dlen;
auto h1 = query();
ql = l+x; qr = l+x+dlen;
puts(h1 == query()?"YES":"NO");
}
}
return ;
}

Codeforces Round #321 (Div. 2) E Kefa and Watch (线段树维护Hash)的更多相关文章

  1. 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 ...

  2. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  3. Codeforces Round #373 (Div. 2) E. Sasha and Array 线段树维护矩阵

    E. Sasha and Array 题目连接: http://codeforces.com/contest/719/problem/E Description Sasha has an array ...

  4. Codeforces Round #312 (Div. 2) E. A Simple Task 线段树

    E. A Simple Task 题目连接: http://www.codeforces.com/contest/558/problem/E Description This task is very ...

  5. Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树, 位运算)

    链接: https://codeforces.com/contest/1234/problem/D 题意: You are given a string s consisting of lowerca ...

  6. Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树

    C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...

  7. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

  8. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线

    D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...

  9. Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)

    题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...

随机推荐

  1. js中match的用法

    match() 方法将检索字符串 stringObject,以找到一个或多个与 regexp 匹配的文本.这个方法的行为在很大程度上有赖于 regexp 是否具有标志 g. 一.如果 regexp 没 ...

  2. C# 中的迭代器 yield关键字 提高性能和可读性

    展示一个例子 IList<string> FindBobs(IEnumerable<string> names) { var bobs = new List<string ...

  3. nginx是如何处理一个请求的(包含https配置)

    配置https首先要有ssl证书,这个证书目前阿里有免费的,但如果自己做实验,也是可以自签证书,只不过不受信 openssl genrsa -des3 -out server.key 1024     ...

  4. Golang : pflag 包简介

    笔者在前文中介绍了 Golang 标准库中 flag 包的用法,事实上有一个第三方的命令行参数解析包 pflag 比 flag 包使用的更为广泛.pflag 包的设计目的就是替代标准库中的 flag ...

  5. 洛谷 - P1631 - 序列合并 - 堆

    https://www.luogu.org/problemnew/show/P1631 序列a中每个数首先都和序列b中的最小元素配对(虽然好像不是很必要这么早插进来?) 每次从堆顶取出最小的和输出答案 ...

  6. JSP 标准标签库(JSTL)(菜鸟教程)

    菜鸟教程 JSTL 1.1 与 JSTL 1.2 之间的区别?如何下载 JSTL 1.2? JSTL 1.2 中不要求 standard.jar 包. 您可以在 Maven 中央仓库中找到它们. ht ...

  7. [Xcode 实际操作]六、媒体与动画-(4)使用CoreImage框架更改图片的色相

    目录:[Swift]Xcode实际操作 本文将演示如何使用CoreImage框架,调整图片的色相. 通过调整图像的色相,使图像产生暖色效果. 在项目导航区,打开视图控制器的代码文件[ViewContr ...

  8. Sublime Text 3 最新注册码激活码 和 Sublime Text 2 注册码

    Sublime是一款很好用的很轻巧的编辑器,堪称一代神级编辑器.此篇文章用于简单学习记录下神器的激活码,不作其他用途.如有侵权,请联系删除,谢谢~~   1.官方下载地址: http://www.su ...

  9. IT兄弟连 JavaWeb教程 使用AJAX发送POST请求并获取响应

    POST请求用于向服务器发送应该被保存的数据,因此POST请求天然比GET请求多需要一份需要被保存的数据.那么这些数据应该放在何处呢?毕竟,我们的open()方法接收的三个参数都没有合适的位置. 答案 ...

  10. Python基础:模块化来搭项目

    简单模块化 import 最好在最顶端 sys.path.append("..")表示把当前程序所在位置向上提了一级 在python3规范中,__init__.py并不是必须的. ...