1080 line segment tree exercise single-point modification and interval query, 1080 Line Segments

Source: Internet
Author: User

1080 line segment tree exercise single-point modification and interval query, 1080 Line Segments
1080 line segment tree exercises

 

Time Limit: 1 s space limit: 128000 KB title level: Diamond Title Description Description

There are N squares in a row. At the beginning, each grid contains an integer. Now, I want to dynamically raise some questions and modify them: the form of a question is to find the sum of all elements in a specific subinterval [a, B; the modified rule is to specify A lattice x and add or subtract A specific value. Now you are required to answer each question correctly. 1 ≤ N <100000, total number of questions and modifications m <10000.

Input description Input Description

The first row of the input file is an integer N, followed by n integers in n rows, representing the original integer in the grid. The next positive integer is m, followed by m, which indicates m queries. The first integer indicates the query code and the query code 1 indicates the increase, the two numbers x and A indicate that A is added to the value at the position X. The query Code 2 indicates the sum of the intervals. The two integers a and B indicate that [, b.

Output description Output Description

A total of m rows, each integer

Sample Input Sample Input

6

4

5

6

2

1

3

4

1 3 5

2 1 4

1 1 9

2 2 6

Sample output Sample Output

22

22

Data range and prompt Data Size & Hint

1 ≤ N ≤ 100000, m ≤ 10000.

CATEGORY tag Tags click here to expand

After changing the line segment tree style, I will write it in this style.

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 using namespace std; 6 const int MAXN = 100001; 7 int n, m; 8 int ans = 0; 9 struct node10 {11 int l, r, w, f; 12} tree [MAXN * 4]; 13 void updata (int k) 14 {15 tree [k]. w = tree [k * 2]. w + tree [k * 2 + 1]. w; 16} 17 void build (int k, int ll, int rr) 18 {19 tree [k]. l = ll; tree [k]. r = rr; 20 if (tree [k]. l = tree [k]. r) 21 {22 scanf ("% d", & tree [k]. w); 23 return; 24} 25 int m = (ll + rr)/2; 26 build (k * 2, ll, m ); 27 build (k * 2 + 1, m + 1, rr); 28 updata (k); 29} 30 void point_change (int k, int p, int v) 31 {32 if (tree [k]. l = tree [k]. r) 33 {34 tree [k]. w + = v; 35 return; 36} 37 int m = (tree [k]. l + tree [k]. r)/2; 38 if (p <= m) point_change (k * 2, p, v); 39 else point_change (k * 2 + 1, p, v ); 40 updata (k); 41} 42 void ask_interval (int k, int ll, int rr) 43 {44 if (ll <= tree [k]. l & rr> = tree [k]. r) 45 {46 ans + = tree [k]. w; 47 return; 48} 49 int m = (tree [k]. l + tree [k]. r)/2; 50 if (ll <= m) 51 ask_interval (k * 2, ll, rr); 52 if (rr> m) 53 ask_interval (k * 2 + 1, ll, rr); 54} 55 int main () 56 {57 scanf ("% d", & n); 58 build (1,1, n); 59 scanf ("% d", & m); 60 for (int I = 1; I <= m; I ++) 61 {62 int how, p, v, x, y; 63 scanf ("% d", & how); 64 if (how = 1) // Add 65 {66 scanf ("% d", & p, & v); 67 point_change (1, p, v ); 68} 69 else // sum of the intervals 70 {71 scanf ("% d", & x, & y); 72 ans = 0; 73 ask_interval (1, x, y); 74 printf ("% d \ n", ans); 75} 76} 77 return 0; 78}

 

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.