HDU4578 Transformation

HDU4578 Transformation

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

一.解题思路及要点

  1. 题目大意:
    给你一个初始全0的数组。有四种操作:
    1.[L,R]范围内的数全部+c
    2.[L,R]范围内的数全部*c
    3.[L,R]范围内的数全部变为c
    4.输出[L,R]范围内的数的k次方和(1 <= k <= 3 )

  2. 区间和,平方和,立方和的维护 (开三个数组)

    我们可以把区间内的数字看做 ax+b 的形式。
    x是原来的值,a是当前区间乘上的值, b是当前区间要加上的值。
    因为P只有1到3,所以我们可以开3个数组来保存每个次方的和,分别是sum1[maxn* 4],sum2[maxn* 4], sum3[maxn* 4]

    对于加c操作,则变成 ax+b+c(b−>b+c)。
    对于乘c操作,则变成 acx+bc(a−>ac,b−>bc)
    对于赋值c操作,则变成c,即(a−>1,x−>c,b−>0)

    这里要解决的一个问题是加上或乘上一个数对这个区间的p次方和分别产生什么改变

    乘上一个数a之后
    sum1[k]=a*sum1[k]

    sum2[k]=a* a *sum1[k]

    sum3[k]=a* a* sum2[k]

    当加上一个数b之后
    sum1[k]=sum1[k]+len∗b
    sum2[k]=sum2[k]+2∗sum1[k]∗b+len* b* b
    sum[3]=sum[3]+3∗b2∗sum[1]+3∗b* sum[2]+len* b * b *b
    其中len是当前区间的长度。(注意左子节点为mid-l+1,而右子节点为r-mid,两者不同)

  3. 注意合适的位置进行取模操作

二.代码

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define maxn 500000
#define mod 10007
#define ll long long

int sum1[maxn],sum2[maxn],sum3[maxn];//分别维护区间和,区间平方和,区间立方和
int add[maxn],mul[maxn],t[maxn];//分别标记加法,乘法,赋值

void PushUp(int k)
{
    sum1[k]=(sum1[k<<1]+sum1[k<<1|1])%mod;
    sum2[k]=(sum2[k<<1]+sum2[k<<1|1])%mod;
    sum3[k]=(sum3[k<<1]+sum3[k<<1|1])%mod;
}

void PushDown(int k,int l,int r)
{
    int mid=(l+r)>>1;
    if(t[k])//优先维护是否有赋值操作
    {
        t[k<<1]=t[k<<1|1]=t[k];
        add[k<<1]=add[k<<1|1]=0;
        mul[k<<1]=mul[k<<1|1]=1;

        sum1[k<<1]=(mid-l+1)*t[k]%mod;
        sum2[k<<1]=sum1[k<<1]*t[k]%mod;
        sum3[k<<1]=sum2[k<<1]*t[k]%mod;
        sum1[k<<1|1]=(r-mid)*t[k]%mod;//r-mid而不是r-mid+1
        sum2[k<<1|1]=sum1[k<<1|1]*t[k]%mod;
        sum3[k<<1|1]=sum2[k<<1|1]*t[k]%mod;
        t[k]=0;
    }
    if(add[k]!=0||mul[k]!=1)//再维护是否有加法和乘法操作
    {
        add[k<<1]=(add[k<<1]*mul[k]+add[k])%mod;
        mul[k<<1]=mul[k<<1]*mul[k]%mod;
        add[k<<1|1]=(add[k<<1|1]*mul[k]+add[k])%mod;
        mul[k<<1|1]=mul[k<<1|1]*mul[k]%mod;

        int _sum1 , _sum2 , _sum3 ;//注意此处必须要有三个中间值,如果直接求值则之前的值将会被覆盖导致新求的值错误

        _sum1=(sum1[k<<1]*mul[k]%mod+add[k]*(mid-l+1)%mod)%mod;
        _sum2=(mul[k]*mul[k]%mod*sum2[k<<1]%mod
               +2*add[k]%mod*mul[k]%mod*sum1[k<<1]%mod
               +add[k]*add[k]%mod*(mid-l+1)%mod)%mod;
        _sum3=(mul[k]*mul[k]%mod*mul[k]%mod*sum3[k<<1]%mod
               +3*mul[k]%mod*mul[k]%mod*add[k]%mod*sum2[k<<1]%mod
               +3*mul[k]%mod*add[k]%mod*add[k]%mod*sum1[k<<1]%mod
               +add[k]*add[k]%mod*add[k]%mod*(mid-l+1)%mod)%mod;

        sum1[k<<1] = _sum1 ;
		sum2[k<<1] = _sum2 ;
		sum3[k<<1] = _sum3 ;
        _sum1=(sum1[k<<1|1]*mul[k]%mod+add[k]*(r-mid)%mod)%mod;
        _sum2=(mul[k]*mul[k]%mod*sum2[k<<1|1]%mod+2*add[k]%mod*mul[k]%mod*sum1[k<<1|1]%mod+add[k]*add[k]%mod*(r-mid)%mod)%mod;
        _sum3=(mul[k]*mul[k]%mod*mul[k]%mod*sum3[k<<1|1]%mod+3*mul[k]%mod*mul[k]%mod*add[k]%mod*sum2[k<<1|1]%mod+3*mul[k]%mod*add[k]%mod*add[k]%mod*sum1[k<<1|1]%mod+add[k]*add[k]%mod*add[k]%mod*(r-mid)%mod)%mod;
        
        sum1[k<<1|1] = _sum1 ;
		sum2[k<<1|1] = _sum2 ;
		sum3[k<<1|1] = _sum3 ;

        add[k]=0;
        mul[k]=1;
    }
}
void build(int l,int r,int k)
{
    sum1[k]=sum2[k]=sum3[k]=0;
    add[k]=t[k]=0;
    mul[k]=1;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(l,mid,k<<1);
    build(mid+1,r,k<<1|1);
}

