hdu 4578 -线段树的多种操作(要相信柯学)

Transformation

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.

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

做到这题的时候以为是道水题,切了两小时切不动,后来才知道不同操作在pushdown的时候有优先级。可是我太懒了,于是就开始相信柯学。但这种东西还是且用且珍惜,不然哪天突然卡你一手就不太妙。
AC代码

#include
#define outtime() cerr<<"User Time = "<<(double)clock()/CLOCKS_PER_SEC<
#define S_IT set::iterator
using namespace std;
typedef long long ll;
const int N = 1e5 +10;
const ll mod = 10007;
int n, m;
struct Node{
	int l, r;
	mutable ll v;
	Node () {}
	Node(int l, int r = 0, ll v = 0) : l(l), r(r), v(v) {}
	bool operator < (const Node &a)const{
		return l < a.l;
	}
};
set <Node> st;
S_IT split(ll pos){
	S_IT it = st.lower_bound(Node(pos));
	if(it != st.end() && it -> l == pos) return it;
	it --;
	int l = it -> l, r = it -> r;
	ll v = it -> v;
	st.erase(it);
	st.insert(Node(l, pos - 1, v));
	return st.insert(Node(pos, r, v)).first;
}
void st_assign(int l, int r, ll v){
	S_IT itr = split(r + 1), itl = split(l);
	st.erase(itl, itr);
	st.insert(Node(l, r, v));
}
void add(int l, int r, ll c){
	S_IT itr = split(r + 1), itl = split(l);
	for(; itl != itr; ++ itl){
		itl -> v =  (itl -> v + c) % mod;
	}
}
void mul(int l, int r, int c){
	S_IT itr = split(r + 1), itl = split(l);
	for(; itl != itr; ++ itl){
		itl -> v =  (itl -> v * c) % mod;
	}
}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll query(int l, int r, ll p){
	S_IT itr = split(r + 1), itl = split(l);
	ll ans = 0;
	for(; itl != itr; ++ itl){
		ans = ((itl ->r - itl -> l +1) * powmod(itl -> v, p) + ans) % mod;
	}
	return ans;
}
int l, r, type;
ll c;
void solve(){
	st.clear();
	for(int i = 1; i <= n; ++i) st.insert(Node(i, i, 0));
	while(m--){
		scanf("%d%d%d%d",&type, &l, &r, &c);
		if(type == 1) add(l, r, c);
		if(type == 2) mul(l, r, c);
		if(type == 3) st_assign(l, r, c);
		if(type == 4) printf("%d\n",query(l, r, c));
	}
}
int main(){
	while(~scanf("%d %d",&n, &m), n || m)
		solve();
	return 0;
}

你可能感兴趣的:(线段树,珂朵莉树)