codeforces#321(div2) E. Kefa and Watch

codeforces#321(div2)  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 ≤ 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".

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.

题意:给一个字符串,两种操作,1表示将L,R中的字符替换为c,2表示判断子串[L,R]的循环节是否为d。

思路:首先,修改可以直接用线段树,难点在于判断子串循环节。容易知道,对字符串[L,R],如果[L,R-d]=[L+d,R],则循环节为d,因此可以通过比较哈希值来判断。那么问题来了,一般的字符串哈希都是一次预处理后缀和来计算子串哈希值的,而这里需要每次修改字符串的哈希值。这就需要理解字符串哈希的原理,那么哈希值是怎么计算的呢? 比如1234567,substr(2,4)的哈希值是432,substr(5,6)的哈希值是65,因此substr(2,5)的哈希值=432+65*10^3=65432,这样有了字符串合并的哈希值的计算,就可以直接给线段树的每个结点一个哈希值了。还有一个问题,如何修改哈希值? 题目中只要求替换,如果把[L,R]替换为c,则字符串为ccc....cc,因此,替换后的哈希值为c+c*10^1+c*10^2+...+c*10^(d-1),d为区间长度,这里求一个等比数列前n项和,用公式或者预处理前n项和,由于后面需要取模,用公式有除法需要用到逆元,速度会比较慢,所以这里直接用预处理比较好。以上的哈希值的进制x为10进制,实际中可以换为任何一个随机数作为进制的基数。到这里题目似乎已经解决了,然而还没有,由于出题人精心构造的数据,如果直接自然溢出会WA,这时有两种解决办法,多哈希或者取模大素数,这里多哈希也会被卡,而且写起来比较麻烦,因此字符串hash WA的时候最好改成模一个大素数。

还有一个注意点,就是哈希值合并的时候需要根据L,R,l,r,m的位置分情况计算,这是线段树合并最需要注意最关键的地方。

至此,题目圆满解决。

#include<bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

using namespace std;

typedef unsigned long long ull;
const int maxn=100100;
const int INF=(1<<29);

const ull x=13LL;
const ull p=1e9+7;
int n,m,k;
int q;
int op,L,R,c;
char s[maxn];
ull xp[maxn];
ull xs[maxn];

struct SegTree
{
    int l,r;
    int tag;
    ull h;
    void debug(int rt)
    {
        printf("rt=%2d l=%2d r=%2d tag=%2d h=%2llu\n",rt,l,r,tag,h);
    }
};SegTree T[maxn<<2];

void push_down(int rt,int l,int r,int m)
{
    if(T[rt].tag!=-1){
        T[rt<<1].tag=T[rt<<1|1].tag=T[rt].tag;
        T[rt<<1].h=((T[rt].tag%p)*(xs[m-l]%p))%p;
        T[rt<<1|1].h=((T[rt].tag%p)*(xs[r-m-1]%p))%p;
        T[rt].tag=-1;
    }
}

void push_up(int rt,int l,int r,int m)
{
    T[rt].h=(T[rt<<1].h+((T[rt<<1|1].h%p)*(xp[m-l+1]%p)%p))%p;
}

void build(int l,int r,int rt)
{
    T[rt].l=l;T[rt].r=r;
    T[rt].tag=-1;
    if(l==r){
        T[rt].h=s[l-1]-'0';
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(rt,l,r,m);
}

void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&r<=R){
        T[rt].tag=c;
        T[rt].h=(c*xs[r-l])%p;
        return;
    }
    int m=(l+r)>>1;
    push_down(rt,l,r,m);
    if(L<=m) update(L,R,c,lson);
    if(R>m) update(L,R,c,rson);
    push_up(rt,l,r,m);
}

ull query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R) return T[rt].h;
    int m=(l+r)>>1;
    push_down(rt,l,r,m);
    ull res=0;
    if(L<=m){
        if(R<=m) res=query(L,R,lson);
        else res=(query(L,R,lson)+((query(L,R,rson)%p)*xp[m-max(l,L)+1]))%p;
    }
    else res=query(L,R,rson);
    return res%p;
}

bool judge(int l1,int r1,int l2,int r2)
{
    ull h1=query(l1,r1,1,n,1);
    ull h2=query(l2,r2,1,n,1);
    return h1==h2;
}

void Init()
{
    xp[0]=1;
    for(ull i=1;i<maxn;i++) xp[i]=(xp[i-1]*x)%p;
    xs[0]=1;
    for(ull i=1;i<maxn;i++) xs[i]=(xs[i-1]+xp[i])%p;
}

int main()
{
    freopen("in.txt","r",stdin);
    Init();
    while(~scanf("%d%d%d",&n,&m,&k)){
        scanf("%s",s);
        q=m+k;
        build(1,n,1);
        while(q--){
            scanf("%d%d%d%d",&op,&L,&R,&c);
            if(op==1) update(L,R,c,1,n,1);
            else{
                if(R-L+1==c) puts("YES");
                else puts(judge(L,R-c,L+c,R)?"YES":"NO");
            }
        }
    }
    return 0;
}
View Code

 

posted @ 2015-09-24 19:20  __560  阅读(334)  评论(0编辑  收藏  举报