HDU4843Wow! Such Sequence!(樹狀陣列寫法)
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.
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.
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
操作一和操作二都是基本的樹狀陣列的操作,操作三需要一定的優化,用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;
}
相關文章
- HDU 6274 Master of Sequence(思維+樹狀陣列+二分)AST陣列
- 樹狀陣列陣列
- HDU5147 Sequence II(樹狀陣列+字首和+字尾和)陣列
- 解析樹狀陣列陣列
- 樹狀陣列詳解陣列
- 樹狀陣列基礎陣列
- poj 2481 樹狀陣列陣列
- hdu 3874 樹狀陣列陣列
- 二維樹狀陣列陣列
- 樹狀陣列模板題 & (樹狀陣列 1:單點修改,區間查詢)陣列
- 樹狀陣列和逆序對陣列
- hdu 5147 樹狀陣列陣列
- 【筆記/模板】樹狀陣列筆記陣列
- 樹狀陣列快速入門陣列
- 樹狀陣列模板+習題集陣列
- 樹狀陣列3種基本操作陣列
- 學習筆記----樹狀陣列筆記陣列
- 樹狀陣列upc1976陣列
- CSU 4441 Necklace (樹狀陣列/LIS)陣列
- 樹狀陣列(我是真小白)陣列
- 資料結構——樹狀陣列資料結構陣列
- 線段樹+差分——【模板】樹狀陣列2陣列
- hdu 4836 The Query on the Tree(線段樹or樹狀陣列)陣列
- 10:Challenge 3(樹狀陣列直接修改)陣列
- POJ 3928 Ping pong(樹狀陣列)陣列
- HDU 1556 Color the ball(線段樹|樹狀陣列)陣列
- CF 293 E Close Vertices (樹的分治+樹狀陣列)陣列
- HDU 1166 敵兵佈陣 (樹狀陣列)陣列
- HDU 1166 敵兵佈陣(樹狀陣列)陣列
- 洛谷題單指南-二叉堆與樹狀陣列-P3368 【模板】樹狀陣列 2陣列
- POJ-2352 Stars(樹狀陣列)陣列
- 【luogu3368】模板 樹狀陣列 2陣列
- 【二維樹狀陣列】poj 2155 Matrix陣列
- D 區間求和 [數學 樹狀陣列]陣列
- 二維樹狀陣列-poj2155陣列
- T4701 【卜卜】樹狀陣列模板陣列
- hdu4325 樹狀陣列+離散化陣列
- poj 1195 二維樹狀陣列陣列