P3368 [TEMPLATE] tree array 2 (tree array maintains the differential sequence), p3368 tree

Source: Internet
Author: User

P3368 [TEMPLATE] tree array 2 (tree array maintains the differential sequence), p3368 tree
Description

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

1. Add x to each number in a certain range

2. Obtain the sum of a certain number

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 2 or 4 integers, indicating an operation, as shown below:

Operation 1: Format: 1 x y k meaning: Add k to each number in the range [x, y]

Operation 2: Format: 2 x meaning: output the value of number x

 

Output Format:

 

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

 

Input and Output sample input sample #1:
5 51 5 4 2 31 2 4 22 31 1 5 -11 3 5 72 4
Output sample #1:
610
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, the output result is 6 and 10.

 

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 using namespace std; 6 int lowbit (int x) 7 {return x &-x;} 8 void read (int & n) 9 {10 char c = '+'; int x = 0; bool flag = 0; 11 while (c <'0' | c> '9') 12 {c = getchar (); if (c = '-') flag = 1 ;} 13 while (c> = '0' & c <= '9') 14 {x = x * 10 + (c-48), c = getchar ();} 15 flag = 1? N =-x: n = x; 16} 17 const int MAXN = 500001; 18 int c [MAXN], n, m, p, x, y, z, pre; 19 void add (int p, int v) 20 {21 while (p <= n) 22 {23 c [p] + = v; 24 p + = lowbit (p ); 25} 26} 27 int ask (int p) 28 {29 int ans = 0; 30 while (p> 0) 31 {32 ans + = c [p]; 33 p-= lowbit (p); 34} 35 return ans; 36} 37 int main () 38 {39 read (n); read (m ); 40 for (int I = 1; I <= n; I ++) 41 {42 read (p); 43 add (I, p-pre); 44 pre = p; 45} 46 47 while (m --) 48 {49 read (p); 50 if (p = 1) // The interval is added with 51 {52 read (x ); read (y); read (z); 53 add (x, z); 54 add (y + 1,-z ); 55} 56 else // Single Point query 57 {58 read (x); 59 printf ("% d \ n", ask (x); 60} 61} 62 return 0; 63}

 

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.