hdu 4578 Transformation (线段树)

http://acm.hdu.edu.cn/showproblem.php?pid=4578

Transformation

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

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杭州赛区全国邀请赛

与上一篇的线段树操作基本上都一样, 不过这次的这个的查询操作就有点麻烦, 因为它是查询 xpl+xpl+1+...+xpr 的总和.

线段树维护的值与上篇相近(不过这次我很调皮的用了另一种方法,就是线段树里面还存着l,r的),不过sum则是三个了.

更新操作的话也是对sum的更新不同了.

如果是乘操作的话总和什么的,三个都是直接乘上去就好了.

不过加操作的话就不一样了,因为平方,立方的不能直接加嘛,不过不怕! 直接加到里面再把它的式子展开, 再把各项求和就OK了!这里我就偷懒不写了.

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<iostream>
#define pb push_back
#define INF 0x3f3f3f3f
using namespace std;
typedef unsigned long long ULL;
typedef long long LL;
const int mod = 10007;
const int N = 1000005;
int n, m;
struct node{
    LL sum[5];
    int add, mul;
    int l, r;
    void init(){ add = 0; mul = 1; }
    int mid(){ return (l+r)>>1; }
    int len(){ return r-l+1; }
}tree[N<<2];
LL sq2(int x){ return (LL)x*x % mod; }
LL sq3(int x){ return sq2(x)*x % mod; }
void update(int rt, int ul, int ur, int add, int mul);
void pushdown(int rt){
    int mid = tree[rt].mid();
    update(rt<<1, tree[rt].l, mid, tree[rt].add, tree[rt].mul);
    update(rt<<1|1, mid+1, tree[rt].r, tree[rt].add, tree[rt].mul);
    tree[rt].init();
}
void pushup(int rt){
    tree[rt].sum[1] = (tree[rt<<1].sum[1] + tree[rt<<1|1].sum[1]) % mod;
    tree[rt].sum[2] = (tree[rt<<1].sum[2] + tree[rt<<1|1].sum[2]) % mod;
    tree[rt].sum[3] = (tree[rt<<1].sum[3] + tree[rt<<1|1].sum[3]) % mod;
}
int query(int rt, int ql, int qr, int c){
    if(ql <= tree[rt].l && tree[rt].r <= qr){
        //for(int i=1; i<=3; i++) printf("l-%d&r-%d : sum[%d] : %lld\n", tree[rt].l, tree[rt].r, i, tree[rt].sum[i]);
        return tree[rt].sum[c] % mod;
    }
    pushdown(rt);
    int mid = tree[rt].mid();
    int ret = 0;
    if(ql <= mid) ret += query(rt<<1, ql, qr, c);
    if(qr > mid) ret += query(rt<<1|1, ql, qr, c);
    pushup(rt);
    return ret % mod;
}
void update(int rt, int ul, int ur, int add, int mul){
    if(ul <= tree[rt].l && tree[rt].r <= ur){
        if(mul != 1){
            tree[rt].sum[1] = (tree[rt].sum[1] * mul) % mod;
            tree[rt].sum[2] = (tree[rt].sum[2] * sq2(mul) ) % mod;
            tree[rt].sum[3] = (tree[rt].sum[3] * sq3(mul) ) % mod;
            tree[rt].mul = (tree[rt].mul * mul) % mod;
            tree[rt].add = (tree[rt].add * mul) % mod;
        }
        if(add){
            int len = tree[rt].len();
            //printf("l - %d r - %d -->\n", tree[rt].l, tree[rt].r);
            //printf("len : %d add : %d mul : %d\n", len, add, mul);
            tree[rt].sum[3] = (tree[rt].sum[3] + 3*add * tree[rt].sum[2] % mod + 3 * tree[rt].sum[1] * sq2(add)%mod + sq3(add)*len%mod) % mod;
            tree[rt].sum[2] = (tree[rt].sum[2] + 2*(add * tree[rt].sum[1] % mod) % mod + sq2(add)*len%mod) % mod;
            tree[rt].sum[1] = (tree[rt].sum[1] + add * len) % mod;
            //printf("sum[1] : %lld - sum[2] : %lld - sum[3] : %lld\n", tree[rt].sum[1], tree[rt].sum[2], tree[rt].sum[3]);
            tree[rt].add = ((LL)tree[rt].add + add) % mod;
        }
        return ;
    }
    pushdown(rt);
    int mid = tree[rt].mid();
    if(ul <= mid) update(rt<<1, ul, ur, add, mul);
    if(mid < ur) update(rt<<1|1, ul, ur, add, mul);
    pushup(rt);
}
void showall(int rt = 1)
{
    int l = tree[rt].l;
    int r = tree[rt].r;
    printf("l-%d r-%d : ", l, r);
    printf("sum[1] : %lld - sum[2] : %lld - sum[3] : %lld\n", tree[rt].sum[1], tree[rt].sum[2], tree[rt].sum[3]);
    if(l == r) return ;
    showall(rt<<1);
    showall(rt<<1|1);
}
void build(int l, int r, int rt){
    tree[rt].init();
    tree[rt].l = l; tree[rt].r = r;
    tree[rt].sum[1] = tree[rt].sum[2] = tree[rt].sum[3] = 0;
    if(l == r) return ;
    int mid = (l+r)>>1;
    build(l, mid, rt<<1);
    build(mid+1, r, rt<<1|1);
}
void solve(){
    while(~scanf("%d %d", &n, &m) && n+m){
        build(1, n, 1);
        for(int i=0; i<m; i++){
            int op, a, b, c;
            scanf("%d %d %d %d", &op, &a, &b, &c);
            //printf("op:%d a:%d b:%d c:%d\n", op, a, b, c);
            if(op == 1) update(1, a, b, c, 1);
            else if(op == 2) update(1, a, b, 0, c);
            else if(op == 3) update(1, a, b, c, 0);
            else  printf("%d\n", query(1, a, b, c));
            //showall(1);
        }
    }
}
int main(void){
#ifdef DK
    freopen("/home/dk/桌面/1.in","r",stdin);
#endif // DK
    solve();
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值