多校3 hdu4893
Wow! Such Sequence!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1734 Accepted Submission(s): 528
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
乍一看被吓到了 ,仔细想想1操作时单点跟新3操作是区间跟新但是已经是Fibonacci数列里的数 不用跟新,这样子加个标记,表示当前区间的所有数是否都是Fibonacci数。这样区间新就会提前结束。(假设一开始就3操作整个区间,之后每次1操作最多只能改变一个点的值,所以以后的区间操作都相当于单点跟新一般。)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #define N 110000 using namespace std; typedef long long LL; LL n,m; LL sum[N<<2]; LL fi[N<<2]; LL fibo[110]; LL inf=1ll<<62; LL num; void getf(){ fibo[0]=fibo[1]=1; LL i; for(i=2;fibo[i-1]+fibo[i-2]<inf;i++){ fibo[i]=fibo[i-1]+fibo[i-2]; //printf("%I64d\n",fibo[i]); } num=i; return ; } LL isFibo(LL x){ if(x<=0)return 0; LL l=0,r=num-1; LL mid; while(l<=r){ mid=(l+r)>>1; if(x<=fibo[mid])r=mid-1; else l=mid+1; } if(fibo[r]==x)return 1; return 0; } LL search(LL x){ if(x<=1)return 1; LL l=0,r=num-1; LL mid; while(l<=r){ mid=(l+r)>>1; if(x<=fibo[mid])r=mid-1; else l=mid+1; } LL ma=inf; LL ans; if(r>=0&&abs(fibo[r]-x)<ma){ ma=abs(fibo[r]-x); ans=fibo[r]; } if(abs(fibo[l]-x)<ma){ ma=abs(fibo[l]-x); ans=fibo[l]; } return ans; } void pushup(LL rt){ sum[rt]=sum[rt<<1|1]+sum[rt<<1]; fi[rt]=(fi[rt<<1|1]==1&&fi[rt<<1]==1)?1:0; } void updata1(LL x,LL a,LL l,LL r,LL rt){ if(l==r){ sum[rt]+=a; if(isFibo(sum[rt]))fi[rt]=1; else fi[rt]=0; return; } LL mid=(l+r)>>1; if(x<=mid)updata1(x,a,lson); else updata1(x,a,rson); pushup(rt); } void updata2(LL L,LL R,LL l,LL r,LL rt){ if(L<=l&&r<=R&&fi[rt]==1){ return; } if(l==r&&fi[rt]==0){ sum[rt]=search(sum[rt]); fi[rt]=1; return; } LL mid=(l+r)>>1; if(L<=mid)updata2(L,R,lson); if(mid<R )updata2(L,R,rson); pushup(rt); } LL query(LL L,LL R,LL l,LL r,LL rt){ if(L<=l&&r<=R){ return sum[rt]; } LL mid=(l+r)>>1; LL ans=0; if(L<=mid)ans+=query(L,R,lson); if(mid<R)ans+=query(L,R,rson); return ans; } void build(LL l,LL r,LL rt){ if(l==r){ sum[rt]=fi[rt]=0; return; } LL mid=(l+r)>>1; build(lson); build(rson); pushup(rt); } int main() { getf(); LL op; LL l,r; //freopen("4893.in","r",stdin); //freopen("4893.txt","w",stdout); while(~scanf("%I64d%I64d",&n,&m)){ build(1,n,1); for(LL i=0;i<m;i++){ scanf("%I64d%I64d%I64d",&op,&l,&r); if(op==1){ updata1(l,r,1,n,1); } else if(op==3){ updata2(l,r,1,n,1); } else { printf("%I64d\n",query(l,r,1,n,1)); } } } return 0; }

浙公网安备 33010602011771号