hdu 4578 线段树lazy标记

Transformation

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


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

题意:

给一个序列 {an},有 4 种操作。

1、将一段区间的数全部加 c。

2、将一段区间的数全部乘 c。

3、将一段区间的数全部等于 c。

4、询问一段区间的和(和、平方和、立方和)。

add记录 加

mul记录 乘

第三种情况可以看成是乘0加c

sum[1]  记录一次方和

sum[2]  记录二次方和

sum[3]  记录三次方和

公式推导出:

sum[3]=(sum[3]+(sqr3(m)*len())%mod + (3*m*sum[2])%mod + (3*sqr2(m)*sum[1])%mod) %mod;

sum[2]=( (2*m*sum[1]%mod) + (sum[2]+sqr2(m)*len()%mod))%mod;

sum[1]= (sum[1]+m*len())%mod;


写的比较难看:

#include<stdio.h>
#define N 100005
#define mod 10007
struct node
{
    int x,y,mul,add,sum[4];
    int len()
    {
        return y-x+1;
    }
}a[N*4];
void build(int x,int y,int t)
{
    int i;
    a[t].x=x; a[t].y=y;
    a[t].mul=1; a[t].add=0;
    for(i=1;i<=3;i++)  a[t].sum[i]=0;
    if(x==y) return ;
    int mid=(x+y)>>1,temp=t<<1;
    build(x,mid,temp);
    build(mid+1,y,temp+1);
}
void fun(int t)
{
    int temp=t<<1,i;
    for(i=1;i<=3;i++)
        a[t].sum[i]=(a[temp].sum[i]+a[temp+1].sum[i])%mod;
}
void pushdown(int t)
{
  int temp=t<<1,i,p,c; struct node b;
  if(a[t].mul!=1||a[t].add!=0)
  {
    a[temp].add=( a[temp].add*a[t].mul%mod + a[t].add)%mod;
    a[temp].mul=( a[temp].mul*a[t].mul )%mod;
    p=c=a[t].mul;
    for(i=1;i<=3;i++)
    {
      a[temp].sum[i]=a[temp].sum[i]*p%mod;
      p=p*c%mod;
    }
    b=a[temp]; c=a[t].add;
    a[temp].sum[1]=( a[temp].sum[1]+a[temp].len()*c%mod )%mod;
    a[temp].sum[2]=( (a[temp].sum[2]+2*c*b.sum[1]%mod)%mod + a[temp].len()*(c*c%mod)%mod )%mod;
    a[temp].sum[3]=( (a[temp].sum[3]+3*b.sum[2]*c%mod )%mod + (3*b.sum[1]*(c*c%mod)%mod+a[temp].len()*(c*c%mod*c%mod))%mod )%mod;

    a[temp+1].add=( a[temp+1].add*a[t].mul%mod + a[t].add)%mod;
    a[temp+1].mul=( a[temp+1].mul*a[t].mul )%mod;
    p=c=a[t].mul;
    for(i=1;i<=3;i++)
    {
      a[temp+1].sum[i]=a[temp+1].sum[i]*p%mod;
      p=p*c%mod;
    }
    b=a[temp+1]; c=a[t].add;
    a[temp+1].sum[1]=( a[temp+1].sum[1]+a[temp+1].len()*c%mod )%mod;
    a[temp+1].sum[2]=( (a[temp+1].sum[2]+2*c*b.sum[1]%mod)%mod + a[temp+1].len()*(c*c%mod)%mod )%mod;
    a[temp+1].sum[3]=( (a[temp+1].sum[3]+3*b.sum[2]*c%mod )%mod + (3*b.sum[1]*(c*c%mod)%mod + a[temp+1].len()*(c*c%mod*c%mod)%mod)%mod )%mod;
    a[t].mul=1;//置为1
    a[t].add=0;//置为0,刚开始居然都忘写了
  }
}
void update(int t,int x,int y,int c,int op)
{
    struct node b=a[t]; int i,p;
    if(a[t].x==x&&a[t].y==y)
    {
        if(op==1)
        {
            a[t].add=(a[t].add+c)%mod;
            a[t].sum[1]=( a[t].sum[1]+a[t].len()*c%mod )%mod;
            a[t].sum[2]=( (a[t].sum[2]+2*c*b.sum[1]%mod)%mod + a[t].len()*(c*c%mod)%mod )%mod;
            a[t].sum[3]=( (a[t].sum[3]+3*b.sum[2]*c%mod)%mod + (3*b.sum[1]*(c*c%mod)%mod+a[t].len()*(c*c%mod*c%mod)%mod)%mod )%mod;
        }
        else if(op==2)
        {
            a[t].add=a[t].add*c%mod;
            a[t].mul=a[t].mul*c%mod;
            p=c;
            for(i=1;i<=3;i++)
            {
                a[t].sum[i]=a[t].sum[i]*p%mod;
                p=p*c%mod;
            }
        }
        else if(op==3)
        {
            a[t].add=c;
            a[t].mul=0;
            p=c;
            for(i=1;i<=3;i++)
            {
                a[t].sum[i]=a[t].len()*p%mod;
                p=p*c%mod;
            }
        }
        return;
    }
    pushdown(t);
    int mid=(a[t].x+a[t].y)>>1,temp=t<<1;
    if(y<=mid)
        update(temp,x,y,c,op);
    else if(x>mid)
        update(temp+1,x,y,c,op);
    else
    {
        update(temp,x,mid,c,op);
        update(temp+1,mid+1,y,c,op);
    }
    fun(t);
}
int query(int t,int x,int y,int p)
{
    int ans;
    if(a[t].x==x&&a[t].y==y)
    {
        return a[t].sum[p]%mod;
    }
    pushdown(t);
    int mid=(a[t].x+a[t].y)>>1,temp=t<<1;
    if(y<=mid)
        ans=query(temp,x,y,p);
    else if(x>mid)
        ans=query(temp+1,x,y,p);
    else
       ans=query(temp,x,mid,p)+query(temp+1,mid+1,y,p);
    fun(t);
    return ans%mod;//这忘了取余,又错了一次
}
int main()
{
    int n,m,x,y,op,p;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)
            break;
        build(1,n,1);
        while(m--)
        {
            scanf("%d%d%d%d",&op,&x,&y,&p);
            if(op==4)
            {
                  printf("%d\n",query(1,x,y,p));
            }
            else
            {
                update(1,x,y,p,op);
            }
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值