线段树Codeforces Round #163 (Div. 2)E

E. More Queries to Array...
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You've got an array, consisting of n integers: a1, a2, ..., an. Your task is to quickly run the queries of two types:

  1. Assign value x to all elements from l to r inclusive. After such query the values of the elements of array al, al + 1, ..., ar become equal to x.
  2. Calculate and print sum , where k doesn't exceed 5. As the value of the sum can be rather large, you should print it modulo 1000000007 (109 + 7).
Input

The first line contains two integers n and m (1 ≤ n, m ≤ 105), showing, how many numbers are in the array and the number of queries, correspondingly. The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 109) — the initial values of the array elements.

Then m queries follow, one per line:

  1. The assign query has the following format: "", (1 ≤ l ≤ r ≤ n; 0 ≤ x ≤ 109).
  2. The query to calculate the sum has the following format: "", (1 ≤ l ≤ r ≤ n; 0 ≤ k ≤ 5).

All numbers in the input are integers.

Output

For each query to calculate the sum print an integer — the required sum modulo 1000000007 (109 + 7).

Sample test(s)
input
4 5
5 10 2 1
? 1 2 1
= 2 2 0
? 2 4 3
= 1 4 1
? 1 4 5
output
25
43
1300
input
3 1
1000000000 1000000000 1000000000
? 1 3 0
output
999999986


题意:有两种操作,一种是把区间内的所有的数都变成x,另一种是计算区间内的 

思路:ai *(i-L+1)^k  =  [ i+(1-L)  ] ^ k  = C(k,0) * i^k + C(k,1) *   i ^ (k-1) * (1-L)......,所以线段树维护ai*(i^j)就可以了,每次进行操作一的时候,更行区间和变成x*[l^k+(l+1)^k+(l+2)^k+...+r^k]其中l到r的k次方的和可以预处理出来(即程序中的s数组),查询的时候求区间和就行了,这样维护就保证了区间的数变化的无差异

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=100010;
const int MOD=1000000007 ;

int n,m;
int a[maxn];
LL c[6][6],s[maxn][6],p[maxn][6];


struct IntervalTree
{
    LL sum[maxn<<3][6];
    int setv[maxn<<3];
    void build(int o,int l,int r)
    {
        memset(sum[o],0LL,sizeof(sum[o]));
        setv[o]=-1;
        if(l==r)
        {
            for(int i=0;i<6;i++)sum[o][i]=(a[l]*p[l][i])%MOD;
            return;
        }
        int mid=(l+r)>>1;
        build(o<<1,l,mid);
        build(o<<1|1,mid+1,r);
        pushup(o);
    }
    void pushup(int o)
    {
        for(int i=0;i<6;i++)
            sum[o][i]=(sum[o<<1][i]+sum[o<<1|1][i])%MOD;
    }

    void maintain(int o,int l,int r)
    {
        int x=setv[o];
        for(int i=0;i<6;i++)
            sum[o][i]=(x*(s[r][i]-s[l-1][i])%MOD+MOD)%MOD;
    }

    void pushdown(int o,int l,int r)
    {
        if(setv[o]!=-1)
        {
            setv[o<<1]=setv[o<<1|1]=setv[o];
            setv[o]=-1;
            int mid=(l+r)>>1;
            maintain(o<<1,l,mid);
            maintain(o<<1|1,mid+1,r);
        }
    }

    void update(int o,int l,int r,int q1,int q2,int x)
    {
        if(q1<=l&&r<=q2)
        {
            setv[o]=x;
            maintain(o,l,r);
            return;
        }
        pushdown(o,l,r);
        int mid=(l+r)>>1;
        if(q1<=mid)update(o<<1,l,mid,q1,q2,x);
        if(q2>mid)update(o<<1|1,mid+1,r,q1,q2,x);
        pushup(o);
    }
    LL query(int o,int l,int r,int q1,int q2,int x)
    {
        if(q1<=l&&r<=q2)return sum[o][x];
        pushdown(o,l,r);
        int mid=(l+r)>>1;
        LL ans=0;
        if(q1<=mid)ans+=query(o<<1,l,mid,q1,q2,x);
        if(q2>mid)ans+=query(o<<1|1,mid+1,r,q1,q2,x);
        return ans;
    }

}tree;


LL POW(int x,int y)
{
    LL ans=1;
    for(int i=1;i<=y;i++)ans=ans*x%MOD;
    return ans;
}

void init()
{
    memset(c,0,sizeof(c));
    for(int i=0;i<6;i++)c[i][0]=1;
    for(int i=1;i<6;i++)
        for(int j=1;j<=i;j++)c[i][j]=(c[i-1][j]+c[i-1][j-1])%MOD;
    for(int i=1;i<maxn;i++)
        for(int j=0;j<6;j++)
            p[i][j]=POW(i,j);
    for(int i=1;i<maxn;i++)
        for(int j=0;j<6;j++)
            s[i][j]=(s[i-1][j]+p[i][j])%MOD;
}

int main()
{
    init();
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    char op[5];
    tree.build(1,1,n);
    while(m--)
    {
        int l,r,x;
        scanf("%s%d%d%d",op,&l,&r,&x);
        if(op[0]=='=')tree.update(1,1,n,l,r,x);
        else
        {
            LL ans=0;
            LL tmp=1;
            for(int i=0;i<=x;i++)
            {
                ans=(ans+tree.query(1,1,n,l,r,x-i)*c[x][i]%MOD*tmp%MOD)%MOD;
                tmp=tmp*(1-l)%MOD;
            }
            cout<<(ans+MOD)%MOD<<endl;
        }
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值