codeforces 580 E. Kefa and Watch(hash+线段树)

来源:互联网 发布:x98 3g桌面软件 编辑:程序博客网 时间:2024/04/30 13:03

E. Kefa and Watch
time limit per test
1.5 seconds
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 ≤ 1051 ≤ 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 ≤ n0 ≤ 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 ≤ n1 ≤ d ≤ r - l + 1).

Output

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

Examples
input
3 1 21122 2 3 11 1 3 82 1 2 1
output
NOYES
input
6 2 33349342 2 5 21 4 4 32 1 6 31 2 3 82 3 6 1
output
NOYESNO
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.


题意:

给出一个只包含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上会卡自然溢出。


这题调了两晚上,最后发现是main里面没写init(),这样还居然过了前几个样例。。。


#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;}

0 0
原创粉丝点击