`
Coco_young
  • 浏览: 125319 次
  • 性别: Icon_minigender_1
  • 来自: 湖南长沙
社区版块
存档分类
最新评论

Codeforces #163 Div2 E

 
阅读更多

传送门:http://codeforces.com/contest/266/problem/E



代码:

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid(x,y) (x+y)>>1
#define mod 1000000007
typedef long long LL;
const int MAXN = 100010;
const int MAXK = 7;
LL val[MAXN<<2][MAXK],flag[MAXN<<2],Sum[MAXN][MAXK],Comb[MAXK][MAXK],A[MAXN];
LL pow_mod(LL a,LL p){
    if(p==0)return 1;
    LL b = pow_mod(a,p/2);
    if(p%2){
        return (((b*b)%mod)*a)%mod;
    }else{
        return (b*b)%mod;
    }
}
LL getComb(LL m,LL n){
    LL up=1,down=1;
    for(int i=0;i<n;i++){
        up = up*(m-i);
        down = down*(i+1);
    }
    return up/down;
}
void preprocess(){
    for(int i=1;i<MAXN;i++){
        for(int j=0;j<MAXK;j++){
            Sum[i][j] = (Sum[i-1][j]+pow_mod(i,j))%mod;
        }
    }
    for(int i=0;i<MAXK;i++){
        for(int j=0;j<=i;j++){
            Comb[i][j] = getComb(i,j)%mod;
        }
    }
}
void PushUp(int rt){
    for(int i=0;i<MAXK;i++){
        val[rt][i] = (val[rt<<1][i]+val[rt<<1|1][i])%mod;
    }
}
void PushDown(int l,int r,int rt){
    if(flag[rt]==-1)return;
    if(l==r){
        flag[rt] = -1;
        return;
    }
    int m = mid(l,r);
    for(int i=0;i<MAXK;i++){
        LL tmp = (Sum[m][i]-Sum[l-1][i])%mod;
        if(tmp<0)tmp+=mod;
        val[rt<<1][i] = (tmp*flag[rt])%mod;
        tmp = (Sum[r][i]-Sum[m][i])%mod;
        if(tmp<0)tmp+=mod;
        val[rt<<1|1][i] = (tmp*flag[rt])%mod;
    }
    flag[rt<<1] = flag[rt<<1|1] = flag[rt];
    flag[rt] = -1;
}
void build(int l,int r,int rt){
    flag[rt] = -1;
    if(l==r){
        for(int i=0;i<MAXK;i++){
            val[rt][i] = (A[l]*pow_mod(l,i))%mod;
        }
        return;
    }
    int m = mid(l,r);
    build(lson);
    build(rson);
    PushUp(rt);
}
void update(int L,int R,int v,int l,int r,int rt){
    if(l>=L&&r<=R){
        flag[rt] = v;
        for(int i=0;i<MAXK;i++){
            LL tmp = (Sum[r][i]-Sum[l-1][i])%mod;
            if(tmp<0)tmp+=mod;
            val[rt][i] = (tmp*flag[rt])%mod;
        }
        return;
    }
    PushDown(l,r,rt);
    int m = mid(l,r);
    if(m>=L)update(L,R,v,lson);
    if(m<R)update(L,R,v,rson);
    PushUp(rt);
}
LL query(int L,int R,int k,int l,int r,int rt){
    if(l>=L&&r<=R){
        return val[rt][k];
    }
    PushDown(l,r,rt);
    int m = mid(l,r);
    LL ret = 0;
    if(m>=L) ret = (ret+query(L,R,k,lson))%mod;
    if(m<R) ret = (ret+query(L,R,k,rson))%mod;
    return ret;
}
int main(){
    int n,m;
    char op;
    int l,r;
    LL x;
    preprocess();
    while(~scanf("%d%d",&n,&m)){
        for(int i=1;i<=n;i++){
            scanf("%I64d",&A[i]);
        }
        build(1,n,1);
        while(m--){
            getchar();
            scanf("%c%d%d%I64d",&op,&l,&r,&x);
            if(op=='=')update(l,r,x,1,n,1);
            else{
                LL res = 0,tmp = 1;
                for(int i=x;i>=0;i--){
                    res += ((((query(l,r,i,1,n,1))*Comb[x][i])%mod)*tmp)%mod;
                    res = (res+mod)%mod;//1-l可能为负数,所以此操作能保证res每一步的正确性
                    tmp = (tmp*(1-l))%mod;
                }
                printf("%I64d\n",res);
            }
        }
    }
    return 0;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics