HDU4893线段树单点和新+二分+laz标记

HDU4893线段树单点跟新+二分+laz标记

原题http://acm.hdu.edu.cn/showproblem.php?pid=4893

Wow! Such Sequence!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1932    Accepted Submission(s): 591


Problem Description
Recently, Doge got a funny birthday present from his new friend, Protein Tiger from St. Beeze College. No, not cactuses. It's a mysterious blackbox.

After some research, Doge found that the box is maintaining a sequence an of n numbers internally, initially all numbers are zero, and there are THREE "operations":

1.Add d to the k-th number of the sequence.
2.Query the sum of ai where l ≤ i ≤ r.
3.Change ai to the nearest Fibonacci number, where l ≤ i ≤ r.
4.Play sound "Chee-rio!", a bit useless.

Let F0 = 1,F1 = 1,Fibonacci number Fn is defined as Fn = Fn - 1 + Fn - 2 for n ≥ 2.

Nearest Fibonacci number of number x means the smallest Fn where |Fn - x| is also smallest.

Doge doesn't believe the machine could respond each request in less than 10ms. Help Doge figure out the reason.


 

Input
Input contains several test cases, please process till EOF.
For each test case, there will be one line containing two integers n, m.
Next m lines, each line indicates a query:

1 k d - "add"
2 l r - "query sum"
3 l r - "change to nearest Fibonacci"

1 ≤ n ≤ 100000, 1 ≤ m ≤ 100000, |d| < 231, all queries will be valid.


 

Output
For each Type 2 ("query sum") operation, output one line containing an integer represent the answer of this query.


 

Sample Input
1 1 2 1 1 5 4 1 1 7 1 3 17 3 2 4 2 1 5


 

Sample Output
0 22
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <vector>
#include <map>
using namespace std;
#define int64 __int64
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 200000;
struct node{
	int64 l;
	int64 r;
	int64 sum;
	bool laz;
	//int64 mid(){
	//	return (l+r)/2;
	//}
};
struct node tree[maxn<<2];
set<int64> ss;
int64 f[10000];

void PushUp(int64 rt){
	if(tree[rt<<1].laz==1 && tree[rt<<1|1].laz==1){//如果两个子节点都不需要更新,那么父节点也不需要跟新
		tree[rt].laz = 1;
	}
	else{
		tree[rt].laz = 0;
	}
}
void getf(){
	int i;
	f[0] = 1;
	f[1] = 1;
	ss.insert(f[0]);
	ss.insert(f[1]);
	for(i=2;i<=73;i++){
		f[i] = f[i-1]+f[i-2];
		ss.insert(f[i]);
	}
}

void build(int64 l,int64 r,int64 rt){
	tree[rt].l = l;
	tree[rt].r = r;
	tree[rt].laz = 0;
	if(tree[rt].l == tree[rt].r){
		tree[rt].sum = 0;
		return ;
	}
	int64 m = (tree[rt].l+tree[rt].r)/2;
	build(lson);
	build(rson);
	tree[rt].sum = tree[rt<<1].sum + tree[rt<<1|1].sum;
}





void update(int64 a,int64 b,int64 rt){
	if(tree[rt].l == tree[rt].r){
		tree[rt].sum+=b;
		tree[rt].laz = 0;
		return ;
	}
	int64 m = (tree[rt].l+tree[rt].r)/2;
	if(a <= m){
		update(a,b,rt<<1);
	}
	else{
		update(a,b,rt<<1|1);
	}
	tree[rt].sum = tree[rt<<1].sum+tree[rt<<1|1].sum;
	PushUp(rt);
}

void update2(int64 L,int64 R,int64 rt){
	if(tree[rt].laz == 1){
		return ;
	}
	if(tree[rt].l == tree[rt].r){
		tree[rt].laz = 1;
		set<int64>::iterator it1,it2;
		int64 l,r;
		it2=it1=ss.lower_bound(tree[rt].sum);//获得》=tree[rt].sum这个数在斐波那契数列中的地址
		l = *it2;
		if(it1 != ss.begin()){
			it1--;
		}
		r = *it1;
		tree[rt].sum = (tree[rt].sum-*it1)>((*it2)-tree[rt].sum)?l:r;
		
		return ;
	}
	int64 m = (tree[rt].l + tree[rt].r)/2;
	if(L<=m){
		update2(L,R,rt<<1);
	}
	if(R > m){
		update2(L,R,rt<<1|1);
	}
	tree[rt].sum = tree[rt<<1].sum + tree[rt<<1|1].sum;
	PushUp(rt);
}

int64 query(int64 L,int64 R,int64 rt){
	if(L<=tree[rt].l && tree[rt].r<=R){
		return tree[rt].sum;
	}
	int64 m = (tree[rt].l+tree[rt].r)/2;
	int64 ret = 0;
	if(L <= m){
		ret+=query(L,R,rt<<1);
	}
	if(R > m){
		ret+=query(L,R,rt<<1|1);
	}
	//else
	//{
	//	ret+=query(L,R,rt<<1);
	//	ret+=query(L,R,rt<<1|1);
	//}
	
	return ret;
}

int main(){
	int64 n,m;
	int64 op,a,b;
	ss.clear();
	getf();
	
	while(~scanf("%I64d%I64d",&n,&m)){
		build(1,n,1);
		while(m--){
			scanf("%I64d",&op);
			if(op == 2){
				scanf("%I64d%I64d",&a,&b);
				printf("%I64d\n",query(a,b,1));
			}
			else if(op == 1){
				scanf("%I64d%I64d",&a,&b);
				update(a,b,1);
			}
			else if(op == 3){
				scanf("%I64d%I64d",&a,&b);
				update2(a,b,1);
			}
		}
		
	}
	
	return 0;
}