codeforces 580 E. Kefa and Watch(hash+线段树)
来源:互联网 发布:x98 3g桌面软件 编辑:程序博客网 时间:2024/04/30 13:03
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.
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 c (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).
For each check on a single line print "YES" if the watch passed it, otherwise print "NO".
3 1 21122 2 3 11 1 3 82 1 2 1
NOYES
6 2 33349342 2 5 21 4 4 32 1 6 31 2 3 82 3 6 1
NOYESNO
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.
题意:
给出一个只包含0至9的,长度为n的字符串s,(m+k)个操作,包含m个修改,k个询问。
.1 l r c 表示将l,r区间的字符变成c。
.2 l r d 需要回答l,r区间的字符串是否是由长度为d的循环节循环组成(周期是否为d),是的话输出"YES", 否则输出"NO"。
1 <= n <= 1e5, 1 <= m + k <= 1e5, 1 <= l <= r <= n, 0 <= c <= 9, 1 <= d <= r - l + 1。
题解:
假若不考虑修改的话,那这题就是一个经典的KMP问题,只需要判定S[l+d, r] 是否和 S[l, r-d]相同即可。
这题因为需要进行修改操作,所以我们可以考虑利用hash来判定上述问题,然后用线段树来维护字符串hash值即可。
需要注意的是,cf上会卡自然溢出。
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<queue>#include<stack>using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define pb push_back#define fi first#define se secondtypedef vector<int> VI;typedef long long ll;typedef pair<int,int> PII;const ll mod=1000000007;const int maxn=1e5+100;const ll key=131;ll f[maxn];ll inv;char s[maxn];struct node{ int l,r; int lazy; ll sum;}seg[maxn*4];ll pow(int x,int y){ ll ans=1,t=x; while(y) { if(y&1) ans=(ans*t)%mod; t=(t*t)%mod; y/=2; } return ans%mod;}void init(){ f[0]=1ll; rep(i,1,maxn) f[i]=(f[i-1]*key)%mod; inv=pow(key-1,mod-2)%mod;}void up(int i){ if(seg[i].l!=seg[i].r) { int m=(seg[i].l+seg[i].r)/2; seg[i].sum=(seg[i*2].sum%mod*f[seg[i].r-m]%mod+seg[i*2+1].sum)%mod; }}void build(int i,int l,int r){ seg[i].l=l,seg[i].r=r,seg[i].lazy=-1; if(l==r) { seg[i].sum=s[l]-'0'; //cout << seg[i].sum << endl; return; } int m=(l+r)/2; build(i*2,l,m); build(i*2+1,m+1,r); up(i);}ll cal(int c,int l){ return 1ll*c*(pow(key,l)+mod-1)%mod*inv%mod;}void pushdown(int i){ if(seg[i].l!=seg[i].r&&seg[i].lazy!=-1) { int m=(seg[i].l+seg[i].r)/2; seg[i*2].lazy=seg[i*2+1].lazy=seg[i].lazy; seg[i*2].sum=cal(seg[i].lazy,m-seg[i].l+1)%mod; seg[i*2+1].sum=cal(seg[i].lazy,seg[i].r-m)%mod; seg[i].lazy=-1; }}void update(int i,int l,int r,int c){ if(seg[i].l==l&&seg[i].r==r) { seg[i].lazy=c; seg[i].sum=cal(c,seg[i].r-seg[i].l+1); return; } if(seg[i].lazy!=-1) pushdown(i); int m=(seg[i].r+seg[i].l)/2; if(r<=m) update(i*2,l,r,c); else if(l>m) update(i*2+1,l,r,c); else { update(i*2,l,m,c),update(i*2+1,m+1,r,c); } up(i);}ll query(int i,int l,int r){ if(seg[i].l==l&&seg[i].r==r) { return seg[i].sum%mod; } pushdown(i); int m=(seg[i].l+seg[i].r)/2; if(r<=m) return query(i*2,l,r); else if(l>m) return query(i*2+1,l,r); else { ll ans=0; ans=(query(i*2,l,m)*pow(key,r-m)%mod+query(i*2+1,m+1,r))%mod; return ans%mod; }}int main(){ int n,m,k; scanf("%d%d%d",&n,&m,&k); init(); scanf("%s",s+1); build(1,1,n); rep(i,1,m+k+1) { int op,l,r,v; scanf("%d%d%d%d",&op,&l,&r,&v); if(op==1) update(1,l,r,v); else { if(r-l+1<=v) puts("YES");// else { ll ans1=query(1,l,r-v),ans2=query(1,l+v,r); ll res=(ans1-ans2+mod)%mod; if(res==0) puts("YES"); else puts("NO"); } } } return 0;}
- Codeforces 580E Kefa and Watch 线段树+双值Hash
- codeforces 580 E. Kefa and Watch (字符串hash + 线段树)
- codeforces 580 E. Kefa and Watch(hash+线段树)
- CodeForces 580 E.Kefa and Watch(hash+线段树)
- codeforces 321# E. Kefa and Watch (线段树+字符串hash)
- Codeforces 580E Kefa and Watch 线段树
- Codeforces Round #321 (Div. 2) E - Kefa and Watch(hash + 线段树)
- CODEFORCES ROUND #321 (DIV. 2) E.Kefa and Watch(线段树+hash)
- Codeforces #321 E. Kefa and Watch (线段树、哈希)
- CODEFORCES 580E Kefa and Watch
- 查询[l,r]区间数字串是不是周期为d的串 线段树区间修改+双重hash Codeforces #321 E. Kefa and Watch
- 字符串hash+线段树 Codeforces580E Kefa and Watch
- CF#321-DIV2-E. Kefa and Watch-线段树+字符串哈希
- Codeforces Round #321 Kefa and Watch(字符串哈希+线段树)
- 【Code Forces 321E】【线段树+字符串哈希】Kefa and Watch 字符串修改 循环节判定
- codeforces580E. Kefa and Watch
- 【线段树+字符串hash】 codeforces 213E Two Permutations
- Codeforces Round #321 (Div. 2)E 线段树+字符串hash
- 【网络流】bzoj3901(?)Magic
- poj1664
- ionic2 中隐藏子页面tabs选项卡的三种方法
- ubuntu 15或者16版本 安装ibus中文输入法
- 嵌入式面试题——有意思的C语言题
- codeforces 580 E. Kefa and Watch(hash+线段树)
- iOSBase64POST上传图片和FormData方式比较
- 浅谈接口自动化如何应用与工作中与开发模式实践
- ubuntu提交代码到远程仓库
- bootstrap div 居中
- ubuntu一键安装mongodb最新版
- GDB命令
- 数据流DataOutputStream
- intelliJ快捷键