HDU 4893(Wow! Such Sequence!-线段树单点修改+区间求和+改为最近Fib数)
来源:互联网 发布:淘宝卖家如何发动态 编辑:程序博客网 时间:2024/05/16 06:01
Wow! Such Sequence!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3830 Accepted Submission(s): 1081
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 12 1 15 41 1 71 3 173 2 42 1 5
Sample Output
022
Author
Fudan University
Source
2014 Multi-University Training Contest 3
Recommend
We have carefully selected several similar problems for you: 5426 5425 5424 5423 5422
存一个sumv,
和改为Fib后的fsum,
mark表示是否改为Fib数
然后线段树
#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<functional>#include<iostream>#include<cmath>#include<cctype>#include<ctime>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=pre[x];p;p=next[p])#define Forpiter(x) for(int &p=iter[x];p;p=next[p]) #define Lson (o<<1)#define Rson ((o<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define MEM2(a,i) memset(a,i,sizeof(a));#define INF (2139062143)#define MAXN (200000+109)#define MAXM (100000+10)typedef __int64 ll;ll f[100];const int MFIB = 80;ll find (ll x) {if (x<=1) return 1; int m=upper_bound(f+1,f+80,x)-(f+1);if (llabs(f[m+1]-x)<llabs(f[m]-x)) m++;if (llabs(f[m-1]-x)<=llabs(f[m]-x)) m--;return f[m];} class SegmentTree { public: ll sumv[MAXN*4],fsum[MAXN*4]; bool mark[MAXN*4]; int n; SegmentTree(){} void mem(int _n) { MEM(sumv) MEM(fsum) MEM(mark) n=_n; build(1,1,_n); } void build(int o,int L,int R) {if (L==R) {sumv[o]=mark[o]=0;fsum[o]=1; }else{int M=(R+L)>>1;if (L<=M) build(Lson,L,M);if (M< R) build(Rson,M+1,R);}maintain(o,L,R); } void maintain(int o,int L,int R) { if (L<R) {sumv[o]=sumv[Lson]+sumv[Rson];fsum[o]=fsum[Lson]+fsum[Rson];}}int y1,y2,v;void update(int o,int L,int R) {pushdown(o,L,R);if (L==R) {sumv[o]+=v;fsum[o]=find(sumv[o]);mark[o]=0;return ;}else{int M=(R+L)>>1;if (y1<=M) update(Lson,L,M); if (M< y2) update(Rson,M+1,R);}maintain(o,L,R); }void update3(int o,int L,int R) //fib{pushdown(o,L,R);if (y1<=L&&R<=y2) {mark[o]=1;sumv[o]=fsum[o];return ; }else{int M=(R+L)>>1;if (y1<=M) update3(Lson,L,M); //维护pushodown,再次maintain if (M< y2) update3(Rson,M+1,R);}maintain(o,L,R); }void pushdown(int o,int L,int R) {if (L>R) return;if (L==R) {mark[o]=0;return ;} if (mark[o]){mark[Lson]= mark[ Rson ] = 1;sumv[Lson]=fsum[Lson]; sumv[Rson]=fsum[Rson]; mark[o]=0;}}ll _sum; void query2(int o,int L,int R){pushdown(o,L,R);if (y1<=L&&R<=y2){_sum+=sumv[o];return;} else {int M=(L+R)>>1;if (y1<=M) query2(Lson,L,M);if (M< y2) query2(Rson,M+1,R);}}void add(int l,int r,ll v){y1=l,y2=r;this->v=v;update(1,1,n);}void fibset(int l,int r){y1=l,y2=r;update3(1,1,n);}ll ask(int l,int r){_sum=0;y1=l,y2=r;query2(1,1,n);return _sum;}void print(){For(i,n)cout<<ask(i,i)<<' ';cout<<endl;}}S; int n,m;int main(){//freopen("data.in","r",stdin);//freopen("data.out","w",stdout);f[1]=f[2]=1;Fork(i,3,80) f[i] = f[i-1] + f[i-2]; //Fork(i,-10,100) cout<<i<<" "<<find(i)<<endl; while(scanf("%d%d",&n,&m)==2) {S.mem(n);For(i,m) {int p;scanf("%d",&p);if (p==1) {int k;ll d;scanf("%d%I64d",&k,&d);S.add(k,k,d);} else {int a,b;scanf("%d%d",&a,&b);if (p==2) printf("%I64d\n",S.ask(a,b));else if (p==3) S.fibset(a,b);}//S.print();}}return 0;}
0 0
- HDU 4893(Wow! Such Sequence!-线段树单点修改+区间求和+改为最近Fib数)
- HDU:4893 Wow! Such Sequence!(线段树单点更新+区间求和)
- hdu 4893 Wow! Such Sequence! 线段树单点更新+区间更新+区间查询
- HDU 4893 Wow! Such Sequence! (线段树单点更新+区间更新+区间查询+二分)
- HDOJ--4893--Wow! Such Sequence!【线段树+单点、区间更新】
- Hdu 4893 Wow! Such Sequence! 线段树单点更新,成段求和
- hdu 4893 Wow! Such Sequence! (线段树 区间更新+单点更新)
- hdu4893 Wow! Such Sequence!,树状数组,线段树,单点修改,区间更新
- hdu 4893 Wow! Such Sequence! 线段树
- hdu 4893 Wow! Such Sequence!(线段树)
- HDU Wow! 4893 Such Sequence!(线段树)
- 【HDU】4893 Wow! Such Sequence! 线段树
- HDU 4893 Wow! Such Sequence!(线段树)
- HDU 4893 Wow! Such Sequence! 线段树
- 【HDU 4893 多校联合】 Wow! Such Sequence!【线段树】
- HDU 4893 Wow! Such Sequence! (线段树)
- HDU 4893 Wow! Such Sequence! 解题报告(线段树)
- hdu 4893 Wow! Such Sequence! (线段树+ 心得)
- 链表的基本操作
- poj 1085 Triangle War 1568 Find the Winning Move 极大极小搜索 alpha-beta剪枝
- Java Java!
- Android Emulator 不能运行cocos2dx的解决方案
- Linux下java -version版本不对
- HDU 4893(Wow! Such Sequence!-线段树单点修改+区间求和+改为最近Fib数)
- 【JAVA】--对环境变量的了解
- 字符串之复制函数
- linux下APUE的代码测试文件路径配置
- 【kruskal】【dfs】【JSOI 2008】【bzoj 1016】最小生成树计数
- javascript面试题 大小写问题
- 关于ormapping
- 代理 单例 属性 block 四大传值方式
- 自学Java系列 笔记1 java的基本语法1