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

BZOJ 1798 [Ahoi2009]维护序列seq (线段树)

时间:2016-05-12 15:09:07      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

题意

对于一个给定的序列有3种操作:
1.给一个区间的数乘c
2.给一个区间的数加c
3.查询区间和。

思路

就是普通的线段树区间更新,因为更新操作有两种,维护两个延迟标记就可以了,不过要注意乘和加在更新时相互之间的关系,在更新乘的时候之前加的数也要相应的乘,更新加的时候之前所乘的数没有改变。

代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define LL long long
#define Lowbit(x) ((x)&(-x))
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1|1
#define MP(a, b) make_pair(a, b)
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 7;
const double eps = 1e-8;
const double PI = acos(-1.0);

LL p;
LL sum[maxn<<2];
LL add[maxn<<2], mul[maxn<<2];

void PushUp(int rt)
{
    sum[rt] = (sum[rt<<1] + sum[rt<<1|1]) % p;
}

void PushDown(int rt, int m)
{
    add[rt<<1] = (add[rt<<1] * mul[rt] + add[rt]) % p;
    add[rt<<1|1] = (add[rt<<1|1] * mul[rt] + add[rt]) % p;
    mul[rt<<1] = (mul[rt<<1] * mul[rt]) % p;
    mul[rt<<1|1] = (mul[rt<<1|1] * mul[rt]) % p;
    sum[rt<<1] = (sum[rt<<1] * mul[rt] + add[rt] * (m - (m >> 1))) % p;
    sum[rt<<1|1] = (sum[rt<<1|1] * mul[rt] + add[rt] * (m >> 1)) % p;
    mul[rt] = 1, add[rt] = 0;
}

void build(int l, int r, int rt)
{
    add[rt] = 0, mul[rt] = 1;
    if (l == r)
    {
        scanf("%lld", &sum[rt]);
        //printf("%d %d %d\n", l, r, sum[rt]);
        return ;
    }
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
    PushUp(rt);
}

void update(int L, int R, int type, LL c, int l, int r, int rt)
{
    if (L <= l && r <= R)
    {
        if (type == 1)
        {
            mul[rt] = (mul[rt] * c) % p;
            add[rt] = (add[rt] * c) % p;
            sum[rt] = (sum[rt] * c) % p;
        }
        else if (type == 2)
        {
            add[rt] = (add[rt] + c) % p;
            sum[rt] = (sum[rt] + (LL)(r - l + 1) * c) % p;
        }
        return;
    }
    PushDown(rt, r - l + 1);
    int mid = (l + r) >> 1;
    if (L <= mid) update(L, R, type, c, lson);
    if (R > mid) update(L, R, type, c, rson);
    PushUp(rt);
}

LL query(int L, int R, int l, int r, int rt)
{
    if (L <= l && r <= R)
    {
        return sum[rt] % p;
    }
    PushDown(rt, r - l + 1);
    int mid = (l + r) >> 1;
    LL res = 0;
    if (L <= mid) res = (res + query(L, R, lson)) % p;
    if (R > mid) res = (res + query(L, R, rson)) % p;
    return res;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n, m;
    while (scanf("%d%lld", &n, &p) != EOF)
    {
        build(1, n, 1);
        scanf("%d", &m);
        for (int i = 1; i <= m; i++)
        {
            int t, l, r, c = 0;
            scanf("%d", &t);
            if (t == 1)
            {
                scanf("%d %d %d", &l, &r, &c);
                update(l, r, 1, c, 1, n, 1);
            }
            if (t == 2)
            {
                scanf("%d %d %d", &l, &r, &c);
                update(l, r, 2, c, 1, n, 1);
            }
            if (t == 3)
            {
                scanf("%d %d", &l, &r);
                printf("%lld\n", query(l, r, 1, n, 1));
            }
        }
    }
    return 0;
}

BZOJ 1798 [Ahoi2009]维护序列seq (线段树)

标签:

原文地址:http://blog.csdn.net/dreamon3/article/details/51365205

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