Codevs 1081 Segment Tree Exercise 2 Interval Update single point query no lazy

Source: Internet
Author: User

Title Description Description

Give you the number of n, there are two operations


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


2: What is the number of questions I have?

Enter a description Input Description

The first line is a positive integer n, the next n rows n integers, and then a positive integer q, which represents the number of operations. The next Q line is a number of integers per line. If the first number is 1, followed by 3 positive integer a,b,x, indicates that each number in the interval [a, b] increases by X, if it is 2, followed by 1 integer i, which indicates the number of the first position to ask.

Output description Output Description

One answer for each query output line

Sample input Sample Input

3

1

2

3

2

1 2 3 2

2 3

Sample output Sample Output

5

Data range and Tips Data Size & Hint

Data range

1<=n<=100000

1<=q<=100000

Test instructions: Chinese test instructions: Bare section tree Interval Update single point query no lazy
1 //code by drizzle2#include <iostream>3#include <cstring>4#include <cstdio>5#include <algorithm>6#include <vector>7 #definell __int648 #definePI ACOs (-1.0)9 #defineMoD 1000000007Ten using namespacestd; One structnode A { -     intL,r,value; -}tree[400005]; the voidBuildtree (intRootintLeftintRight ) - { -Tree[root].l=Left ; -Tree[root].r=Right ; +     if(left==Right ) -     { +scanf"%d",&tree[root].value); A         return ; at     } -     intMid= (left+right) >>1; -Buildtree (root<<1, left,mid); -Buildtree (root<<1|1, mid+1, right); -tree[root].value=tree[root<<1].value+tree[root<<1|1].value; - } in voidUpdata (intCintLeftintRightintroot) - { to     if(tree[root].l==left&&tree[root].r==right&&left==Right ) +     { -tree[root].value+=C; the         return; *     } $     intMid= (TREE[ROOT].L+TREE[ROOT].R) >>1;Panax Notoginseng     if(right<=mid) -Updata (c,left,right,root<<1); the     Else +     { A         if(left>mid) theUpdata (c,left,right,root<<1|1); +         Else -         { $Updata (c,left,mid,root<<1); $Updata (c,mid+1,right,root<<1|1); -         } -     } thetree[root].value=tree[root<<1].value+tree[root<<1|1].value; - }Wuyi intQueryintLeft,intRightintroot) the { -     if(tree[root].l==left&&tree[root].r==Right ) Wu      { -          returnTree[root].value; About       } $     intMid= (TREE[ROOT].L+TREE[ROOT].R) >>1; -     if(right<=mid) -     returnQuery (left,right,root<<1); -     Else A     { +         if(left>mid) the         returnQuery (left,right,root<<1|1); -         Else $         returnQuery (left,mid,root<<1) +query (mid+1,right,root<<1|1); the     } the } the intN; the intEXM; - intQ; in intFlag; the intLll,rrr,add; the intcha; About intMain () the { the      while(SCANF ("%d", &n)! =EOF) the     { +Buildtree (1,1, n); -scanf"%d",&q); the          for(intI=1; i<=q;i++)Bayi         { thescanf"%d",&flag); the             if(flag==1) -             { -scanf" %d%d%d",&lll,&rrr,&add); theUpdata (Add,lll,rrr,1); the             } the             if(flag==2) the             { -scanf"%d",&cha); theprintf"%d\n", Query (Cha,cha,1)); the             } the         }94     } the     return 0; the}

Codevs 1081 Segment Tree Exercise 2 Interval Update single point query no lazy

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.