hdu 4578 Transformation【线段树区间&思维】

Problem Description
Yuanfang is puzzled with the question below:
There are n integers, a1, a2, …, an. The initial values of them are 0. There are four kinds of operations.
Operation 1: Add c to each number between ax and ay inclusive. In other words, do transformation ak<—ak+c, k = x,x+1,…,y.
Operation 2: Multiply c to each number between ax and ay inclusive. In other words, do transformation ak<—ak×c, k = x,x+1,…,y.
Operation 3: Change the numbers between ax and ay to c, inclusive. In other words, do transformation ak<—c, k = x,x+1,…,y.
Operation 4: Get the sum of p power among the numbers between ax and ay inclusive. In other words, get the result of axp+ax+1p+…+ay p.
Yuanfang has no idea of how to do it. So he wants to ask you to help him.
Input
There are no more than 10 test cases.
For each case, the first line contains two numbers n and m, meaning that there are n integers and m operations. 1 <= n, m <= 100,000.
Each the following m lines contains an operation. Operation 1 to 3 is in this format: “1 x y c” or “2 x y c” or “3 x y c”. Operation 4 is in this format: “4 x y p”. (1 <= x <= y <= n, 1 <= c <= 10,000, 1 <= p <= 3)
The input ends with 0 0.
Output
For each operation 4, output a single integer in one line representing the result. The answer may be quite large. You just need to calculate the remainder of the answer when divided by 10007.
Sample Input
5 5
3 3 5 7
1 2 4 4
4 1 5 2
2 2 5 8
4 3 5 3
0 0
Sample Output
307
7489
Source
2013ACM-ICPC杭州赛区全国邀请赛

题意:n个初始值为零的点,m个操作,操作有四种:
(1)“1 x y c”,把区间 [x,y] 上的值全部加c;
(2)”2 x y c”,把区间 [x,y] 上的值全部乘以c;
(3)”3 x y c” 把区间 [x,y]上的值全部赋值为c;
(4)”4 x y p” 求区间 [x,y] 上值的p次方和1<=p<=3;
分析:
1,这三个更新会有冲突,lazy无法确切记录三种前后,想办法把三种操作转化成一种,1操作可以当成 x1+c x ∗ 1 + c ,2操作可以当成 xc+0 x ∗ c + 0 ,3操作可以当成 x0+1 x ∗ 0 + 1 ,这样就只需要两个lazy标记乘和加,我先处理的是乘然后再加;
2,p很小可预处理p,查询时可以做到区间查询,需要公式推导,当x表示p = 2的区间和时,乘和加的操作公式转化: (ax+c)2=a2x2+2acx+c2 ( a x + c ) 2 = a 2 x 2 + 2 a c x + c 2 ,三次方也是如此。但有点需要注意,x都是未处理之前的,所以更新的时候,先从次方高的更新;
搞清楚以上两点,写起来就是两个lazy的普通区间线段树了.

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;

const LL mod = 10007;
const LL MAXN = 1e5 + 5;
LL dp1[MAXN * 6], dp2[MAXN * 6], dp3[MAXN * 6];
LL lazy_mul[MAXN * 6], lazy_add[MAXN * 6];

inline void pushup(LL root) {
    dp1[root] = (dp1[root << 1] + dp1[root << 1 | 1]) % mod;
    dp2[root] = (dp2[root << 1] + dp2[root << 1 | 1]) % mod;
    dp3[root] = (dp3[root << 1] + dp3[root << 1 | 1]) % mod;
}

