hdu 4893 Wow! Such Sequence! 线段树


Wow! Such Sequence!

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


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 F 0 = 1,F 1 = 1,Fibonacci number Fn is defined as F n = F n - 1 + F n - 2 for n ≥ 2.

Nearest Fibonacci number of number x means the smallest Fn where |F n - 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| < 2 31, 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
 


一道区间线段树更新. 用了set 的二分.     找过一次最近斐波那契数.  就可以laz标记掉 下次不再更新.  否则会超时.


#include<stdio.h>
#include<set>
#define int64 __int64
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int64 f[10000];
const int maxn=200000;
set<int64> ss;
struct node
{
	int64 l,r;
	bool laz;  //laz==0 代表需要更新 斐波那契.  laz==1 代表不用更新斐波那契操作 也就是当节点的laz==1他和他的子孙第三种操作可以忽略.
	int64 sum;
	int64 mid()
	{
		return (l+r)/2;
	}
};
struct node tree[maxn<<2];
void PushUp(int64 rt) {
	if(tree[rt<<1].laz && tree[rt<<1|1].laz)//如果两个儿子都不用更新斐波那契, 那父节点也不用
		tree[rt].laz = 1;
	else
		tree[rt].laz = 0;
}
void getf()//算斐波那契数 存入f数组
{
	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].mid();
	build(lson);
	build(rson);
	tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}

 
void updata(int64 p,int64 add,int64 rt)
{
	if(tree[rt].l==tree[rt].r)//单点更新,
	{
        tree[rt].laz=0;//如果更新了 那么就又需要重新寻找最近的斐波那契数
		tree[rt].sum+=add;
		return ;
	}
	int64 m=tree[rt].mid();
	if(p<=m)
		updata(p,add,rt<<1);
	else
		updata(p,add,rt<<1|1);
	tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;//更新sum
    PushUp(rt);//更新laz
}

void updata2(int64 L,int64 R,int64 rt) //3操作  更新到最近的斐波那契数
{
	if(tree[rt].laz==1) 
		return;
	if(tree[rt].l==tree[rt].r)
	{
		tree[rt].laz=1;//该节点已经更新到最近斐波那契数,以后不再更新  
		
		//二分查找最近的斐波那契数.
		int64 l,r;
		set<int64>::iterator it1,it2;
		it2=it1=ss.lower_bound(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].mid();
	if (L <= m) 
		updata2(L , R , rt<<1);
	if (m < R)
		updata2(L , R , rt<<1|1);
	PushUp(rt);
	tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
 
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].mid();
	int64 ret=0;
	if(R<=m)
		ret+=query(L,R,rt<<1);
	else if(L>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 a,b,c;
	int64 op,i;
	int64 n,m;
	ss.clear();
	getf();
	while(scanf("%I64d %I64d",&n,&m)!=EOF)
	{
		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);
				updata(a,b,1);
			}
			else if(op==3)//f
			{
				scanf("%I64d %I64d",&a,&b);
			    updata2(a,b,1);
			}
		}
	}
    return 0;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值