|
Wow! Such Sequence!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 4627 Accepted Submission(s): 1324 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
Sample Output
|
#include <bits/stdc++.h>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
const int maxn =100005;
const int mod = 10005;
const int INF = 1e9;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define rush() int T;scanf("%d",&T);while(T--)
ll tree1[maxn<<2];
ll tree2[maxn<<2];//伴随树,存储与tree数组数据最近的Fibonacci数
int flag[maxn<<2];
ll f[105];
ll ans;
void init()
{
f[0]=f[1]=1;
f(i,2,105)
{
f[i]=f[i-1]+f[i-2];
}
}
void pushup(int rt)
{
tree1[rt]=tree1[rt<<1]+tree1[rt<<1|1];
tree2[rt]=tree2[rt<<1]+tree2[rt<<1|1];
}
void pushdown(int rt)
{
if(flag[rt]==1)
{
flag[rt<<1]=flag[rt<<1|1]=1;
tree1[rt<<1]=tree2[rt<<1];
tree1[rt<<1|1]=tree2[rt<<1|1];
flag[rt]=0;
}
}
ll getfibo(ll n)
{
int x=lower_bound(f,f+100,n)-f;
if(x==0)
return 1;
return f[x]-n>=n-f[x-1]?f[x-1]:f[x];
}
void build(int l,int r,int rt)
{
if(l==r)
{
tree1[rt]=0;
tree2[rt]=1;
flag[rt]=0;
return;
}
int m=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(int p,int c,int l,int r,int rt)
{
if(l==r)
{
tree1[rt]+=c;
tree2[rt]=getfibo(tree1[rt]);
flag[rt]=0;
return;
}
pushdown(rt);
int m=(l+r)>>1;
if(p<=m)
{
update(p,c,lson);
}
else update(p,c,rson);
pushup(rt);
}
void f_update(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
tree1[rt]=tree2[rt];
flag[rt]=1;
return;
}
pushdown(rt);
int m=(l+r)>>1;
if(L<=m)
{
f_update(L,R,lson);
}
if(m<R)
{
f_update(L,R,rson);
}
pushup(rt);
}
void query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
if(flag[rt])
ans+=tree2[rt];
else
ans+=tree1[rt];
return;
}
pushdown(rt);
int m=(l+r)>>1;
if(L<=m)
{
query(L,R,lson);
}
if(m<R)
{
query(L,R,rson);
}
}
int main()
{
int n,m;
int a,b,c;
init();
while(~scanf("%d%d",&n,&m))
{
mst(tree1,0);
mst(tree2,0);
mst(flag,0);
build(1,n,1);
while(m--)
{
ans=0;
scanf("%d%d%d",&a,&b,&c);
if(a==1)
{
update(b,c,1,n,1);
}
if(a==2)
{
query(b,c,1,n,1);
printf("%I64d\n",ans);
}
if(a==3)
{
f_update(b,c,1,n,1);
}
}
}
return 0;
}