Wikioi 1080 Segment Tree Practice tree Array

Source: Internet
Author: User

1080 line tree exercises

Time limit: 1 s space limit: 128000 KB title level: Diamonds Diamond Title Description Description

A row of n squares, beginning with an integer in each lattice. Some questions and modifications are presented dynamically: the form of questioning is the summation of all elements in a particular sub-interval [a, b]; the modified rule is to specify a lattice x, plus or minus a specific value, a. You are now asked to make the right answer to each question. 1≤n<100000, the total number of questions and modifications m<10000.

Enter a description Input Description

The input file first behaves as an integer n, followed by n rows n integers, representing the original integers in the lattice. followed by a positive integer m, followed by M-line, the M-query, the first integer to indicate the query code, the number 1 indicates an increase, the following two numbers x and a for the value on position x to increase a, ask the code 2 for the interval sum, followed by two integers for A and B, representing the interval between [A, a] and

Output description Output Description

A total of M lines, each integer

Sample input Sample Input

6

4

5

6

2

1

3

4

1 3 5

2 1 4

1 1 9

2 2 6

Sample output Sample Output

22

22

Single point update, interval query

#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>typedefLong Longll;using namespacestd;#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 100004intN;intD[MAXN];intLowbit (intX//seek the jurisdiction of a certain point{    returnx& (-x);}voidUpdate1 (intXintNum//single-point modification makes the D[x]+num{     while(x<=N) {d[x]+=num; X+=lowbit (x); }}voidUpdate2 (intXintNum//interval modification allows a[1] to be modified to a[x]{     while(x>0) {D[x]+=num; X-=lowbit (x); }}intGETSUM1 (intX//interval sum expression a[1] always add to a[x]{    ints=0;  while(x>0) {s+=D[x]; X-=lowbit (x); }    returns;}intMain () {memset (d,0,sizeof(d)); scanf ("%d",&N); intA,b,c;  for(intI=1; i<=n;i++) {scanf ("%d",&b);    Update1 (I,B); }    intm; scanf ("%d",&m);  while(m--) {scanf ("%d%d%d",&a,&b,&c); if(a==1) update1 (B,C); d[0]=0; if(a==2) printf ("%d\n", GetSum1 (c)-getsum1 (b-1)); }    return 0;}

Wikioi 1080 Segment Tree Practice tree Array

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.