首页 > 代码库 > HDOJ 4893 Wow! Such Sequence!
HDOJ 4893 Wow! Such Sequence!
Wow! Such Sequence!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1265 Accepted Submission(s): 387
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
2014 Multi-University Training Contest 3
Recommend
We have carefully selected several similar problems for you: 4896 4895 4894 4892 4890
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long int LL; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int maxn=110000; LL sum[maxn<<2],fibsum[maxn<<2],fib[100]; bool cov[maxn<<2]; void GET_FIB() { fib[0]=fib[1]=1LL; for(int i=2;i<100;i++) fib[i]=fib[i-1]+fib[i-2]; } LL CHANGE(LL x) { int mark=-1;LL mx=99999999999LL; for(int i=0;i<100;i++) { if(abs(x-fib[i])<mx) { mx=abs(x-fib[i]); mark=i; } } return fib[mark]; } void push_up(int rt) { sum[rt]=sum[rt<<1]+sum[rt<<1|1]; fibsum[rt]=fibsum[rt<<1]+fibsum[rt<<1|1]; } void push_down(int l,int r,int rt) { if(l==r) return ; if(cov[rt]) { sum[rt<<1]=fibsum[rt<<1]; sum[rt<<1|1]=fibsum[rt<<1|1]; cov[rt<<1]=cov[rt<<1|1]=cov[rt]; cov[rt]=0; } } void build(int l,int r,int rt) { sum[rt]=0,fibsum[rt]=1,cov[rt]=0; if(l==r) return ; int m=(l+r)/2; build(lson); build(rson); push_up(rt); } void ADD(int Kth,LL d,int l,int r,int rt) { push_down(l,r,rt); if(l==r) { sum[rt]+=d; fibsum[rt]=CHANGE(sum[rt]); return ; } int m=(l+r)/2; if(Kth<=m) ADD(Kth,d,lson); else ADD(Kth,d,rson); push_up(rt); } LL query(int L,int R,int l,int r,int rt) { if(L<=l&&r<=R) { return sum[rt]; } push_down(l,r,rt); int m=(l+r)/2; LL ret=0; if(L<=m) ret+=query(L,R,lson); if(R>m) ret+=query(L,R,rson); return ret; } void C_F(int L,int R,int l,int r,int rt) { if(L<=l&&r<=R) { cov[rt]=1; sum[rt]=fibsum[rt]; return ; } push_down(l,r,rt); int m=(l+r)/2; if(L<=m) C_F(L,R,lson); if(R>m) C_F(L,R,rson); push_up(rt); } int main() { int n,m; GET_FIB(); while(scanf("%d%d",&n,&m)!=EOF) { build(1,n,1); LL a,b,c; while(m--) { scanf("%I64d%I64d%I64d",&a,&b,&c); if(a==1) ADD(b,c,1,n,1); else if(a==2) printf("%I64d\n",query(b,c,1,n,1)); else if(a==3) C_F(b,c,1,n,1); } } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。