UESTC 1073 Qiushi brother and Line segment tree (segment Tree---Time-saving achievements)

Source: Internet
Author: User

Title Link: http://acm.uestc.edu.cn/#/problem/show/1073

"Learn this bottomless, forward Mo." "Qiushi brother to the side play cell phone learning brother said."

Qiushi is a person who loves to learn, today he just learned the data structure of line segment tree.

In order to test their mastery degree, Qiushi eldest brother gave himself a problem, at the same time invited everyone to do.

Qiushi's subject asks you to maintain a sequence that supports two actions: one is to modify the value of an element, and one is to ask for an interval.

Input

The first line contains an integer n, which represents the length of the sequence.

The next line contains n integers a I, which represents the initial element of the sequence.

The next line contains an integer m, which represents the operand.

Next m line, each line is one of the following two actions:

1 x V: Indicates that the value of the X element is changed to V2 L R: the element that asks [l,r] this interval and

1≤n,m,v,a i≤100000, 1≤l≤r≤n.

Output

For each 2 2 L r operation, the output is an integer representing the corresponding answer.

Sample Input and output

31 2 332 1 21 1 52 1 2
37
Test instructions: Basic line tree Build, update, find operation.
Note: The update is an operation on a single leaf node, not an interval, and if you build it, recursively building all the trees will time out.
So the method to be taken is where the leaf node appears, before it is established.
Code:
#include <stdio.h>#include<string.h>#include<algorithm>#defineN 100007using namespacestd;Long LongSum[n *Ten];voidBuildintTintLintRintValinti) {    if(t = = L && L = =R) {Sum[i]= (Long Long) Val; return ; }    intMid = (L + R) >>1; if(T <=mid) Build (T, L, Mid, Val, I<<1); Else if(T >mid) Build (T, Mid+1, R, Val, i<<1|1); Sum[i]= sum[i<<1] + sum[i<<1|1];}Long LongQueryintLintRintLintRinti) {    if(L = = L && r = =R)returnSum[i]; intMid = (L + R) >>1; if(R <=mid)returnQuery (L, R, L, Mid, i<<1); Else if(L >mid)returnQuery (L, R, Mid +1, R, i<<1|1); Else    {        returnQuery (L, Mid, L, Mid, i<<1) + Query (Mid +1, R, Mid +1, R, i<<1|1); } Sum[i]= sum[i<<1] + sum[i<<1|1];}intMain () {intN, M;  while(SCANF ("%d", &n)! =EOF) {        inttemp;  for(inti =0; I < n; i++) {scanf ("%lld", &temp); Build (I+1,1, N, temp,1); } scanf ("%d", &m);  for(inti =0; I < n; i++)        {            intorder, A, B; scanf ("%d%d%d", &order, &a, &b); if(Order = =1) {Build (A,1, N, B,1); }            Else{printf ("%lld\n", query (A, B,1N1)); }        }    }    return 0;}

UESTC 1073 Qiushi brother and Line segment tree (segment Tree---Time-saving achievements)

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.