Wow! Such Sequence!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3808 Accepted Submission(s): 1079
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
Author
Fudan University
Source
2014 Multi-University Training Contest 3
线段树的好题
/*
本题所求的可以分为两部分,一是在原来的基础上进行加和,二是区间转化为最近的fib.前者线段树的单点更新,没有优化的程度,只能做单点更新,
后者是对一个区间的操作,不能进行的单纯的单点更新,否则会超时,所以可以利用lazy,将每一个点的fib算出来,区间的也算出来,当需要进行操
作3的时候,可以查询到所需要更改的区间将其fib的区间和赋给sum,但是这种操作会影响操作1,因为你只是更新的区间的值,而叶子节点的值是原
值,当你进行单点更新的时候,会在原值的基础上进行,所以得到结果是不对的开始的时候我用lazy标记区间的值是不是都相同,进行区间的合并,在
区间的更新的时候减少时间复杂度,节点记录的是相同的值,但是超时,而且代码还十分冗长,我想是在查询的时候超时了,后来用lazy标记这个区间
是不是进行操作3了,节点分别记录着区间和和fib区间的和,进行操作3的时候直接查询到所在的区间,将其sum=fibsum,并将这个区间的lazy进行
标记,但进行其他的操作的时候,只要判断所在区间lazy是否标记,如果标记就向下进行更新,同时将这个区间的lazy取消,表示这个操作在这个区间
已经完成,不会对子区间产生影响.在回溯的时候进行向上更新,维护每个每个节点意义的正确性
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAX=100000+100;
struct node
{
LL num;//Fibsum
LL sum;//区间和
bool lazy;//标记区间是不是进行了操作3,
} Tree[MAX*5];
LL Arr[100];
void pushup(int site,int L,int R)//向上更新
{
if(L==R)//相等都时候没有必要处理
{
return ;
}
Tree[site].sum=Tree[site<<1].sum+Tree[site<<1|1].sum;
Tree[site].num=Tree[site<<1].num+Tree[site<<1|1].num;
}
void pushDown(int site,int L, int R)//向下更新
{
if(Tree[site].lazy)//如果区间标记
{
if(L==R)
{
Tree[site].sum=Tree[site].num;
}
else
{
Tree[site<<1].lazy=Tree[site<<1|1].lazy=true;
Tree[site<<1].sum=Tree[site<<1].num;
Tree[site<<1|1].sum=Tree[site<<1|1].num;
}
Tree[site].lazy=false;
}
}
LL Rearch(int L,int R,LL s)//二分查找上限
{
while(L<=R)
{
int mid=(L+R)>>1;
if(Arr[mid]>=s)
{
R=mid-1;
}
else
{
L=mid+1;
}
}
if(Arr[L]==s)
{
return Arr[L];
}
else
{
if(Arr[L]-s>=s-Arr[L-1])
{
return Arr[L-1];
}
else
{
return Arr[L];
}
}
}
void Build(int L,int R,int site)//建立线段树,进行初始化
{
if(L==R)
{
Tree[site].lazy=false;
Tree[site].num=1;
Tree[site].sum=0;
return ;
}
int mid=(L+R)>>1;
Build(L,mid,site<<1);
Build(mid+1,R,site<<1|1);
Tree[site].num=Tree[site<<1].num+Tree[site<<1|1].num;
Tree[site].sum=Tree[site<<1].sum+Tree[site<<1|1].sum;
Tree[site].lazy=false;
}
void update(int L,int R,int l,int r,int site)//进行操作3
{
int mid=(L+R)>>1;
if(L==l&&R==r)
{
Tree[site].lazy=true;
Tree[site].sum=Tree[site].num;
return ;
}
pushDown(site,L,R);
if(r<=mid)
{
update(L,mid,l,r,site<<1);
}
else if(l>mid)
{
update(mid+1,R,l,r,site<<1|1);
}
else
{
update(L,mid,l,mid,site<<1);
update(mid+1,R,mid+1,r,site<<1|1);
}
pushup(site,L,R);
}
void Add(int L,int R,int site,int k,int s)//进行操作2
{
if(L==R)
{
Tree[site].sum+=s;
Tree[site].num=Rearch(1,80,Tree[site].sum);
return ;
}
int mid=(L+R)>>1;
pushDown(site,L,R);
if(k<=mid)
{
Add(L,mid,site<<1,k,s);
}
else
{
Add(mid+1,R,site<<1|1,k,s);
}
pushup(site,L,R);
}
LL Query(int L,int R,int l,int r,int site)//进行操作2
{
if(L==l&&R==r)
{
return Tree[site].sum;
}
int mid=(L+R)>>1;
pushDown(site,L,R);
if(r<=mid)
{
return Query(L,mid,l,r,site<<1);
}
else if(l>mid)
{
return Query(mid+1,R,l,r,site<<1|1);
}
else
{
return Query(L,mid,l,mid,site<<1)+Query(mid+1,R,mid+1,r,site<<1|1);
}
}
int main()
{
int op,l,r,d;
Arr[0]=1;
Arr[1]=1;
for(int i=2;i<=80;i++)
{
Arr[i]=Arr[i-1]+Arr[i-2];
}
int n,m;
while(~scanf("%d %d",&n,&m))
{
Build(1,n,1);
for(int i=1;i<=m;i++)
{
scanf("%d",&op);
switch(op)
{
case 1:
scanf("%d %d",&l,&d);
Add(1,n,1,l,d);
break;
case 2:
scanf("%d %d",&l,&r);
printf("%I64d\n",Query(1,n,l,r,1));
break;
case 3:
scanf("%d %d",&l,&r);
update(1,n,l,r,1);
}
}
}
return 0;
}