本文主要是介绍【CodeForces】266E More Queries to Array... 线段树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
You've got an array, consisting of n integers: a1, a2, ..., an. Your task is to quickly run the queries of two types:
- Assign value x to all elements froml to r inclusive. After such query the values of the elements of arrayal, al + 1, ..., ar become equal tox.
- Calculate and print sum , wherek doesn't exceed 5. As the value of the sum can be rather large, you should print it modulo1000000007 (109 + 7).
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:
- The assign query has the following format: "", (1 ≤ l ≤ r ≤ n; 0 ≤ x ≤ 109).
- The query to calculate the sum has the following format: "", (1 ≤ l ≤ r ≤ n; 0 ≤ k ≤ 5).
All numbers in the input are integers.
For each query to calculate the sum print an integer — the required sum modulo1000000007 (109 + 7).
4 5
5 10 2 1
? 1 2 1
= 2 2 0
? 2 4 3
= 1 4 1
? 1 4 5
25
43
1300
3 1
1000000000 1000000000 1000000000
? 1 3 0
999999986
传送门:【CodeForces】266E More Queries to Array...
题目大意:
给你一串数,编号从1~n,进行m次操作。(1 <= n , m <= 10 ^ 5 )
= L R X 区间【L,R】上的数设为X
?L R K 输出 。
题目分析:
本题主要还是数学拆项的思想。
一开始我们直接将每个数设成ai * (i ^ k)。这样条件k下整个序列的和就是sum (ai * ( i ^ k ) ) ,我们需要分别保存每个k对应的和。
接下来,如果我们要求区间【L,R】上条件k下的和。
那么答案ans = sum ( ai * ( ( i - l + 1 ) ^ k ) ) (l <= i <= r )。这样我们就需要将其转化。
对于sum中的一项 ai * ( ( i - L + 1 ) ^ k ),我们需要转化成ai * ( i ^ k )。
我们可以进行二项式拆项:
ai * ( ( i - L + 1 ) ^ k ) =
ai * ( C( k , 0 ) * ( ( 1 - L ) ^ k ) ) * ( i ^ 0 ) +
C( k , 1 ) * ( ( 1 - L ) ^ ( k - 1 ) ) * ( i ^ 1 ) + ... + C( k , k ) * ( ( 1 - L ) ^ 0 ) * ( i ^ k ) 。
那么我们只要预处理出组合数C,以及1^k + 2^k + ... + n^k 的前缀和即可。
具体部分看代码就行了。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;typedef long long BigInt ;#define ls ( o << 1 )
#define rs ( o << 1 | 1 )
#define rt o , l , r
#define root 1 , 1 , n
#define lson ls , l , m
#define rson rs , m + 1 , r
#define mid ( ( l + r ) >> 1 )
#define clear( A , X ) memset ( A , X , sizeof A )const int maxK = 6 ;
const int maxN = 100005 ;
const int MOD = 1e9 + 7 ;BigInt sum[maxN << 2][maxK] , S[maxN][maxK] ;
int set[maxN << 2] , C[maxK][maxK] ;BigInt Pow ( int x , int n ) {BigInt a = 1 , b = x ;while ( n ) {if ( n & 1 ) a = a * b % MOD ;b = b * b % MOD ;n >>= 1 ;}return a ;
}void Ready () {for ( int i = 0 ; i < maxK ; ++ i ) {C[i][0] = 1 ;for ( int j = 1 ; j <= i ; ++ j ) {C[i][j] = C[i - 1][j - 1] + C[i - 1][j] ;}}for ( int i = 0 ; i < maxN ; ++ i ) {S[i][0] = 0 ;for ( int j = 0 ; j < maxK ; ++ j ) {S[i][j] = ( S[i - 1][j] + Pow ( i , j ) ) % MOD ;}}
}void Cal ( int v , int o , int l , int r ) {set[o] = v ;for ( int i = 0 ; i < maxK ; ++ i ) {sum[o][i] = ( S[r][i] - S[l - 1][i] + MOD ) % MOD * v % MOD ;}
}void PushUp ( int o ) {for ( int i = 0 ; i < maxK ; ++ i ) {sum[o][i] = ( sum[ls][i] + sum[rs][i] ) % MOD ;}
}void PushDown ( int o , int l , int r ) {if ( ~set[o] ) {int m = mid ;Cal ( set[o] , lson ) ;Cal ( set[o] , rson ) ;set[o] = -1 ;}
}void Update ( int L , int R , int v , int o , int l , int r ) {if ( L <= l && r <= R ) {Cal ( v , rt ) ;return ;}PushDown ( rt ) ;int m = mid ;if ( L <= m ) Update ( L , R , v , lson ) ;if ( m < R ) Update ( L , R , v , rson ) ;PushUp ( o ) ;
}BigInt Query ( int L , int R , int k , int o , int l , int r ) {if ( L <= l && r <= R ) {BigInt res = 0 , tmp = 1 ;for ( int i = k ; i >= 0 ; -- i ) {res = ( res + sum[o][i] * C[k][i] % MOD * tmp % MOD + MOD ) % MOD ;tmp = tmp * ( 1 - L ) % MOD ;}return res ;}PushDown ( rt ) ;int m = mid ;BigInt ans = 0 ;if ( L <= m ) ans = ( ans + Query ( L , R , k , lson ) ) % MOD ;if ( m < R ) ans = ( ans + Query ( L , R , k , rson ) ) % MOD ;return ans ;
}void Build ( int o , int l , int r ) {set[o] = -1 ;if ( l == r ) {int x ;scanf ( "%d", &x ) ;for ( int i = 0 ; i < maxK ; ++ i ) sum[o][i] = Pow ( l , i ) * x % MOD ;return ;}int m = mid ;Build ( lson ) ;Build ( rson ) ;PushUp ( o ) ;
}void work () {int n , m , l , r , v ;char ch ;Ready () ;while ( ~scanf ( "%d%d" , &n , &m ) ) {Build ( root ) ;while ( m -- ) {getchar () ;scanf ( "%c%d%d%d" , &ch , &l , &r , &v ) ;if ( '=' == ch ) Update ( l , r , v , root ) ;if ( '?' == ch ) printf ( "%I64d\n" , Query ( l , r , v , root ) ) ;}}
}int main () {work () ;return 0 ;
}
这篇关于【CodeForces】266E More Queries to Array... 线段树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!