Transformation HDU - 4578 (线段树,审题很重要)

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. 


#include

using namespace std;
#define lson i<<1,l,m
#define rson i<<1|1, m+1,r
const int mod = 10007;
const int maxn=1e5+10;

int x[maxn<<2],flag[maxn<<2];

void pushup(int i,int l,int r)
{
    if(!flag[i<<1] || !flag[i<<1|1])
        flag[i] = 0;
    else if(x[i<<1] != x[i<<1|1])
        flag[i] = 0;
    else flag[i]=1,x[i]=x[i<<1];
}

void pushdown(int i,int l,int r)
{
    if(flag[i])
    {
        flag[i<<1] = flag[i<<1|1] =1;
        x[i<<1] = x[i<<1|1] = x[i];
        flag[i]=0;
    }
}

void update(int ql,int qr,int p,int v,int i,int l,int r)
{
    if(ql<=l && qr>=r && flag[i])
    {
        if(p==1)
            x[i] = (x[i]+v)%mod;
        else if(p==2)
            x[i] = (x[i]*v)%mod;
        else x[i] = v;
        return;
    }
    pushdown(i,l,r);

    int m = (l+r)>>1;
    if(ql<=m) update(ql,qr,p,v,lson);
    if(qr>m) update(ql,qr,p,v,rson);
    pushup(i,l,r);
}
int query(int ql,int qr,int num,int i,int l,int r)
{
    if(flag[i] && ql<=l && qr>=r)
    {
        int ans=1;
        for(int j=0;j>1;
    int ans=0;
    if(ql<=m)ans+=query(ql,qr,num,lson);
    if(qr>m)ans+=query(ql,qr,num,rson);
    return ans%mod;
}

int main()
{
    int n,m;
    while(cin>>n>>m,n||m)
    {
        memset(flag,1,sizeof flag);
        memset(x,0,sizeof x);
        int p,x,y,v;
        while(m--)
        {
            scanf("%d%d%d%d",&p,&x,&y,&v);
            if(p>=1 && p<=3)update(x,y,p,v,1,1,n);
            else printf("%d\n",query(x,y,v,1,1,n));
        }
    }
}

你可能感兴趣的:(数据结构----线段树)