【AHOI2009】 维护序列 - 线段树

题目描述

老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成。 有长为N的数列,不妨设为a1,a2,…,aN 。有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一段数全部加一个值; (3)询问数列中的一段数的和,由于答案可能很大,你只需输出这个数模P的值。

思路

线段树,打个乘法lazy标记即可

#include <cstdio>
const int maxn = 100000 + 10;
struct Seg { long long l,r,sum,add,mul; } tree[maxn*4];
long long p;
long long n,m;
inline void pushup(long long root) { tree[root].sum = tree[root<<1].sum+tree[root<<1|1].sum; tree[root].sum %= p; }
inline void BuildTree(long long l,long long r,long long root) {
    tree[root].l = l;
    tree[root].r = r;
    tree[root].mul = 1;
    if (l == r) {
        scanf("%lld",&tree[root].sum);
        tree[root].sum %= p;
        return;
    }
    long long mid = l+r>>1;
    BuildTree(l,mid,root<<1);
    BuildTree(mid+1,r,root<<1|1);
    pushup(root);
}
inline void pushdown(long long root) {
    if (tree[root].mul != 1) {
        tree[root<<1].mul = tree[root<<1].mul*tree[root].mul%p;
        tree[root<<1|1].mul = tree[root<<1|1].mul*tree[root].mul%p;
        tree[root<<1].add = tree[root<<1].add*tree[root].mul%p;
        tree[root<<1|1].add = tree[root<<1|1].add*tree[root].mul%p;
        tree[root<<1].sum = tree[root<<1].sum*tree[root].mul%p;
        tree[root<<1|1].sum = tree[root<<1|1].sum*tree[root].mul%p;
        tree[root].mul = 1;
    }
    if (tree[root].add != 0) {
        tree[root<<1].add = (tree[root<<1].add+tree[root].add)%p;
        tree[root<<1|1].add = (tree[root<<1|1].add+tree[root].add)%p;
        tree[root<<1].sum = (tree[root<<1].sum+tree[root].add*(tree[root<<1].r-tree[root<<1].l+1))%p;
        tree[root<<1|1].sum = (tree[root<<1|1].sum+tree[root].add*(tree[root<<1|1].r-tree[root<<1|1].l+1))%p;
        tree[root].add = 0;
    }
}
inline void UpdateAdd(long long ql,long long qr,long long l,long long r,long long root,long long x) {
    if (ql > r || qr < l) return;
    if (ql <= l && qr >= r) {
        tree[root].add = (tree[root].add+x)%p;
        tree[root].sum = (tree[root].sum+x*(r-l+1))%p;
        return;
    }
    pushdown(root);
    long long mid = l+r>>1;
    UpdateAdd(ql,qr,l,mid,root<<1,x);
    UpdateAdd(ql,qr,mid+1,r,root<<1|1,x);
    pushup(root);
}
inline void UpdateMul(long long ql,long long qr,long long l,long long r,long long root,long long x) {
    if (ql > r || qr < l) return;
    if (ql <= l && qr >= r) {
        tree[root].add = tree[root].add*x%p;
        tree[root].mul = tree[root].mul*x%p;
        tree[root].sum = tree[root].sum*x%p;
        return;
    }
    pushdown(root);
    long long mid = l+r>>1;
    UpdateMul(ql,qr,l,mid,root<<1,x);
    UpdateMul(ql,qr,mid+1,r,root<<1|1,x);
    pushup(root);
}
inline long long Query(long long ql,long long qr,long long l,long long r,long long root) {
    if (ql > r || qr < l) return 0;
    if (ql <= l && qr >= r) return tree[root].sum;
    pushdown(root);
    long long mid = l+r>>1;
    return (Query(ql,qr,l,mid,root<<1)+Query(ql,qr,mid+1,r,root<<1|1))%p;
}
int main() {
    scanf("%lld%lld",&n,&p);
    BuildTree(1,n,1);
    scanf("%lld",&m);
    while (m--) {
        long long val;
        long long op,l,r;
        scanf("%lld%lld%lld",&op,&l,&r);
        if (op == 1) {
            scanf("%lld",&val);
            UpdateMul(l,r,1,n,1,val);
        } else if (op == 2) {
            scanf("%lld",&val);
            UpdateAdd(l,r,1,n,1,val);
        } else printf("%lld\n",Query(l,r,1,n,1));
    }
    return 0;
}
posted @ 2018-03-24 21:49  lrj124  阅读(183)  评论(0编辑  收藏  举报