hdu 4578 Transformation(线段树+多种操作)

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

思路:
这道题有三种询问: add(加) , mul(乘),same(赋值)。所以lazy标记要有三个,
如果三个标记同时出现的处理方法——当更新same操作时,就把add标记和mul标记全部取消;

当更新mul操作时,如果当前节点add标记存在,就把add标记改为:add * mul。

这样的话就可以在PushDown()操作中先执行same,然后mul,最后add。

麻烦的是有三种询问:和 , 平方和 , 立方和。对于same和mul操作来说,这三种询问都比较好弄。

对于add操作,和的话就比较好弄,按照正常方法就可以;

平方和这样来推: (a+c)2=a2+c2+2ac , 即sum2[rt] = sum2[rt] + (r - l + 1) * c * c + 2 * sum1[rt] * c;

立方和这样推: (a+c)3=a3+c3+3ac(a+c) , 即sum3[rt] = sum3[rt] + (r - l + 1) * c * c * c + 3 *c* (sum2[rt] + sum1[rt] * c);

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
const int maxn=1e5+10;
const int mod=10007;

struct Segtree
{
    int le,ri,add,mul,same,sum[4];
    int mid()
    {
        return (le+ri)>>1;
    }
} tree[maxn<<2];
int len,tmp1,tmp2,tmp3,sum1,sum2,sum3;
int n,m;

void Build(int rt,int le,int ri)
{
    tree[rt].le=le,tree[rt].ri=ri;
    tree[rt].add=tree[rt].mul=tree[rt].same=0;
    tree[rt].sum[1]=tree[rt].sum[2]=tree[rt].sum[3]=0;
    if(le==ri)
        return ;
    int mid=tree[rt].mid();
    Build(LL(rt),le,mid);
    Build(RR(rt),mid+1,ri);
}

void fun_add(int rt,int num)
{
    tree[rt].add=(tree[rt].add+num)%mod;

    sum1=tree[rt].sum[1],sum2=tree[rt].sum[2],sum3=tree[rt].sum[3];
    len=tree[rt].ri-tree[rt].le+1;

    tmp1=(num*len)%mod;
    tree[rt].sum[1]=(sum1+tmp1)%mod;

    tmp2=(num*num)%mod;
    tree[rt].sum[2]=(sum2+(2*sum1*num)%mod+(tmp2*len)%mod)%mod;//(a+num)^2*len=a^2*len+2*a*num*len+num*num*len

    tmp3=(tmp2*num)%mod;
    tree[rt].sum[3]=(sum3+(3*sum2*num)%mod+(3*sum1*tmp2)%mod+(tmp3*len)%mod)%mod;//同理
}

void fun_mul(int rt,int num)
{
    if(tree[rt].mul)
        tree[rt].mul=(tree[rt].mul*num)%mod;
    else
        tree[rt].mul=num;

    tree[rt].add=(tree[rt].add*num)%mod;

    sum1=tree[rt].sum[1],sum2=tree[rt].sum[2],sum3=tree[rt].sum[3];

    tmp1=num;
    tree[rt].sum[1]=(sum1*tmp1)%mod;

    tmp2=(tmp1*num)%mod;
    tree[rt].sum[2]=(sum2*tmp2)%mod;

    tmp3=(tmp2*num)%mod;
    tree[rt].sum[3]=(sum3*tmp3)%mod;
}

void fun_same(int rt,int num)
{
    tree[rt].same=num;
    tree[rt].add=tree[rt].mul=0;

    len=tree[rt].ri-tree[rt].le+1;
    tmp1=num;
    tree[rt].sum[1]=(tmp1*len)%mod;

    tmp2=(tmp1*num)%mod;
    tree[rt].sum[2]=(tmp2*len)%mod;

    tmp3=(tmp2*num)%mod;
    tree[rt].sum[3]=(tmp3*len)%mod;
}

void Pushdown(int rt)
{
    if(tree[rt].same)
    {
        fun_same(LL(rt),tree[rt].same);
        fun_same(RR(rt),tree[rt].same);
        tree[rt].same=0;
    }
    if(tree[rt].mul)
    {
        fun_mul(LL(rt),tree[rt].mul);
        fun_mul(RR(rt),tree[rt].mul);
        tree[rt].mul=0;
    }
    if(tree[rt].add)
    {
        fun_add(LL(rt),tree[rt].add);
        fun_add(RR(rt),tree[rt].add);
        tree[rt].add=0;
    }
}

void Pushup(int rt)
{
    tree[rt].sum[1]=(tree[LL(rt)].sum[1]+tree[RR(rt)].sum[1])%mod;
    tree[rt].sum[2]=(tree[LL(rt)].sum[2]+tree[RR(rt)].sum[2])%mod;
    tree[rt].sum[3]=(tree[LL(rt)].sum[3]+tree[RR(rt)].sum[3])%mod;
}

void Update(int rt,int le,int ri,int num,int op)
{
    if(le<=tree[rt].le&&tree[rt].ri<=ri)
    {
        if(op==1)
            fun_add(rt,num);
        else if(op==2)
            fun_mul(rt,num);
        else
            fun_same(rt,num);
        return ;
    }
    Pushdown(rt);
    int mid=tree[rt].mid();
    if(le<=mid)
        Update(LL(rt),le,ri,num,op);
    if(ri>mid)
        Update(RR(rt),le,ri,num,op);
    Pushup(rt);
}

int Query(int rt,int le,int ri,int p)
{
    if(le<=tree[rt].le&&tree[rt].ri<=ri)
        return tree[rt].sum[p];
    Pushdown(rt);
    int mid=tree[rt].mid(),ans=0;
    if(le<=mid)
        ans=(ans+Query(LL(rt),le,ri,p))%mod;
    if(ri>mid)
        ans=(ans+Query(RR(rt),le,ri,p))%mod;
    Pushup(rt);
    return ans;
}

int main()
{
    while(scanf("%d%d",&n,&m),n||m)
    {
        Build(1,1,n);
        int op,x,y,c;
        while(m--)
        {
            scanf("%d%d%d%d",&op,&x,&y,&c);
            if(op<=3)
                Update(1,x,y,c,op);
            else
                printf("%d\n",Query(1,x,y,c));
        }
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值