您的位置:首页 > 其它

HDU 4893(线段树区间更新)

2015-10-09 21:30 369 查看


Wow! Such Sequence!

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 3856 Accepted Submission(s): 1085

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 <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <string.h>
using namespace std;
#define  ll __int64
const int maxn=100000+10;
ll fib[100],ans=0;
void fun()
{
fib[0]=1;
fib[1]=1,fib[2]=1;
for(ll i=3;i<=90;i++)
fib[i]=fib[i-1]+fib[i-2];
}
struct Node
{
int l,r,flag;// flag=1  说明是fib标记
ll sum,fsum;
int mid()
{
return (l+r)/2;
}
}tree[maxn*6];
ll Find(int l,int r,__int64 seach)
{
while(l<=r)
{
int mid=(l+r)/2;
if(fib[mid]<=seach)
l=mid+1;
else
r=mid-1;
}
if(fib[l]-seach>=seach-fib[r])
return fib[r];
else
return fib[l];
}
void Buildtree(int rt,int l,int r)
{
tree[rt].l=l;
tree[rt].r=r;
tree[rt].flag=tree[rt].sum=0;
tree[rt].fsum=0;
if(l!=r)
{
Buildtree(2*rt,l,(l+r)/2);
Buildtree(2*rt+1,(l+r)/2+1,r);
tree[rt].fsum=tree[2*rt].fsum+tree[2*rt+1].fsum;
tree[rt].sum=tree[2*rt].sum+tree[rt*2+1].sum;
}
else
tree[rt].fsum=1;
}
void Pushdown(int rt)
{
tree[2*rt].sum=tree[2*rt].fsum;
tree[2*rt+1].sum=tree[2*rt+1].fsum;
tree[2*rt].flag=tree[2*rt+1].flag=1;
tree[rt].flag=0;
}
void Pushup(int rt)
{
tree[rt].sum=tree[2*rt].sum+tree[2*rt+1].sum;
tree[rt].fsum=tree[2*rt].fsum+tree[2*rt+1].fsum;
}
void Update(int rt,int k,int d)
{
if(tree[rt].flag) Pushdown(rt);
if(tree[rt].l==tree[rt].r&&tree[rt].l==k)
{
tree[rt].sum+=d;
tree[rt].fsum=Find(1,90,tree[rt].sum);
tree[rt].flag=0;
return;
}
if(k<=tree[rt].mid())
Update(2*rt,k,d);
else
Update(2*rt+1,k,d);
Pushup(rt);
}
void Update_1(int rt,int l,int r)
{
if(tree[rt].flag)Pushdown(rt);
if(tree[rt].l==l&&tree[rt].r==r)
{
tree[rt].sum=tree[rt].fsum;
tree[rt].flag=1;
return;
}
if(r<=tree[rt].mid())
Update_1(2*rt,l,r);
else if(l>tree[rt].mid())
Update_1(2*rt+1,l,r);
else
{
Update_1(2*rt,l,tree[rt].mid());
Update_1(2*rt+1,tree[rt].mid()+1,r);
}
Pushup(rt);
}
void Query(int rt,int l,int r)
{
if(tree[rt].flag)Pushdown(rt);
if(tree[rt].l==l&&tree[rt].r==r)
{
ans+=tree[rt].sum;
return;
}
if(r<=tree[rt].mid())
Query(2*rt,l,r);
else if(l>tree[rt].mid())
Query(2*rt+1,l,r);
else
{
Query(2*rt,l,tree[rt].mid());
Query(2*rt+1,tree[rt].mid()+1,r);
}
}
int main()
{
int n,m;
fun();
while(~scanf("%d%d",&n,&m))
{
Buildtree(1,1,n);
while(m--)
{
int type,l,r;
scanf("%d%d%d",&type,&l,&r);
if(type==1)
Update(1,l,r);
else if(type==3)
Update_1(1,l,r);
else if(type==2)
{
ans=0;
Query(1,l,r);
printf("%I64d\n",ans);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: