CODERFOCES446C (Fibonacci series)

Source: Internet
Author: User

Title Description:

Interval increment, but each added value is Fi-l + 1,f[i] for the Fibonacci sequence, for interval and?

Consider the line tree, just beginning to use the Fibonacci sequence of the first n and, but can not be pushed out, considering each interval of the value-added sequence is a Fibonacci series, their and whether there are any characteristics of it?

Found if the first two items are a and B, then, a,b,a+b,a+2b,2a+3b,3a+5b;

The coefficient before A and B is the Fibonacci sequence (the second is the sum of the first two,

F[K] denotes the value of the K term starting with a, B, S[k] represents the top K and

F[K]=A*F[K-2]+B*F[K-1];

F[1]=1*a+0*b;

F[2]=0*a+1*b;

F[3]=f[1]*a+f[2]*b;

F[4]=f[2]*a+f[3]*b;

F[k]=f[k-2]*a+f[k-1]*b;

PP[K]=1+0+F[1]+F[2]+F[3]+...F[K-2];

QQ[K]=0+F[1]+F[2]+F[3]+...+F[K-1];

Sum :

S[K]=A*PP[K]+B*QQ[K];

This only needs to determine the a and bof each interval, the length can be calculated, then the k can be obtained , the former k items and can also be obtained;

Maintain the values of a and b for each interval ,a and b as tokens.

Writing this question is finding a deeper understanding of the processing of the marks:

What do the tags do?

1 position, the bottom of the tag below the node is not updated, the top of the tag above all have been updated,

That is, for each tag, it does not update the child nodes of its node, updating all of its parent nodes.

2 marks in the same interval can accumulate (additive type mark)

3 When the token is passed, the value of the child node is changed by the markup of the parent node, and the child node's tag is used to pass down, so the tag value of the child node is not used.

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<cstring>#include<algorithm>#defineLL Long Long#defineMOD 1000000009using namespacestd;//Segment Tree//increment each point of interval, seek interval andConst intMAXN =310000;structnode{intLT, RT; intAdda,addb; LL Val;} tree[4*MAXN]; LL A[MAXN];intn,m; LL F[MAXN]; LL PP[MAXN]; LL QQ[MAXN];voidinit () {memset (F,0,sizeof(f)); f[1]=1; f[2]=1; pp[1]=1; pp[2]=1; qq[1]=0; qq[2]=1;  for(intI=3; i<maxn;i++) {F[i]= (f[i-1]+f[i-2])%MOD; Pp[i]= (pp[i-1]+f[i-2])%MOD; Qq[i]= (qq[i-1]+f[i-1])%MOD; }}//Update downvoidPushdown (intID) {    if(Tree[id].adda! =0|| tree[id].addb!=0) {LL A, B; intleftlen= Tree[id <<1].rt-tree[id <<1].lt +1; A=tree[id].adda; b=Tree[id].addb; Tree[id<<1].adda + =A; Tree[id<<1].ADDB + =b; Tree[id<<1].adda%=MOD; Tree[id<<1].ADDB%=MOD; Tree[id<<1].val + = (((pp[leftlen]* a)%mod + (Qq[leftlen] *b)%mod)%MOD); Tree[id<<1].val%=MOD; intrightlen= Tree[id <<1|1].rt-tree[id <<1|1].lt +1; A= (((Tree[id].adda * f[leftlen+1-2])%mod + (TREE[ID].ADDB * F[leftlen +1-1])%mod)%MOD); b= (((Tree[id].adda * f[leftlen+1-2+1])%mod + (TREE[ID].ADDB * F[leftlen +1-1+1])%mod)%MOD); //A and B are subparagraphs K and k+1 respectivelytree[id<<1|1].adda + =A; Tree[id<<1|1].ADDB + =b; Tree[id<<1|1].adda%=MOD; Tree[id<<1|1].addb%=MOD; Tree[id<<1|1].val + = (((Pp[rightlen] *a)%mod + (Qq[rightlen] *b)%mod)%MOD); Tree[id<<1|1].val%=MOD; Tree[id].adda=0; TREE[ID].ADDB=0; }}//Update upvoidPushup (intID) {Tree[id].val= ((tree[id<<1].val + tree[id<<1|1].val)%MOD);}//Create a line segment treevoidBuildintLtintRtintID) {tree[id].lt=lt; Tree[id].rt=RT; Tree[id].val=0;//the initial value of each paragraph, according to the topic requirementsTree[id].adda =0; TREE[ID].ADDB=0; if(LT = =RT) {Tree[id].val=A[LT]; return; }    intMid = (LT+RT) >>1; Build (LT, Mid, id<<1); Build (Mid+1, RT, id<<1|1); Pushup (ID);}//increase the fixed value of each point within the intervalvoidADD2 (intLtintRtintIdintLeft ) {    if(LT <= tree[id].lt && RT >=tree[id].rt) {        intPlsa= Tree[id].lt-left +1; intplsb= Tree[id].lt-left +2; Tree[id].adda+=F[plsa]; TREE[ID].ADDB+=F[PLSB]; Tree[id].adda%=MOD; TREE[ID].ADDB%=MOD;        LL A, B; A=F[PLSA]; b=F[PLSB]; intlen= tree[id].rt-tree[id].lt +1; Tree[id].val+ = ((A*pp[len])%mod + (B*qq[len])%mod)%MOD; Tree[id].val%=MOD; return;    } pushdown (ID); //The most important lazy operation in the interval update is to update the tag for the next node that might be queried, and then as long as it does not affect the query.     intMid = (TREE[ID].LT+TREE[ID].RT) >>1; if(LT <=mid) Add2 (LT, RT, id<<1, left); if(Rt >mid) Add2 (LT, RT, id<<1|1, left); Pushup (ID);}//query for and within a certain intervalLL Query (intLtintRtintID) {    if(LT <= tree[id].lt && RT >=tree[id].rt)returnTree[id].val;    Pushdown (ID); //if it is not pushdown, it is calculated by writing down the markings along the way, and then calculating the sum of the top-down marks of the destination node after reaching the destination node;//And then add this node before from the bottom-up markup recursive and that is tree.val (tree.val meaning is only the node ID and its descendants node of the add operation, the node ID corresponds to the sum of all the numbers in the interval)//If you can update the tag of the next node that might be queried every time pushdown (tree.val means to perform all ID operations that affect the node ID, the sum of all the numbers in the interval of the node ID)    intMid = (TREE[ID].LT+TREE[ID].RT) >>1; LL ans=0; if(LT <=mid) ans+ = query (LT, RT, id<<1); if(Rt >mid) ans+ = query (LT, RT, id<<1|1); Ans%=MOD; returnans;}intMain () {//freopen ("Test.txt", "R", stdin);init ();  while(~SCANF ("%d%d",&n,&m)) { for(intI=1; i<=n;i++) scanf ("%d",&A[i]); Build (1N1);  for(intI=1; i<=m;i++)       {           intC,l,r; scanf ("%d%d%d",&c,&l,&R); if(c==1) {add2 (L,r,1, L); }           if(c==2) {printf ("%i64d\n", Query (L,r,1)); }       }    }    return 0;}

coderfoces446c (Fibonacci sequence)

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.