codevs1080 Line segment Tree Exercises

Source: Internet
Author: User

1080 Line tree exercise (http://codevs.cn/problem/1080/)

time limit: 1 sspace limit: 128000 KBtitle level: Diamonds DiamondTitle 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

Data range and Tips Data Size & Hint

1≤n≤100000, m≤10000.

#include <cstdio>Const intmaxn=100001;intA[maxn],cur=0;structtreetype{intLeft,right; intlptr,rptr; intsum;} tree[2*MAXN];voidBuildtree (intllintRR) {    inttot=++cur; Tree[tot].left=ll; Tree[tot].right=RR; if(ll!=rr-1) {tree[tot].lptr=cur+1;//record the number of the left and right son (location)Buildtree (ll, (LL+RR)/2); Tree[tot].rptr=cur+1; Buildtree (LL+RR)/2, RR); Tree[tot].sum=tree[tree[tot].lptr].sum+tree[tree[tot].rptr].sum; }    Elsetree[tot].sum=a[ll];}voidChangeintKintCintDelta) {    if(tree[k].left==tree[k].right-1) Tree[k].sum+=Delta; Else       {         if(c< (tree[k].left+tree[k].right)/2) Change (Tree[k].lptr,c,delta); if(c>= (tree[k].left+tree[k].right)/2) Change (Tree[k].rptr,c,delta); Tree[k].sum=tree[tree[k].lptr].sum+tree[tree[k].rptr].sum; }}intQueryintKintllintRR) {    if(Ll<=tree[k].left&&rr>=tree[k].right)returntree[k].sum; intans=0;//defined inside the function to prevent the next layer from being searched, the value of the previous layer is discarded, which is equivalent to redefining a new variable at each level    if(ll< (tree[k].left+tree[k].right)/2) ans+=query (TREE[K].LPTR,LL,RR); if(Rr> (tree[k].left+tree[k].right)/2) ans+=query (TREE[K].RPTR,LL,RR); returnans;}intMain () {intN; scanf ("%d",&N);  for(intI=1; i<=n;i++) scanf ("%d",&A[i]); Buildtree (1, n+1); scanf ("%d",&N);  for(intI=1; i<=n;i++)      {           intx, Y, Z scanf ("%d%d%d",&x,&y,&z); if(x==1) Change (1, y,z); if(x==2) printf ("%d\n", Query (1, y,z+1)); }    return 0;}

codevs1080 Line segment Tree Exercises

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.