inline void pushdown(LL root, LL L, LL R) {
    LL mid = (L + R) >> 1;
    if(lazy_mul[root] != 1) {
        LL a = lazy_mul[root] % mod;
        (lazy_mul[root << 1] *= a) %= mod;
        (lazy_mul[root << 1 | 1] *= a) %= mod;
        (lazy_add[root << 1] *= a) %= mod;
        (lazy_add[root << 1 | 1] *= a) %= mod;
        (dp3[root << 1] *= a * a * a) %= mod;
        (dp3[root << 1 | 1] *= a * a * a) %= mod;
        (dp2[root << 1] *= a * a) %= mod;
        (dp2[root << 1 | 1] *= a * a) %= mod;
        (dp1[root << 1] *= a) %= mod;
        (dp1[root << 1 | 1] *= a) %= mod;
        lazy_mul[root] = 1;
    }
    if(lazy_add[root]) {
        LL a = lazy_add[root] % mod;
        (lazy_add[root << 1] += a) %= mod;
        (lazy_add[root << 1 | 1] += a) %= mod;
        (dp3[root << 1] += 3 * a * dp2[root << 1] + 3 * a * a * dp1[root << 1] + a * a * a * (mid - L + 1)) %= mod;
        (dp3[root << 1 | 1] += 3 * a * dp2[root << 1 | 1] + 3 * a * a * dp1[root << 1 | 1] + a * a * a * (R - mid)) %= mod;
        (dp2[root << 1] += 2 * a * dp1[root << 1] + (mid - L + 1) * a * a) %= mod;
        (dp2[root << 1 | 1] += 2 * a * dp1[root << 1 | 1] + (R - mid) * a * a) %= mod;
        (dp1[root << 1] += a * (mid - L + 1)) %= mod;
        (dp1[root << 1 | 1] += a * (R - mid)) %= mod;
        lazy_add[root] = 0;
    }
}

inline void init() {
    for(LL i = 0; i < (MAXN * 5); ++i) {
        dp1[i] = dp2[i] = dp3[i] = 0;
        lazy_add[i] = 0;
        lazy_mul[i] = 1;
    }
}

inline void updata(LL root, LL L, LL R, LL l, LL r, LL b, LL op) {
    if(l <= L && R <= r) {
        LL a, c;
        if(op == 1) {
            a = 1;
            c = b;
            (lazy_add[root] += b) %= mod;
        }
        else if(op == 2) {
            a = b;
            c = 0;
            (lazy_mul[root] *= b) %= mod;
            (lazy_add[root] *= b) %= mod;
        }
        else if(op == 3) {
            a = 0;
            c = b;
            lazy_mul[root] = 0; //注意这一步不是1
            lazy_add[root] = b;
        }
        dp3[root] = (a * a * a * dp3[root] + 3 * a * a * c * dp2[root] + 3 * a * c * c * dp1[root] + c * c * c * (R - L + 1)) % mod;
        dp2[root] = (a * a * dp2[root] + c * c * (R - L + 1) + 2 * a * c * dp1[root]) % mod;
        dp1[root] = (a * dp1[root] + c * (R - L + 1)) % mod;
        return ;
    }
    pushdown(root, L, R);
    LL mid = (L + R) >> 1;
    if(l <= mid) updata(root << 1, L, mid, l, r, b, op);
    if(mid < r) updata(root << 1 | 1, mid + 1, R, l, r, b, op);
    pushup(root);
}

inline LL query(LL root, LL L, LL R, LL l, LL r, LL op) {
    if(l <= L && R <= r) {
        if(op == 1) return dp1[root] % mod;
        if(op == 2) return dp2[root] % mod;
        if(op == 3) return dp3[root] % mod;
    }
    LL ans = 0;
    pushdown(root, L, R);
    LL mid = (L + R) >> 1;
    if(l <= mid) ans = (ans + query(root << 1, L, mid, l, r, op)) % mod;
    if(mid < r) ans = (ans + query(root << 1 | 1, mid + 1, R, l, r, op)) % mod;
    return ans;
}

int main() {
    // freopen("Data.in", "r", stdin);
    // freopen("Data.out", "w", stdout);
    LL n, m;
    while(scanf("%lld %lld", &n, &m) && (n + m)) {
        init();
        while(m--) {
            LL op, x, y, c;
            scanf("%lld %lld %lld %lld", &op, &x, &y, &c);
            if(op == 4) {
                printf("%lld\n", query(1, 1, n, x, y, c) % mod);
            }
            else {
                updata(1, 1, n, x, y, c, op);
            }
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值