HDU 4578(线段树区间更新)

Transformation

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


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+…+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
 

//只有一个字 艹
//每个节点存是纯色的val 
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn=100000+10;
struct Node
{
    __int64 l,r,col;  //纯色 0 不纯为1
    __int64 val;
    int mid()
    {
        return (l+r)/2;
    }
}tree[maxn*6];
__int64 Mod=10007;
__int64 n,m,ans=0;
void Buildtree(__int64 rt,__int64 l,__int64 r)
{
    tree[rt].l=l;
    tree[rt].r=r;
    tree[rt].val=tree[rt].col=0;
    if(l!=r)
    {
        Buildtree(2*rt,l,tree[rt].mid());
        Buildtree(2*rt+1,tree[rt].mid()+1,r);
    }
}
void Pushdown(__int64 rt)
{
        tree[2*rt].val=tree[2*rt+1].val=tree[rt].val;
        tree[2*rt].col=tree[2*rt+1].col=0;
}
void Update(__int64 rt,__int64 l,__int64 r,__int64 type,__int64 val)
{
    if(tree[rt].l==l&&tree[rt].r==r&&tree[rt].col==0)
    {
        if(type==1)
            tree[rt].val=(tree[rt].val+val)%10007;  //这里让我错了N多发 fuck...
        else if(type==2)
            tree[rt].val=(tree[rt].val*val)%10007;
        else
            tree[rt].val=val%10007;
        tree[rt].col=0;
        return;
    }
    if(tree[rt].col==0)
        Pushdown(rt);
    if(r<=tree[rt].mid())
        Update(2*rt,l,r,type,val);
    else if(l>tree[rt].mid())
        Update(2*rt+1,l,r,type,val);
    else
    {
        Update(2*rt,l,tree[rt].mid(),type,val);
        Update(2*rt+1,tree[rt].mid()+1,r,type,val);
    }
    tree[rt].col=(tree[rt*2].col||tree[2*rt+1].col||tree[2*rt].val!=tree[2*rt+1].val);  //回溯
    if(tree[2*rt].col==0&&tree[rt*2+1].col==0&&tree[2*rt].val==tree[rt*2+1].val)
    {
        tree[rt].val=tree[2*rt].val;
        tree[rt].col=0;
    }
}
void Query(__int64 rt,__int64 l,__int64 r,__int64 val)
{
    __int64 t=tree[rt].val;
    if(tree[rt].l==l&&tree[rt].r==r&&tree[rt].col==0)
    {
        if(val==1)
            ans=(ans+((tree[rt].r-tree[rt].l+1)*t))%10007;
        else if(val==2)
            ans=(ans+((tree[rt].r-tree[rt].l+1)*t%10007*t))%10007;
        else if(val==3)
            ans=(ans+((tree[rt].r-tree[rt].l+1)*t%10007*t%10007*t%10007))%10007;
        return;
    }
    if(tree[rt].col==0)
        Pushdown(rt);
    if(r<=tree[rt].mid())
        Query(2*rt,l,r,val);
    else if(l>tree[rt].mid())
        Query(2*rt+1,l,r,val);
    else
    {
        Query(2*rt,l,tree[rt].mid(),val);
        Query(2*rt+1,tree[rt].mid()+1,r,val);
    }
}
int main()
{
    while(~scanf("%I64d%I64d",&n,&m)&&(n+m))
    {
        Buildtree(1,1,n);
        while(m--)
        {
            __int64 type,l,r,val;
            scanf("%I64d%I64d%I64d%I64d",&type,&l,&r,&val);
            if(type<=3)
                Update(1,l,r,type,val);
            else
            {
                ans=0;
                Query(1,l,r,val);
                printf("%I64d\n",ans%10007);
            }
        }
    }
    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值