Rokua P3374 "template" Tree array 1

Source: Internet
Author: User

This article for Bo Master original key, reproduced when please inform Bo Master, and the original link in the text eye-catching position.

Title Link: https://www.luogu.org/problem/show?pid=3374

Title Description

Title, you need to perform the following two operations, known as a sequence:

1. Add a number to the X

2. Find out the amount of each number in a range and

Input/output format Input Format:

The first line contains two integers n, M, respectively, indicating the number of numbers and the total number of operations.

The second line contains n space-delimited integers, where the I number represents the initial value of item I of a sequence.

The next M line contains 3 or 4 integers, representing an operation, as follows:

Action 1: Format: 1 x k Meaning: Add x number plus K

Action 2: Format: 2 x y meaning: the and of each number in the output interval [x, y]

output Format:

The output contains several line integers, which is the result of all Operation 2.

Input/Output sample Input Sample # #:
5 51 5 4 2 31 1 32 2 51 3-11 4 22 1 4
Sample # # of output:
1416
Description

Time limit: 1000ms,128m

Data size:

For 30% data: n<=8,m<=10

For 70% data: n<=10000,m<=10000

For 100% data: n<=500000,m<=500000

Sample Description:

Therefore, the output results 14, 16

AC Code:

(studied Hzwer's board)

The tree array can be directly back, altogether only three or four lines ...

1#include <cstdio>2#include <iostream>3 using namespacestd;4 intn,m,tmp,x,y;5 inta[500005]; 6 7InlinevoidReadint&x)8 {9     Charch = GetChar (), c = ch;x =0;Ten      while(Ch <'0'|| CH >'9') c = Ch,ch =GetChar (); One      while(Ch <='9'&& CH >='0') x = (x<<1) + (x<<3) +ch-'0', ch =GetChar (); A     if(c = ='-') x =-x; - } -  theInlineintLowbit (intx) -{returnx& (-x);} -  -InlinevoidUpdateintXintnum) + { -      while(x <=N) +     { AA[X] + =num; atX + =lowbit (x); -     } - } -  -InlineintSumintx) - { in     intAns =0; -      while(X >0) to     { +Ans + =A[x]; -X-=lowbit (x); the     } *     returnans; $ }Panax Notoginseng  - intMain () the { + read (n), read (m); A      for(inti =1; I <= n;++i) the     { + read (TMP); - Update (I,TMP); $     } $      for(inti =1; I <= m;++i) -     { - Read ( tmp), read (x), read (y); the         if(TMP = =1) -         {Wuyi Update (x, y); the         } -         Else Wu         { -printf"%d\n", sum (y)-sum (X-1)); About         } $     } -     return 0; -}

Rokua P3374 "template" Tree array 1

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.