Implementation of differential fractional groups

Source: Internet
Author: User

The function of the difference fractional group is to modify the interval, query the point

The time complexity of the modified interval is O (1), and the time complexity of the query point is O (n)

We have to choose the method flexibly according to the data range, not to be stuck in the difference score Group

void Update (int x,int y,int  z) {    b[x]+ =Z;    B[y+1]-=z;}

The above is to modify the interval operation, x position plus the amount of modification, y+1 position minus the amount of modification, so that the entire interval element is equivalent to modify the

int sum (int  x) {    int ans=0;      for (int i=1; i<=x;i++)        ans+ =b[i]    ; return ans;}

It was convenient, but I need to add it all at the time of the inquiry.

And when it comes to preprocessing.

    b[1]=a[1];      for (int i=2; i<=n;i++)        b[i]=a[i]-a[i-1];

The following is a complete implementation code:

1 //aininot2602 //modify interval, query point3#include <iostream>4#include <cstring>5 using namespacestd;6 Const intmaxn=100005;7 Const intmaxq=100005;8 intA[MAXN];9 intB[MAXN];Ten intn,q; One voidUpdateintXintYintz) A { -b[x]+=Z; -b[y+1]-=Z; the } - intSumintx) - { -     intans=0; +      for(intI=1; i<=x;i++) -ans+=B[i]; +     returnans; A } at intMain () - { -Cin>>N; -      for(intI=1; i<=n;i++) -Cin>>A[i]; -b[1]=a[1]; in      for(intI=2; i<=n;i++) -b[i]=a[i]-a[i-1]; toCin>>Q; +      while(q--) -     { the         intx; *Cin>>x; $         if(x==1)Panax Notoginseng         { -             inty,z,w; theCin>>y>>z>>W; + Update (Y,Z,W); A         } the         if(x==2) +         { -             inty; $Cin>>y; $Cout<<sum (y) <<Endl; -         } -     } the     return 0; -}

Implementation of differential fractional groups

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.