HDU 4578 Transformation 线段树
来源:互联网 发布:mini metro mac下载 编辑:程序博客网 时间:2024/05/10 01:18
Transformation
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 65535/65536 K (Java/Others)Total Submission(s): 2374 Accepted Submission(s): 558
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.
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.
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 53 3 5 71 2 4 44 1 5 22 2 5 84 3 5 30 0
Sample Output
3077489
Source
2013ACM-ICPC杭州赛区全国邀请赛
传送门:HDU 4578 Transformation
题目大意:
给你一个初始全0的数组。有四种操作:
1.[L,R]范围内的数全部+c
2.[L,R]范围内的数全部*c
3.[L,R]范围内的数全部变为c
4.输出[L,R]范围内的数的k次方和(1 <= k <= 3 )
题目分析:
就是在裸的线段树上稍作变化,注意lazy标记中的乘法标记和加法标记有结合律,然后拆项转换即可。
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std ;#define lson l , m , o << 1#define rson m + 1 , r , o << 1 | 1const int mod = 10007 ;const int maxN = 500000 ;int sum1[maxN] , sum2[maxN] , sum3[maxN] ;int add[maxN] , mul[maxN] , set[maxN] ;void PushUp ( int o ) {sum1[o] = ( sum1[o << 1] + sum1[o << 1 | 1] ) % mod ;sum2[o] = ( sum2[o << 1] + sum2[o << 1 | 1] ) % mod ;sum3[o] = ( sum3[o << 1] + sum3[o << 1 | 1] ) % mod ;}void PushDown ( int o , int l , int r ) {int m = ( l + r ) >> 1 ;if ( set[o] ) {set[o << 1] = set[o << 1 | 1] = set[o] ;add[o << 1] = add[o << 1 | 1] = 0 ;mul[o << 1] = mul[o << 1 | 1] = 1 ;sum1[o << 1] = ( m - l + 1 ) * set[o] % mod ;sum2[o << 1] = sum1[o << 1] * set[o] % mod ;sum3[o << 1] = sum2[o << 1] * set[o] % mod ;sum1[o << 1 | 1] = ( r - m ) * set[o] % mod ;sum2[o << 1 | 1] = sum1[o << 1 | 1] * set[o] % mod ;sum3[o << 1 | 1] = sum2[o << 1 | 1] * set[o] % mod ;set[o] = 0 ;}if ( add[o] || mul[o] != 1 ) {add[o << 1] = ( add[o << 1] * mul[o] + add[o] ) % mod ;mul[o << 1] = mul[o << 1] * mul[o] % mod ;add[o << 1 | 1] = ( add[o << 1 | 1] * mul[o] + add[o] ) % mod ;mul[o << 1 | 1] = mul[o << 1 | 1] * mul[o] % mod ;int _sum1 , _sum2 , _sum3 ;_sum1 = ( sum1[o << 1] * mul[o] % mod + add[o] * ( m - l + 1 ) % mod ) % mod ;_sum2 = ( mul[o] * mul[o] % mod * sum2[o << 1] % mod+ 2 * add[o] % mod * mul[o] % mod * sum1[o << 1] % mod+ add[o] * add[o] % mod * ( m - l + 1 ) % mod ) % mod ;_sum3 = ( mul[o] * mul[o] % mod * mul[o] % mod * sum3[o << 1] % mod+ 3 * mul[o] % mod * mul[o] % mod * add[o] % mod * sum2[o << 1] % mod+ 3 * mul[o] % mod * add[o] % mod * add[o] % mod * sum1[o << 1] % mod+ add[o] * add[o] % mod * add[o] % mod * ( m - l + 1 ) % mod ) % mod ;sum1[o << 1] = _sum1 ;sum2[o << 1] = _sum2 ;sum3[o << 1] = _sum3 ;_sum1 = ( sum1[o << 1 | 1] * mul[o] % mod + add[o] * ( r - m ) % mod ) % mod ;_sum2 = ( mul[o] * mul[o] % mod * sum2[o << 1 | 1] % mod+ 2 * add[o] % mod * mul[o] % mod * sum1[o << 1 | 1] % mod+ add[o] * add[o] % mod * ( r - m ) % mod ) % mod ;_sum3 = ( mul[o] * mul[o] % mod * mul[o] % mod * sum3[o << 1 | 1] % mod+ 3 * mul[o] % mod * mul[o] % mod * add[o] % mod * sum2[o << 1 | 1] % mod+ 3 * mul[o] % mod * add[o] % mod * add[o] % mod * sum1[o << 1 | 1] % mod+ add[o] * add[o] % mod * add[o] % mod * ( r - m ) % mod ) % mod ;sum1[o << 1 | 1] = _sum1 ;sum2[o << 1 | 1] = _sum2 ;sum3[o << 1 | 1] = _sum3 ;add[o] = 0 ;mul[o] = 1 ;}}void Build ( int l , int r , int o ) {sum1[o] = sum2[o] = sum3[o] = 0 ;add[o] = set[o] = 0 ;mul[o] = 1 ;if ( l == r ) return ;int m = ( l + r ) >> 1 ;Build ( lson ) ;Build ( rson ) ;}void Update ( int ch , int v , int L , int R , int l , int r , int o ) {if ( L <= l && r <= R ) {int len = r - l + 1 ;if ( 1 == ch ) {add[o] = ( add[o] + v ) % mod ;int _sum1 , _sum2 , _sum3 ;_sum1 = ( sum1[o] + v * len ) % mod ;_sum2 = ( sum2[o] + 2 * sum1[o] % mod * v % mod + v * v % mod * len % mod ) % mod ;_sum3 = ( sum3[o] + 3 * v % mod * v % mod * sum1[o] % mod+ 3 * v % mod * sum2[o] % mod + v * v % mod * v % mod * len % mod ) % mod ;sum1[o] = _sum1 ;sum2[o] = _sum2 ;sum3[o] = _sum3 ;}if ( 2 == ch ) {mul[o] = mul[o] * v % mod ;add[o] = add[o] * v % mod ;sum1[o] = sum1[o] * v % mod ;sum2[o] = sum2[o] * v % mod * v % mod ;sum3[o] = sum3[o] * v % mod * v % mod * v % mod ;}if ( 3 == ch ) {set[o] = v ;add[o] = 0 ;mul[o] = 1 ;sum1[o] = v * len % mod ;sum2[o] = v * sum1[o] % mod ;sum3[o] = v * sum2[o] % mod ;}return ;}PushDown ( o , l , r ) ;int m = ( l + r ) >> 1 ;if ( L <= m ) Update ( ch , v , L , R , lson ) ;if ( m < R ) Update ( ch , v , L , R , rson ) ;PushUp ( o ) ;}int Query ( int v , int L , int R , int l , int r , int o ) {if ( L <= l && r <= R ) {if ( 1 == v ) return sum1[o] ;if ( 2 == v ) return sum2[o] ;if ( 3 == v ) return sum3[o] ;}PushDown ( o , l , r ) ;int ans = 0 , m = ( l + r ) >> 1 ;if ( L <= m ) ans = ( ans + Query ( v , L , R , lson ) ) % mod ;if ( m < R ) ans = ( ans + Query ( v , L , R , rson ) ) % mod ;return ans ;}void work () {int n , m , ch , v , l , r ;while ( ~scanf ( "%d%d" , &n , &m ) && ( n || m ) ) {Build ( 1 , n , 1 ) ;while ( m -- ) {scanf ( "%d%d%d%d" , &ch , &l , &r , &v ) ;if ( ch == 4 ) {int ans = Query ( v , l , r , 1 , n , 1 ) ;printf ( "%d\n" , ans ) ;}else Update ( ch , v , l , r , 1 , n , 1 ) ;}}}int main () {work () ;return 0 ;}
阅读全文
0 0
- hdu 4578 Transformation 线段树
- HDU 4578 Transformation (线段树)
- hdu-4578-Transformation-线段树
- HDU 4578 Transformation 线段树
- HDU 4578 Transformation[线段树]
- hdu 4578 Transformation (线段树)
- hdu 4578 Transformation 线段树
- hdu 4578 Transformation 线段树
- HDU 4578 Transformation 线段树
- hdu 4578 Transformation(区间线段树)
- hdu 4578 Transformation(线段树)
- hdu 4578 Transformation [线段树 区间更新]
- HDU 4578 Transformation --线段树,好题
- HDU - 4578 Transformation(线段树 区间修改)
- HDU 4578-Transformation(线段树)
- HDU - 4578 -Transformation(线段树)
- HDU 4578 Transformation (线段树)
- 4578 Transformation 线段树
- HQL单表条件查询
- spring加mybatis(Maven构建项目)简单篇---旨在探究spring配置方法和spring项目出现的异常分析
- CUDA driver version is insufficient for CUDA runtime version
- github团队协作开发流程
- Microbiome:肠道菌群失衡促进高血压
- HDU 4578 Transformation 线段树
- cocos2d_js 拼图游戏源码(点击移动版)
- bzoj 2281: [Sdoi2011]黑白棋 && bzoj 4550: 小奇的博弈(Nimk博弈+DP)
- 混子随笔3
- PyCharm快捷键
- 决策树ID3
- Session ID
- C/C++面试经典
- 用dijkstra获得全部最短路径