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
Author
Fudan University
Source
2014 Multi-University Training Contest 3
操作一和操作二都是基本的树状数组的操作,操作三需要一定的优化,用set记录已经改点是否被更改成斐波那契数,如果已经更改了,就不需要更新了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <cstdlib>
using namespace std;
typedef long long LL;
const int MAX=100010;
LL tree[MAX],fib[100];
int del[MAX],n,m;
inline int Lowbit(int t){
return t&-t;
}
LL Query(int i){
LL s=0;
while(i>0){
s+=tree[i];
i-=Lowbit(i);
}
return s;
}
void Update(int i,LL v){
while(i<=n){
tree[i]+=v;
i+=Lowbit(i);
}
}
LL myabs(LL a){
return a > 0 ? a :-a;
}
void init(){
fib[0]=1;fib[1]=1;
for(int i=2;i<=91;i++)
fib[i]=fib[i-1]+fib[i-2];
}
int main(){
init();
while(~scanf("%d%d",&n,&m)){
memset(tree,0,sizeof(tree));
set<int>st;
set<int>::iterator it;
for(int i=1;i<=n;i++) st.insert(i);
while(m--){
int ord,a,b;
scanf("%d%d%d",&ord,&a,&b);
if(ord==1){
Update(a,b);
st.insert(a);
}
if(ord==2)
printf("%I64d\n",Query(b)-Query(a-1));
if(ord==3){
it=lower_bound(st.begin(),st.end(),a);
del[0]=0;
for(;it!=st.end()&&((*it)<=b);it++){
int p=*it;
del[++del[0]]=p;
LL u=Query(p)-Query(p-1),k;
int t=lower_bound(fib+1,fib+92,u)-fib;
if(myabs(fib[t]-u)<myabs(fib[t-1]-u)) k=fib[t];
else k=fib[t-1];
Update(p,k-u);
}
for(int i=1;i<=del[0];i++)//剔除已经变成斐波那契数的序号
st.erase(del[i]);
}
}
}
return 0;
}