HDU 4578 Transformation (线段树)

27 篇文章 0 订阅

题意:

4种操作:

区间加和, 区间赋值,区间乘法, 查询区间 一次方和,二次方和,三次方和。

思路:

区间赋值优先级最高。

直接把加法标记和乘法标记直接赋0, 然后计算和。

其次是乘法, 先计算和, 如果发现有加法标记,在更新一下加法标记, 因为 (a+b)*x = a*x + b*x;  相当于原数乘以x 在加bx ,此时加法标记由b变成了bx。

最后是加法。

更新区间和时, 算一下递推关系即可。


智障错误, wa了两遍= =

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 100000 + 10;
const int mod = 10007;
int n, q;

struct node{
    int l,r;
    int sum[3];
    int addr;
    int setv;
    int mult;
}nod[maxn<<2];

void build(int l,int r,int o){

    nod[o].l = l;
    nod[o].r = r;
    for (int i = 0; i < 3; ++i){
        nod[o].sum[i] = 0;
    }
    nod[o].addr = nod[o].setv = 0;
    nod[o].mult = 1;
    if (l == r) return;
    int m = l + r >> 1;
    build(l,m,o<<1);
    build(m+1,r,o<<1|1);
}

void pushup(int o){
    int lson = o << 1;
    int rson = o << 1 | 1;
    for (int i = 0; i < 3; ++i){
        nod[o].sum[i] = (nod[lson].sum[i] + nod[rson].sum[i]) % mod;
//        printf("%d %d %d\n", nod[lson].sum[i], nod[rson].sum[i], nod[o].sum[i]);
    }
}

void uset(int o,int c,int l,int r){
    nod[o].setv = c % mod;
    nod[o].addr = 0;
    nod[o].mult = 1;
    nod[o].sum[2] = (((((r-l+1)%mod)*c)%mod*c)%mod*c)%mod;
    nod[o].sum[1] = ((((r-l+1)%mod)*c)%mod*c)%mod;
    nod[o].sum[0] = ((r-l+1)%mod*c)%mod;
}

void uadd(int o,int c,int l,int r){
    nod[o].addr = (nod[o].addr + c) % mod;
    nod[o].sum[2] = (nod[o].sum[2] + (  ( ((3*c*c)%mod *nod[o].sum[0])%mod + (3*c*nod[o].sum[1])%mod)%mod  + (((r-l+1)%mod *c)%mod *c)%mod *c) % mod) % mod;
    nod[o].sum[1] = (nod[o].sum[1] + ( (((((r-l+1)%mod)*c)%mod *c)%mod + (2*c*nod[o].sum[0])%mod ) )%mod) % mod;
    nod[o].sum[0] = (nod[o].sum[0] +  ((r-l+1)%mod * c))%mod;


}
void umul(int o,int c,int l,int r){
    nod[o].mult = (nod[o].mult * c) %mod;
    nod[o].sum[2] = (nod[o].sum[2] * ( ((c*c)%mod *c)%mod )) % mod ;
    nod[o].sum[1] = (nod[o].sum[1] * ((c*c)%mod) )%mod;
    nod[o].sum[0] = (nod[o].sum[0] * c) % mod;
    if (nod[o].addr){
        nod[o].addr = (nod[o].addr * c) % mod;
    }
}

void pushdown(int o){
    int l = nod[o].l;
    int r = nod[o].r;
    int m = l + r >> 1;
    int lson = o << 1;
    int rson = o << 1 | 1;


    if (nod[o].setv != 0){
        uset(lson, nod[o].setv, l, m);
        uset(rson, nod[o].setv, m+1, r);
        nod[o].setv = 0;
    }
    if (nod[o].mult != 1){
        umul(lson, nod[o].mult, l, m);
        umul(rson, nod[o].mult, m+1, r);
        nod[o].mult = 1;
    }
    if (nod[o].addr != 0){
        uadd(lson, nod[o].addr, l, m);
        uadd(rson, nod[o].addr, m+1, r);
        nod[o].addr = 0;
    }
}

void update(int L,int R,int c,int l,int r,int o,int way){
    if (L <= l && r <= R){
        if (way == 3){
            uset(o,c,l,r);
//            printf("%d %d %d o = %d\n",l,r, nod[o].sum[1],o);
            return;
        }
        else if (way == 2){
            umul(o,c,l,r);
        }
        else {
            uadd(o,c,l,r);
        }
        return ;
    }

    pushdown(o);
    int m = l + r >> 1;
    if (m >= L){
        update(L,R,c,l,m,o<<1,way);
    }
    if (m < R){
        update(L,R,c,m+1,r,o<<1|1,way);
    }

    pushup(o);
//    printf("pushup: %d %d %d o = %d\n",l,r, nod[o].sum[1],o);
}


int query(int L,int R,int c,int l,int r,int o){
    if (L <= l && r <= R){
        return nod[o].sum[c];
    }
    int m = l + r >> 1;
    int ans = 0;
    pushdown(o);
    if (m >= L){
        ans = (ans + query(L,R,c, l, m, o << 1)) % mod;
    }
    if (m < R){
        ans = (ans + query(L,R,c,m+1, r, o<<1|1)) % mod;

    }
    pushup(o);
    return ans;


}

int main(){
    while(~scanf("%d %d",&n, &q) && (n || q)){
        int op,x,y,c;
        build(1,n,1);
        while(q--){
            scanf("%d %d %d %d",&op, &x, &y, &c);
            if (op != 4){
                update(x,y,c,1,n,1,op);
            }
            else {
//                printf("%d\n",nod[1].sum[1]);
                printf("%d\n", query(x,y,c-1,1,n,1));
            }
        }
    }
    return 0;
}


Transformation

Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 65535/65536 K (Java/Others)
Total Submission(s): 5450    Accepted Submission(s): 1325


Problem Description
Yuanfang is puzzled with the question below: 
There are n integers, a 1, a 2, …, a n. The initial values of them are 0. There are four kinds of operations.
Operation 1: Add c to each number between a x and a y inclusive. In other words, do transformation a k<---a k+c, k = x,x+1,…,y.
Operation 2: Multiply c to each number between a x and a y inclusive. In other words, do transformation a k<---a k×c, k = x,x+1,…,y.
Operation 3: Change the numbers between a x and a y to c, inclusive. In other words, do transformation a k<---c, k = x,x+1,…,y.
Operation 4: Get the sum of p power among the numbers between a x and a y inclusive. In other words, get the result of a x p+a x+1 p+…+a y  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
 

Recommend
We have carefully selected several similar problems for you:   6032  6031  6030  6029  6028 
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值