Codeforces Round #FF (Div. 2) E. Dzy Loves Fibonacci Numbers (Fibonacci theorem + line tree)

Source: Internet
Author: User

/*
Make full use of the two theorems of the Fibonacci series:
① definition f[1] = A, f[2] = b, f[n] = f[n-1] + f[n-2] (n≥3).
F[n] = b * fib[n-1] + A * fib[n-2] (n≥3), where Fib[i] is the first term of the Fibonacci sequence.
② definition f[1] = A, f[2] = b, f[n] = f[n-1] + f[n-2] (n≥3).
F[1] + f[2] + ... + f[n] = f[n + 2]-B
There is also a fact, that is, two of the above definitions of the sequence, add, still conform to f[n] = f[n-1] + f[n-2] recursive formula.
Using these two theorems, the sequence is maintained with the segment tree, and each node of the segment tree records what the first two items of this paragraph are, and the Fibonacci sequence is preprocessed.
, O (1) is able to calculate the number of the middle of each node, and the amount of each node.

*/

#include <cstdio> #include <cstring> #include <cstdlib> #include <iostream>using namespace std; #define MAXN 300005#define Lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define ll long longconst ll mod=100000000 9;ll fibo[maxn],sum[maxn<<2],a[maxn<<2],b[maxn<<2];//Play table Fibonacci that tangent sequence void Get_fibo () {fibo[1]=fibo[2]=1    ; for (int i=3;i<maxn;i++) fibo[i]= (fibo[i-1]+fibo[i-2])%mod;}    According to the formula Two get the nth number of the meager series ll GET_FB (int n,ll a1,ll B1) {if (n==1) return a1%mod;    else if (n==2) return b1%mod; else return (b1*fibo[n-1]+a1*fibo[n-2])%mod;} void push_up (int rt) {sum[rt]= (sum[rt<<1]+sum[rt<<1|1])%mod;}    void Push_down (int rt,int l,int r) {int mid= (L+R) >>1;    A[rt<<1]= (A[rt<<1]+a[rt])%mod;    B[rt<<1]= (b[rt]+b[rt<<1])%mod;    A[rt<<1|1]= (A[RT&LT;&LT;1|1]+GET_FB (MID+1-L+1,A[RT],B[RT))%mod;    B[rt<<1|1]= (B[RT&LT;&LT;1|1]+GET_FB (MID+1-L+2,A[RT],B[RT))%mod; ll k1= (GET_FB (Mid-l+1+2,a[rt],b[rt])-b[Rt]+mod)%mod;      Record the first half and LL k2= (GET_FB (R-L+1+2,A[RT],B[RT))-b[rt]-k1+mod)%mod;    Record the second half of the and sum[rt<<1]= (SUM[RT&LT;&LT;1]+K1)%mod;    sum[rt<<1|1]= (SUM[RT&LT;&LT;1|1]+K2)%mod; a[rt]=b[rt]=0;}    void build (int l,int R,int RT) {a[rt]=b[rt]=0;    if (l==r) {scanf ("%d", &sum[rt]); return;    } int m= (L+R) >>1; Build (Lson);    Build (Rson); PUSH_UP (RT);}        void Update (LL l,ll R,LL l,ll r,ll RT) {if (l<=l&&r>=r) {a[rt]= (a[rt]+fibo[l-l+1])%mod;        B[rt]= (b[rt]+fibo[l-l+2])%mod;        Sum[rt]= (SUM[RT]+GET_FB (r-l+1+2,fibo[l-l+1],fibo[l-l+2])-fibo[l-l+2]+mod)%mod;    Return    } push_down (Rt,l,r);    ll m= (l+r) >>1;    if (l<=m) update (L,r,lson);    if (r>m) update (L,r,rson); PUSH_UP (RT);}    ll query (ll l,ll R,LL l,ll r,ll RT) {if (l<=l&&r>=r) return sum[rt]%mod;    Push_down (RT,L,R); ll m= (l+r) >>1;    ll ret=0;    if (l<=m) ret+=query (L,r,lson);    if (r>m) ret+=query (L,r,rson); RetUrn Ret%mod;}    int main () {ll i,j,n,m,a1,b1,c1;    Get_fibo ();    cin>>n>>m;    Build (1,n,1);        while (m--) {scanf ("%lld%lld%lld", &AMP;A1,&AMP;B1,&AMP;C1);        if (a1==1) update (b1,c1,1,n,1);    else printf ("%lld\n", (Query (b1,c1,1,n,1) +mod)%mod); } return 0;}


Codeforces Round #FF (Div. 2) E. Dzy Loves Fibonacci Numbers (Fibonacci theorem + line tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.