Wow! Such Sequence! http://acm.hdu.edu.cn/showproblem.php?pid=4893
线段树,比赛时写的不好。
1 #include<cstdio> 2 #include<algorithm> 3 #define lrrt int L,int R,int rt 4 #define iall 1,n,1 5 #define imid int mid=(L+R)>>1 6 #define lson L,mid,rt<<1 7 #define rson mid+1,R,rt<<1|1 8 using namespace std; 9 typedef __int64 LL; 10 const int M=100010; 11 struct T{ 12 LL sum; 13 bool cover; 14 }tree[M<<2]; 15 LL F[M]; 16 int Flen=92; 17 void initF(){ 18 F[0]=F[1]=1; 19 for(int i=2;i<Flen;i++){ 20 F[i]=F[i-1]+F[i-2]; 21 } 22 } 23 int Findcloseid(LL val){ 24 if(val<=1) return 0; 25 int id=lower_bound(F,F+Flen,val)-F; 26 if(abs(F[id-1]-val)<=abs(F[id]-val)) id--; 27 return id; 28 } 29 void pushup(int rt){ 30 tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum; 31 tree[rt].cover=tree[rt<<1].cover&tree[rt<<1|1].cover; 32 } 33 void build(lrrt){ 34 tree[rt].cover=0; 35 tree[rt].sum=0; 36 if(L==R) return ; 37 imid; 38 build(lson); 39 build(rson); 40 } 41 void add(int x,int val,lrrt){ 42 if(L==R){ 43 tree[rt].sum+=val; 44 int id=Findcloseid(tree[rt].sum); 45 tree[rt].cover=(F[id]==tree[rt].sum); 46 return ; 47 } 48 imid; 49 if(mid>=x) add(x,val,lson); 50 else add(x,val,rson); 51 pushup(rt); 52 } 53 void update(int x,int y,lrrt){ 54 if(L==R){ 55 tree[rt].cover=true; 56 tree[rt].sum=F[Findcloseid(tree[rt].sum)]; 57 return ; 58 } 59 imid; 60 if(x<=L&&R<=y){ 61 if(tree[rt].cover) return ; 62 update(x,y,lson); 63 update(x,y,rson); 64 pushup(rt); 65 return ; 66 } 67 if(mid>=x) update(x,y,lson); 68 if(mid<y) update(x,y,rson); 69 pushup(rt); 70 } 71 LL query(int x,int y,lrrt){ 72 if(x<=L&&R<=y) return tree[rt].sum; 73 imid; 74 LL ans=0; 75 if(mid>=x) ans+=query(x,y,lson); 76 if(mid<y) ans+=query(x,y,rson); 77 return ans; 78 } 79 int main(){ 80 initF(); 81 int n,m; 82 while(~scanf("%d%d",&n,&m)){ 83 build(iall); 84 while(m--){ 85 int op,x,y; 86 scanf("%d%d%d",&op,&x,&y); 87 if(op==1){ 88 add(x,y,iall); 89 } 90 else if(op==2){ 91 printf("%I64d\n",query(x,y,iall)); 92 } 93 else{ 94 update(x,y,iall); 95 } 96 } 97 } 98 return 0; 99 }