One-dimensional array of arrays modified, query

Source: Internet
Author: User

Template Problem CODEVS-1082

Give you the number of n, there are two operations:

1: Add x for all numbers of interval [a, b]

2: Ask for the number of intervals [A, b] and.

The first line is a positive integer n, and the next n rows n integers,

Then the next positive integer q, each line represents the number of operations,

If the first number is 1, followed by 3 positive integers,

Indicates that each number in the interval [a, b] is increased by x, if it is 2,

Represents the number of the and of the action 2 ask interval [A, b].

A one-dimensional tree array can be considered to be differential, but not to be extended to two dimensions.

So we make the increment of di= (Ai~an)

The idea is the same as the difference, can be extended to two-dimensional (not now understand)

#include <iostream>#include<cstdio>#include<algorithm>#defineRep (i,a,b) for (int i=a;i<=b;i++)using namespacestd;#defineLowbit (x) x& (-X)intn,m,a[1000005],c1[1000005],c2[1000005];voidUpdateintVintval) {     for(intpp=v;pp<=n;c1[pp]+=val,c2[pp]+=val*v,pp+=lowbit (PP));}intQueryintLintR) {    intans=0;  for(intPp=r;pp>0; ans+= (r+1) *c1[pp],ans-=c2[pp],pp-=lowbit (PP)); L--;  for(intPp=l;pp>0; ans-= (L +1) *c1[pp],ans+=c2[pp],pp-=lowbit (PP)); returnans;}intMain () {scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++) scanf ("%d",&A[i]);  for(intI=1; i<=n;i++) Update (I,a[i]-a[i-1]);  for(intI=1; i<=m;i++)    {        intP,l,r,val; scanf (" %d%d%d",&p,&l,&R); if(p==1) {scanf ("%d",&val);            Update (L,VAL); Update (R+1,-val); }Else{printf ("%d\n", Query (l,r)); }    }                return 0;}

One-dimensional array of arrays modified, query

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.