Luogu P1438 boring series, Luogu p1438 Series

Source: Internet
Author: User

Luogu P1438 boring series, Luogu p1438 Series

Https://www.luogu.org/problemnew/show/1438

Lazytag records the first item and the tolerances of the arithmetic difference series to be added to a certain range. Given that the interval length is known (r-l + 1), we can only introduce the number to be added to each number in this interval.

We can find that each of the two arithmetic difference columns is added, and the result is an arithmetic difference column, and the first item and the tolerances are added respectively.

For interval decomposition (marked lower pass), use the formula for calculating the total/sum/number of items in the arithmetic difference series. The first item and tolerance that should be added to the parent range can be used to introduce the first item and tolerance that should be added to the subrange, which is about to be the same as the tolerance, the first item of the two subintervals is the first item of the parent interval and the first item of the parent Interval Series (to be pushed ).

In fact, it can be changed to interval query.

Because only single-point query is required, a simpler method is to record only the differential series of the original series.

Long time reason: Incorrect hands too many times/too slow

 1 #include<cstdio> 2 #include<iostream> 3 #define ls (num<<1) 4 #define rs (num<<1|1) 5 #define mid ((l+r)>>1) 6 typedef long long LL; 7 LL t[800100],kk[800100],dd[800100],a[200100]; 8 LL L,R,k,d,n,m; 9 void pushdown(LL l,LL r,LL num)10 {11     if(kk[num]!=0||dd[num]!=0)12     {13         t[ls]+=((2*kk[num]+dd[num]*(mid-l))*(mid-l+1)/2);14         t[rs]+=((2*kk[num]+dd[num]*(mid-l+1+r-l))*(r-mid)/2);15         kk[ls]+=kk[num];16         dd[ls]+=dd[num];17         kk[rs]+=(kk[num]+dd[num]*(mid-l+1));18         dd[rs]+=dd[num];19         kk[num]=0;dd[num]=0;20     }21 }22 void build(LL l,LL r,LL num)23 {24     if(l==r)25     {26         t[num]=a[l];27         return;28     }29     build(l,mid,ls);30     build(mid+1,r,rs);31     t[num]=t[ls]+t[rs];32 }33 void change(LL l,LL r,LL num)34 {35     if(L<=l&&r<=R)36     {37         t[num]+=((2*(k+d*(l-L))+d*(r-l))*(r-l+1)/2);38         kk[num]+=(k+d*(l-L));39         dd[num]+=d;40         return;41     }42     pushdown(l,r,num);43     if(L<=mid)    change(l,mid,ls);44     if(mid<R)        change(mid+1,r,rs);45     t[num]=t[ls]+t[rs];46 }47 LL query(LL l,LL r,LL num)48 {49     if(L<=l&&r<=R)50         return t[num];51     pushdown(l,r,num);52     LL ans=0;53     if(L<=mid)    ans+=query(l,mid,ls);54     if(mid<R)    ans+=query(mid+1,r,rs);55     return ans;56 }57 int main()58 {59     LL i,tt;60     scanf("%lld%lld",&n,&m);61     for(i=1;i<=n;i++)62         scanf("%lld",&a[i]);63     build(1,n,1);64     while(m--)65     {66         scanf("%lld",&tt);67         if(tt==1)68         {69             scanf("%lld%lld%lld%lld",&L,&R,&k,&d);70             change(1,n,1);71         }72         else73         {74             scanf("%lld",&L);R=L;75             printf("%lld\n",query(1,n,1));76         }77     }78     return 0;79 }

Related Article

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.