快乐补题-SDNU_ACM_ICPC_2020_Winter_Practice_2st——E

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

分析

沙雕线段树orz…和edu的线段树特别像,直接般的大佬的题解

AC代码

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define left k*2
#define right k*2+1
const int N=10007;
struct node
{
    int l,r;
    int tag1,tag2,tag3;//分别表示加号,乘号,等号标记
}t[100005*4];
int n;
void Build(int l,int r,int k)
{
    t[k].l=l;
    t[k].r=r;
    t[k].tag1=0;
    t[k].tag2=1;
    t[k].tag3=-1;
    if(l==r)
    {
        t[k].tag3=0;//最底层要赋值为0
        return;
    };
    int mid=(l+r)/2;
    Build(l,mid,left);
    Build(mid+1,r,right);
}
void pushdown(int k)
{
    if(t[k].l==t[k].r)//没有子区间了不用退了
        return;
    if(t[k].tag3!=-1)//处理等号
    {
        t[left].tag3=t[right].tag3=t[k].tag3;//更新子区间等号标记
        t[left].tag2=t[right].tag2=1;
        t[left].tag1=t[right].tag1=0;//清空子区间加乘标记
        t[k].tag3=-1;
        return;
    }
    if(t[k].tag2!=1)//处理乘号
    {
        if(t[left].tag3!=-1)//如果子区间有等号标记,直接修改等号标记
            t[left].tag3=(t[left].tag3*t[k].tag2)%N;
        else//否则清空该子区间标记,进行子区间标记
        {
            pushdown(left);
            t[left].tag2=(t[left].tag2*t[k].tag2)%N;
        }
        if(t[right].tag3!=-1)//同理处理右区间
            t[right].tag3=(t[right].tag3*t[k].tag2)%N;
        else
        {
            pushdown(right);
            t[right].tag2=(t[right].tag2*t[k].tag2)%N;
        }
        t[k].tag2=1;
    }
    if(t[k].tag1!=0)//处理加号标记,和上面同理处理
    {
        if(t[left].tag3!=-1)
            t[left].tag3=(t[left].tag3+t[k].tag1)%N;
        else
        {
            pushdown(left);
            t[left].tag1=(t[left].tag1+t[k].tag1)%N;
        }
        if(t[right].tag3!=-1)
            t[right].tag3=(t[right].tag3+t[k].tag1)%N;
        else
        {
            pushdown(right);
            t[right].tag1=(t[right].tag1+t[k].tag1)%N;
        }
        t[k].tag1=0;//记得还原
    }
}
void update(int l,int r,int v,int d,int k)
{
    if(t[k].l==l&&t[k].r==r)
    {
        if(d==1)
        {
            if(t[k].tag3!=-1)//如果有等号标记,就直接修改等号标记
            {
                t[k].tag3=(t[k].tag3+v%N)%N;
            }
            else
            {
                pushdown(k);//否则清空该区间,进行标记
                t[k].tag1=(t[k].tag1+v%N)%N;
            }
        }
        else if(d==2)//同理
        {
            if(t[k].tag3!=-1)
            {
                t[k].tag3=(t[k].tag3*v%N)%N;
            }
            else
            {
                pushdown(k);
                t[k].tag2=(t[k].tag2*v%N)%N;
            }
        }
        else
        {
            t[k].tag3=v%N;
            t[k].tag1=0;
            t[k].tag2=1;
        }
        return;
    }
    pushdown(k);//向下更新
    int mid=(t[k].l+t[k].r)/2;
    if(r<=mid)
        update(l,r,v,d,left);
    else if(l>mid)
        update(l,r,v,d,right);
    else
    {
        update(l,mid,v,d,left);
        update(mid+1,r,v,d,right);
    }
}
int query(int l,int r,int p,int k)//查询
{
    if(t[k].l>=l&&t[k].r<=r&&t[k].tag3!=-1)//查到是查询区间的子区间且一段全为相同的数
    {
        int temp=1;
        for(int i=1;i<=p;i++)
            temp=(temp*t[k].tag3)%N;
        return ((t[k].r-t[k].l+1)%N*temp)%N;//注意要乘上长度
    }
    pushdown(k);
    int mid=(t[k].l+t[k].r)/2;
    if(r<=mid)
        return query(l,r,p,left)%N;
    else if(l>mid)
        return query(l,r,p,right)%N;
    else
    {
        return (query(l,mid,p,left)+query(mid+1,r,p,right))%N;
    }
}
int main()
{
    int i,j,k,m,d,x,y,c;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)
            break;
        Build(1,n,1);
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d%d",&d,&x,&y,&c);
            if(d>=1&&d<=3)
            {
                update(x,y,c,d,1);
            }
            else
            {
                printf("%d\n",query(x,y,c,1)%N);
            }
        }
    }
    return 0;
}
————————————————
版权声明:本文为CSDN博主「i-unique」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_37497322/article/details/76355061

你可能感兴趣的:(快乐补题-SDNU_ACM_ICPC_2020_Winter_Practice_2st——E)