HDU4578-Transformation

Transformation

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


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
 


题意:一开始有n个为0的数,一共有四种操作:区间[l,r]内的数全部加c。区间[l,r]内的数全部乘c。区间[l,r]内的数全部初始为c。询问区间[l,r]内所有数的P次方之和。

解题思路:不可能全部查询的节点,最好的情况就是查询到一段[l,r],这段区间内所有的值都相等,那么返回(r-l+1)*val 的值即可。只要记录区间内的所有数是否相同,并记录下区间内所有数相同的区间的值即可。每次询问时查询到区间内所有值相同的区间即可


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

const int mod=10007;

int n,q,flag[400009],x[400009];

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

void update(int k,int l,int r,int ll,int rr,int val,int type)
{
    if(l>=ll&&r<=rr&&flag[k])
    {
        if(type==1) x[k]=(x[k]+val)%mod;
        else if(type==2) x[k]=(x[k]*val)%mod;
        else x[k]=val;
        return ;
    }
    if(flag[k])
    {
        flag[k<<1]=flag[k<<1|1]=1;
        x[k<<1]=x[k<<1|1]=x[k];
        flag[k]=0;
    }
    int mid=(l+r)>>1;
    if(ll<=mid) update(k<<1,l,mid,ll,rr,val,type);
    if(rr>mid) update(k<<1|1,mid+1,r,ll,rr,val,type);
    Union(k,k<<1,k<<1|1);
}

int query(int k,int l,int r,int ll,int rr,int val)
{
    if(flag[k]&&l>=ll&&r<=rr)
    {
        int ans=1;
        for(int i=1;i<=val;i++) ans=(ans*x[k])%mod;
        ans=(ans*(r-l+1))%mod;
        return ans;
    }
    if(flag[k])
    {
        flag[k<<1]=flag[k<<1|1]=1;
        flag[k]=0;
        x[k<<1]=x[k<<1|1]=x[k];
    }
    int mid=(l+r)>>1,ans=0;;
    if(mid>=ll) ans+=query(k<<1,l,mid,ll,rr,val);
    if(rr>mid) ans+=query(k<<1|1,mid+1,r,ll,rr,val);
    return ans%mod;
}

int main()
{
    while(~scanf("%d%d",&n,&q)&&(n+q))
    {
        memset(flag,1,sizeof flag);
        memset(x,0,sizeof x);
        int x,l,r,val;
        while(q--)
        {
            scanf("%d%d%d%d",&x,&l,&r,&val);
            if(x>=1&&x<=3) update(1,1,n,l,r,val,x);
            else printf("%d\n",query(1,1,n,l,r,val));
        }
    }
    return 0;
}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值