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/problem/E
Description
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 l, r 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 n, m 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 Input
3 1 2
112
2 2 3 1
1 1 3 8
2 1 2 1
Sample Output
NO
YES
HINT
题意
给你一个字符集只有10的串 ,然后又两个操作
1.区间更新
2.查询lr区间的字符的周期是否为d
题解:
直接暴力线段树hash就好了
查询操作只有判断(l,l+len-d)和(l+len-d+1,r)是否一样就好了
可以用类似错位来解释
代码来自peterpan
@)1%KBO0HM418$J94$1R.jpg)
代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<cstring>
using namespace std;
typedef long long ll;
#define input freopen("/Users/peteryuanpan/data.txt","r",stdin) #define N 100010
int n, m, k, lens;
char s[N]; #define lch id<<1
#define rch id<<1|1
class HashTree{
public:
int mod, p;
ll powp[N], sump[N];
void getpowp(){
powp[] = ;
sump[] = ;
for(int i = ; i < N; i++){
powp[i] = powp[i-] * p;
if(powp[i] >= mod) powp[i] %= mod; sump[i] = sump[i-] + powp[i];
if(sump[i] >= mod) sump[i] %= mod;
}
}
ll v[N << ];
int len[N << ];
char lazy[N << ];
void plant(int id,int l,int r){
lazy[id] = '\0';
if(l == r){
v[id] = s[l];
len[id] = ;
return;
}
int mid = (l + r) >> ;
plant(lch, l, mid);
plant(rch, mid + , r);
len[id] = len[lch] + len[rch];
v[id] = v[lch] * powp[len[rch]] + v[rch];
if(v[id] >= mod) v[id] %= mod;
}
void pushdown(int id){
if(lazy[id] != '\0'){
lazy[lch] = lazy[rch] = lazy[id]; v[lch] = lazy[id] * sump[len[lch] - ];
if(v[lch] >= mod) v[lch] %= mod; v[rch] = lazy[id] * sump[len[rch] - ];
if(v[rch] >= mod) v[rch] %= mod; lazy[id] = '\0';
}
}
void update(int id,int ql,int qr,int l,int r,char c){
if(ql == l && qr == r){
lazy[id] = c;
v[id] = c * sump[len[id] - ];
if(v[id] >= mod) v[id] %= mod;
return;
}
pushdown(id);
int mid = (l + r) >> ;
if(qr <= mid) update(lch, ql, qr, l, mid, c);
else if(mid < ql) update(rch, ql, qr, mid + , r, c);
else update(lch, ql, mid, l, mid, c), update(rch, mid + , qr, mid + , r, c); v[id] = v[lch] * powp[len[rch]] + v[rch];
if(v[id] >= mod) v[id] %= mod;
}
ll query(int id,int ql,int qr,int l,int r){
if(ql == l && qr == r){
return v[id];
}
pushdown(id);
int mid = (l + r) >> ;
if(qr <= mid) return query(lch, ql, qr, l, mid);
else if(mid < ql) return query(rch, ql, qr, mid + , r);
else{
ll t1 = query(lch, ql, mid, l, mid);
ll t2 = query(rch, mid + , qr, mid + , r);
ll t = t1 * powp[qr - (mid + ) + ] + t2;
if(t >= mod) t %= mod;
return t;
}
}
}tree1, tree2; bool equal(int l1, int r1, int l2, int r2){
if(tree1.query(, l1, r1, , lens - ) != tree1.query(, l2, r2, , lens - )) return false;
if(tree2.query(, l1, r1, , lens - ) != tree2.query(, l2, r2, , lens - )) return false;
return true;
} bool judge(int l, int r, int d){
if(r - l + == d) return true;
int l2 = l + d, r2 = r;
int len = r2 - l2 + ;
int l1 = l, r1 = l1 + len - ;
return equal(l1, r1, l2, r2);
} int main(){
//input;
tree1.mod = 1e9 + , tree1.p = , tree1.getpowp();
tree2.mod = 1e9 + , tree2.p = , tree2.getpowp();
scanf("%d%d%d",&n,&m,&k); scanf("%s",s);
lens = (int)strlen(s);
tree1.plant(, , lens - ), tree2.plant(, , lens - ); for(int i = ; i <= m + k; i++){
int ty, l, r;
scanf("%d%d%d",&ty,&l,&r);
l--, r--;
if(ty == ){
char c[];
scanf("%s",c);
tree1.update(, l, r, , lens - , c[]);
tree2.update(, l, r, , lens - , c[]);
}
else{
int d;
scanf("%d",&d);
printf("%s\n", judge(l, r, d) ? "YES" : "NO");
}
} return ;
}
Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash的更多相关文章
- 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. ...
- 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 ...
- 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 ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)
题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所 ...
- 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 ...
- Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并
题目链接:http://codeforces.com/contest/381/problem/E E. Sereja and Brackets time limit per test 1 secon ...
随机推荐
- Tomcat详解
解压缩下载的Tomcat压缩包,呈现的目录结构如下. bin:目录存放一些启动和关闭Tomcat的可执行程序和相关内容.conf:存放关于Tomcat服务器的全局配置.lib:目录存放Tomcat运行 ...
- Codeforces 374A - Inna and Pink Pony
原题地址:http://codeforces.com/contest/374/problem/A 好久没写题目总结了,最近状态十分不好,无论是写程序还是写作业还是精神面貌……NOIP挂了之后总觉得缺乏 ...
- CentOS 7 安装JDK
卸载原系统上的JDK: [root@admin ~]# java -version 查看需要卸载的JDK清单: [root@admin ~]# rpm -qa | grep java java-1.6 ...
- jQuery遍历DOM
jQuery提供了多种遍历DOM的方法.遍历方法中最大的种类是树遍历. 向上遍历DOM树 parent():返回被选元素的直接父元素 parents():返回被选元素的所有祖先元素,它一直遍历到根元素 ...
- 如何使用ping和tracert命令测试网站访问速度
在我们平时访问的网站中,有一些网站访问速度非常快,比如百度搜索网站和一些门户网站,有些网站访问很慢,有些网站甚至无法访问.那么我们该如何判断这些网站的访问速度呢?下面我们就使用Windows的ping ...
- Ramdisk文件系统的制作与调试运行
开发环境:Fedora 9交叉编译工具链:arm-linux-gcc 4.3.2 with EABI嵌入式Linux内核版本:2.6.29.4-FriendlyARM.昨天写贴子的时候具体记不清了,今 ...
- Spring 教程(一)
一.Spring是什么 通常说的Spring其实指的是Spring Framework,它是Spring下的一个子项目,Spring围绕Spring Framework这个核心项目开发了大量其他项目, ...
- UVA 10462 Is There A Second Way Left? 次小生成树
模板题 #include <iostream> #include <algorithm> #include <cstdio> #include <cstdli ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
- 【转】C++类中对同类对象private成员访问
私有成员变量的概念,在脑海中的现象是,以private关键字声明,是类的实现部分,不对外公开,不能在对象外部访问对象的私有成员变量. 然而,在实现拷贝构造函数和赋值符函数时,在函数里利用对象直接访问了 ...