hdu 4578 线段树lazy标记

来源:互联网 发布:ubuntu安装到u盘上 编辑:程序博客网 时间:2024/05/29 13:21

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, 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+…+ayp.
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 53 3 5 71 2 4 44 1 5 22 2 5 84 3 5 30 0

Sample Output
3077489

Source
2013ACM-ICPC杭州赛区全国邀请赛  

题意:

给一个序列 {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 10007struct 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;}



0 0