码迷,mamicode.com
首页 > 其他好文 > 详细

test

时间:2019-11-30 19:33:44      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:class   roo   turn   shu   pre   else   sum   root   cpp   

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

test

标签:class   roo   turn   shu   pre   else   sum   root   cpp   

原文地址:https://www.cnblogs.com/lrj124/p/11963506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!