P3374 [TEMPLATE] tree array 1 single point modification and interval query, p3374 tree

Source: Internet
Author: User

P3374 [TEMPLATE] tree array 1 single point modification and interval query, p3374 tree
Description

For example, if you know a sequence, you need to perform the following two operations:

1. Add x to a certain number

2. Obtain the sum of each number in a certain range.

Input/Output Format Input Format:

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

The second row contains N integers separated by spaces. the I-th digit indicates the initial value of the I-th Column.

Each row in the next M row contains 3 or 4 integers, indicating an operation, as shown below:

Operation 1: Format: 1 x k meaning: Add k to the number x

Operation 2: Format: 2 x y Meaning: Sum of each number in the output range [x, y]

Output Format:

The output contains several integer rows, that is, the result of all operation 2.

Input and Output sample Input example #1:
5 51 5 4 2 31 1 32 2 51 3 -11 4 22 1 4
Output sample #1:
1416
Description

Time-Space limit: 1000 ms, 128 M

Data scale:

For 30% of data: N <= 8, M <= 10

For 70% of data: N <= 10000, M <= 10000

For 100% of data: N <= 500000, M <= 500000

Example:

Therefore, output results 14 and 16

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 using namespace std; 5 const int MAXN = 500001; 6 int n, m, p; 7 int tree [MAXN]; // 8 int lowbit (int p) 9 {10 return p & (-p); 11} 12 void point_increase (int w, int v) 13 {14 for (int I = w; I <= n; I = I + lowbit (I) 15 tree [I] = tree [I] + v; 16 return; 17} // Add the added value to 18 int interval_ask (int x) 19 {20 int ans = 0; 21 for (int I = x; I! = 0; I = I-lowbit (I) 22 ans = ans + tree [I]; 23 return ans; 24} // obtain a prefix and return 25 int main () 26 {27 scanf ("% d", & n, & m ); 28 for (int I = 1; I <= n; I ++) 29 {30 scanf ("% d", & p); 31 point_increase (I, p ); 32} // initialize each vertex to manage its own management vertex 33 for (int I = 1; I <= m; I ++) 34 {35 scanf ("% d", & p); 36 if (p = 1) // Add 37 {38 int x, y; 39 scanf ("% d", & x, & y); 40 point_increase (x, y); 41} 42 else // sum 43 {44 int x, y; 45 scanf ("% d", & x, & y); 46 printf ("% d \ n", interval_ask (y)-interval_ask (x-1 )); 47} // because the tree array can only obtain the prefix and so the two areas need to be added 48} 49 return 0; 50}

 

Related Article

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.