HDU 4578 - Transformation (선분 트 리)

5930 단어 데이터 구조
J - Transformation
Time Limit:8000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit 
Status 
Practice 
HDU 4578
Appoint description:  System Crawler  (2015-11-11)
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 
kk+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 
kk×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 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
 
这题的代码量太感人了,写到我想哭。这题其实难点在于要用公式ax+b来做,这样就不管是乘了先还是加了先。


AC代码:

#include
#include
#include
#include
using namespace std;
#define T 200005
#define mod 10007
#define lson (rt<<1)
#define rson (rt<<1|1)
typedef long long ll;
int n,m;
struct node
{
	int L,R,mid;
	int add,mul;
	int sum[3];
}tree[T<<2];
void PushUp(int rt)//       
{
	for(int i=0;i<3;++i)
	tree[rt].sum[i] = (tree[lson].sum[i]+tree[rson].sum[i])%mod;
}
void cal(int rt,int mul,int add)//    
{
	int len = tree[rt].R - tree[rt].L + 1;//    
	tree[rt].sum[0] = tree[rt].sum[0] * mul %mod;//   
	tree[rt].sum[1] = tree[rt].sum[1] * mul %mod*mul%mod;//    
	tree[rt].sum[2] = tree[rt].sum[2] * mul %mod*mul%mod*mul%mod;//   

	/*
	    val = a*x + c    
	  
	        a mul
	        c mul     
	*/
	tree[rt].mul = (tree[rt].mul * mul )%mod;//    
	tree[rt].add = ((tree[rt].add * mul )% mod + add)%mod;//    
	
	/*
	(ax+c)^3 = a^3*x^3 + 3*a^2*c*x + 3*a*c^2*x + c^3
	  
	tree.sum  1-3  
	*/

	tree[rt].sum[2] = (tree[rt].sum[2] + 3*add%mod*add%mod *tree[rt].sum[0]%mod)%mod;
	tree[rt].sum[2] = (tree[rt].sum[2] + 3*add%mod*tree[rt].sum[1]%mod)%mod ;
	tree[rt].sum[2] = (tree[rt].sum[2] + len * add%mod *add %mod *add%mod)%mod;
	
	tree[rt].sum[1] = (tree[rt].sum[1] + 2*add%mod *tree[rt].sum[0] %mod )%mod;
	tree[rt].sum[1] = (tree[rt].sum[1] + len*add%mod*add%mod)%mod;

	tree[rt].sum[0] = (tree[rt].sum[0] +len*add%mod)%mod;

}
void PushDown(int rt)
{
	if(tree[rt].L==tree[rt].R){return;}
	cal(lson,tree[rt].mul,tree[rt].add);
	cal(rson,tree[rt].mul,tree[rt].add);
	tree[rt].mul = 1;
	tree[rt].add = 0;
}
void Build(int rt,int L,int R)
{
	tree[rt].L = L,tree[rt].R = R;
	tree[rt].mid = (L+R)>>1;
	tree[rt].mul = 1,tree[rt].add = 0;
	tree[rt].sum[0] = tree[rt].sum[1] = tree[rt].sum[2] = 0;
	if(L==R){
		return;
	}
	Build(lson,L,tree[rt].mid);
	Build(rson,tree[rt].mid+1,R);
}
void Update(int rt,int L,int R,int mul,int add)
{
	if(L<=tree[rt].L&&R>=tree[rt].R){
		cal(rt,mul,add);//        
		return;
	}
	PushDown(rt);
	if(R<=tree[rt].mid){
		Update(lson,L,R,mul,add);
	}
	else if(L>tree[rt].mid){
		Update(rson,L,R,mul,add);
	}
	else
	{
		Update(lson,L,tree[rt].mid,mul,add);
		Update(rson,tree[rt].mid+1,R,mul,add);
	}
	PushUp(rt);
}
int query(int rt,int L,int R,int num)
{
	if(L<=tree[rt].L&&R>=tree[rt].R){
			return tree[rt].sum[num-1];
	}
	PushDown(rt);
	if(R<=tree[rt].mid){
		return query(lson,L,R,num);
	}
	else if(L>tree[rt].mid){
		return query(rson,L,R,num);
	}
	else
	{
		return (query(lson,L,tree[rt].mid,num) + query(rson,tree[rt].mid+1,R,num))%mod;
	}
}
int main()
{
#ifdef zsc
	freopen("input.txt","r",stdin);
#endif
	int i,num,x,y,c;
	while(scanf("%d%d",&n,&m)&&(n&&m))
	{
		Build(1,1,n);
		for(i=0;i

좋은 웹페이지 즐겨찾기