void upd(int opt,int c,int L,int R,int l,int r,int k)
{
    if(L<=l&&r<=R)
    {
        int len=(r-l+1);
        if(opt==1)
        {
            add[k]=(add[k]+c)%mod;
            int _sum1 , _sum2 , _sum3 ;
            _sum1=(sum1[k]+c*len)%mod;
            _sum2=(sum2[k]+2*sum1[k]%mod*c%mod+c*c%mod*len%mod)%mod;
            _sum3=(sum3[k]+3*sum1[k]%mod*c%mod*c%mod+3*c%mod*sum2[k]%mod+c*c%mod*c%mod*len%mod)%mod;
            
            sum1[k] = _sum1 ;
		    sum2[k] = _sum2 ;
		    sum3[k] = _sum3 ;
        
        }
        if(opt==2)
        {
            mul[k]=(mul[k]*c)%mod;
            add[k]=add[k]*c%mod;
            sum1[k]=sum1[k]*c%mod;
            sum2[k]=sum2[k]*c%mod*c%mod;
            sum3[k]=sum3[k]*c%mod*c%mod*c%mod;
        }
        if(opt==3)
        {
            t[k]=c;
            add[k]=0;
            mul[k]=1;
            sum1[k]=c*len%mod;
            sum2[k]=c*sum1[k]%mod;
            sum3[k]=c*sum2[k]%mod;
        }
        return;
    }
    PushDown(k,l,r);
    int mid=(l+r)>>1;
    if(L<=mid)upd(opt,c,L,R,l,mid,k<<1);
    if(R>mid)upd(opt,c,L,R,mid+1,r,k<<1|1);
    PushUp(k);
}

int query(int c,int L,int R,int l,int r,int k)
{
    if(L<=l&&r<=R)
    {
        if(c==1)return sum1[k];
        if(c==2)return sum2[k];
        if(c==3)return sum3[k];
    }
    PushDown(k,l,r);
    int ans=0;
    int mid=(l+r)>>1;
    if(L<=mid)ans=(ans+query(c,L,R,l,mid,k<<1))%mod;
    if(R>mid)ans=(ans+query(c,L,R,mid+1,r,k<<1|1))%mod;
    return ans;
}
int main()
{
    int n,m,opt;
    while(~scanf("%d%d",&n,&m)&&(n||m))
    {
        build(1,n,1);//
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&opt);
            int x,y,c;
            scanf("%d%d%d",&x,&y,&c);
            if(opt==4)
            {
                printf("%d\n",query(c,x,y,1,n,1));
            }
            else
            {
                upd(opt,c,x,y,1,n,1);
            }
            
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值