Codeforces 580E Kefa and Watch 线段树

参考:http://blog.csdn.net/w703710691d/article/details/48687603


代码:

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define ll long long
const ll mod1 = 1e9+7;
const ll mod2 = 1e9+9;

char s[100010];
ll p1[100010], sump1[100010];
ll p2[100010], sump2[100010];
struct node
{
    int l,r;
    ll lazy, sum1, sum2;
}tree[4*100010];

void pushup(int id)
{
    if(tree[id<<1].lazy == tree[id<<1|1].lazy)
        tree[id].lazy = tree[id<<1].lazy;
    else
        tree[id].lazy = 'a';
    int len = tree[id<<1].r - tree[id<<1].l + 1;
    tree[id].sum1 = (p1[len] * tree[id<<1|1].sum1 % mod1 + tree[id<<1].sum1) % mod1;
    tree[id].sum2 = (p2[len] * tree[id<<1|1].sum2 % mod2 + tree[id<<1].sum2) % mod2;
}

void pushdown(int id)
{
    if(tree[id].lazy != 'a'){
        tree[id<<1].lazy = tree[id<<1|1].lazy = tree[id].lazy;
        tree[id<<1].sum1 = tree[id].lazy * sump1[tree[id<<1].r-tree[id<<1].l] % mod1;
        tree[id<<1|1].sum1 = tree[id].lazy * sump1[tree[id<<1|1].r-tree[id<<1|1].l] % mod1;
        tree[id<<1].sum2 = tree[id].lazy * sump2[tree[id<<1].r-tree[id<<1].l] % mod2;
        tree[id<<1|1].sum2 = tree[id].lazy * sump2[tree[id<<1|1].r-tree[id<<1|1].l] % mod2;
    }
}
void build(int id, int x, int y)
{
    tree[id].l = x;
    tree[id].r = y;
    if(x == y){
        tree[id].lazy = s[x];
        tree[id].sum1 = s[x];
        tree[id].sum2 = s[x];
        return ;
    }
    int mid = (x + y) >> 1;
    build(id<<1, x, mid);
    build(id<<1|1, mid+1, y);
    pushup(id);
}

void update(int id, int x, int y, int z)
{
    if(tree[id].l == x && tree[id].r == y){
        tree[id].lazy = z;
        tree[id].sum1 = sump1[y-x] * z % mod1;
        tree[id].sum2 = sump2[y-x] * z % mod2;
        return ;
    }
    pushdown(id);
    int mid = (tree[id].l + tree[id].r) >> 1;
    if(y <= mid)
        update(id<<1, x, y, z);
    else if(x >= mid+1)
        update(id<<1|1, x, y, z);
    else{
        update(id<<1, x, mid, z);
        update(id<<1|1, mid+1, y, z);
    }
    pushup(id);
}

ll query(int id, int x, int y, int z)
{
    if(tree[id].l == x && tree[id].r == y){
        if(z == 1)
            return tree[id].sum1;
        else
            return tree[id].sum2;
    }
    pushdown(id);
    int mid = (tree[id].l + tree[id].r) >> 1;
    if(y <= mid)
        return query(id<<1, x, y, z);
    else if(x >= mid+1)
        return query(id<<1|1, x, y, z);
    else{
        ll t1 = query(id<<1, x, mid, z);
        ll t2 = query(id<<1|1, mid + 1, y, z);
        int len = mid - x + 1;
        if(z == 1)
            return (t1 + t2 * p1[len] % mod1) % mod1;
        else
            return (t1 + t2 * p2[len] % mod2) % mod2;
    }
}
void init()
{
    ll t = 131;
    p1[0] = sump1[0] = 1;
    p2[0] = sump2[0] = 1;
    for(int i=1;i<=100005;i++){
        p1[i] = p1[i-1] * 131 % mod1;
        p2[i] = p2[i-1] * 131 % mod2;
        sump1[i] = (sump1[i-1] + p1[i]) % mod1;
        sump2[i] = (sump2[i-1] + p2[i]) % mod2;
    }
}
int main()
{
    int n, m, k;
    cin>>n>>m>>k;
    scanf("%s",s);
    init();
    build(1,0,n-1);
    for(int i = 0; i < k + m; ++i){
        int op, l, r, c;
        cin >> op >> l >> r >> c;
        l--,r--;
        if(op == 1){
            update(1, l, r, c + '0');
        } else {
            if(r - l + 1 <= c)
                printf("YES\n");
            else if(r - l + 1 <= 2*c){
                int len = (r-l+1)-c;
                ll l1 = query(1, l, l + len - 1, 1);
                ll r1 = query(1, r - len + 1, r, 1);
                ll l2 = query(1, l, l + len - 1, 2);
                ll r2 = query(1, r - len + 1, r, 2);
                if(l1 == r1 && l2 == r2)
                    printf("YES\n");
                else
                    printf("NO\n");
            }
            else{
                int len = (r-l+1)%c;
                int flag = 0;
                if(len != 0){
                    ll l1 = query(1, l, l + len - 1, 1);
                    ll r1 = query(1, r - len + 1, r, 1);
                    ll l2 = query(1, l, l + len - 1, 2);
                    ll r2 = query(1, r - len + 1, r, 2);
                    if(l1 != r1 || l2 != r2)
                        flag = 1;
                }
                if(flag == 1){
                    printf("NO\n");
                    continue;
                }
                r -= len;
                ll L1 = query(1, l, r-c, 1);
                ll R1 = query(1, l+c, r, 1);
                ll L2 = query(1, l, r-c, 2);
                ll R2 = query(1, l+c, r, 2);
                if(L1 == R1 && L2 == R2)
                    printf("YES\n");
                else
                    printf("NO\n");
            }
        }
    }
}


你可能感兴趣的:(线段树,线段树)