Wow! Such Sequence!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 315 Accepted Submission(s): 95
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.
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.
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
Source
线段树裸题,用sumf[]表示该段fib数的和,加一个add[]标记
具体看代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <functional>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
//#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
#define INF 1e9
#define MAXN 21
#define maxn 100005
#define mod 1000000009
#define eps 1e-7
#define pi 3.1415926535897932384626433
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define scan(n) scanf("%d",&n)
#define scanll(n) scanf("%I64d",&n)
#define scan2(n,m) scanf("%d%d",&n,&m)
#define scans(s) scanf("%s",s);
#define ini(a) memset(a,0,sizeof(a))
#define out(n) printf("%d\n",n)
ll gcd(ll a,ll b) {return b==0?a:gcd(b,a%b);}
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
ll add[maxn<<2];
ll fib[90];
ll sum[maxn<<2];
ll sumf[maxn<<2];
ll flag[maxn<<2];
int n,m;
void init()
{
fib[0] = 1;
fib[1] = 1;
for(int i = 2;i < 90; i++) fib[i] = (fib[i-1] + fib[i-2]);
}
void PushUp(int rt) {
sum[rt] = (sum[rt<<1] + sum[rt<<1|1]);
sumf[rt] = (sumf[rt<<1] + sumf[rt<<1|1]);
}
ll Abs(ll a)
{
return a > 0 ? a : -a;
}
void build(int l,int r,int rt) {
add[rt] = 0;
sum[rt] = 0;
flag[rt] = 0;
if (l == r) {
sumf[rt] = 1;
return ;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
PushUp(rt);
}
void PushDown(int l,int r,int rt) {
if(add[rt]){
add[rt<<1] = add[rt];
add[rt<<1|1] = add[rt];
sum[rt<<1] = sumf[rt<<1];
sum[rt<<1|1] = sumf[rt<<1|1];
int m = (r + l) >> 1;
flag[rt<<1] = m - l + 1;
flag[rt<<1|1] = r - m;
add[rt] = 0;
}
}
void update(int L, int R, int d,int l,int r,int rt) {
if (L <= l && r <= R) {
if(flag[rt])
{
sum[rt] = sumf[rt] + d;
flag[rt] = 0;
add[rt] = 0;
}
else
sum[rt] += d;
int pos = lower_bound(fib, fib+90, sum[rt]) - fib;
if(pos == 0) sumf[rt] = 1;
else if(Abs(fib[pos-1] - sum[rt]) <= Abs(fib[pos] - sum[rt])) sumf[rt] = fib[pos-1];
else sumf[rt] = fib[pos];
return ;
}
PushDown(l , r , rt);
int m = (l + r) >> 1;
if (L <= m) update(L , R , d, lson);
if (m < R) update(L , R, d, rson);
PushUp(rt);
}
void change(int L,int R,int l,int r,int rt)
{
if(L <= l && r <= R)
{
flag[rt] = r - l + 1;
add[rt] = 1;
sum[rt] = sumf[rt];
return;
}
PushDown(l , r , rt);
int m = (l + r) >> 1;
if (L <= m) change(L , R , lson);
if (m < R) change(L , R, rson);
PushUp(rt);
}
ll query(int L,int R,int l,int r,int rt) {
if (L <= l && r <= R) {
return sum[rt];
}
PushDown( l , r , rt);
int m = (l + r) >> 1;
ll ret = 0;
if (L <= m) ret = (ret + query(L , R , lson)) ;
if (m < R) ret = (ret + query(L , R , rson));
return ret;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
init();
while(~scanf("%d%d",&n,&m))
{
build(1,n,1);
int ty,L,R;
// change(1,5,1,n,1);
while(m--)
{
scanf("%d%d%d",&ty,&L,&R);
if(ty == 1) update(L,L,R,1,n,1);
else if(ty == 2) printf("%I64d\n",query(L,R,1,n,1));
else change(L,R,1,n,1);
}
}
return 0;
}