Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of C language Tree Array and Line Segment Tree

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "C language tree array and line segment tree example analysis". In daily operation, I believe that many people have doubts about C language tree array and line tree case analysis. I have consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "C language tree array and line segment tree case analysis". Next, please follow the editor to study!

Dynamic calculation of continuous interval sum of tree array

Given a sequence of n numbers, it is specified that there are two operations, one is to modify an element, and the other is to find the continuous sum of the subsequence [a].

Input format

The first line contains two integers n and m, representing the number of numbers and the number of operations, respectively.

The second row contains n integers, representing the complete sequence.

The next m lines, each containing three integers, kjingo, represents the sum of the subsequence [aformab], and kend1, which represents the a plus b).

The sequence starts at 1.

Output format

Output several rows of numbers that represent the continuous sum of the corresponding subsequence [a _ r _ b] when kicking 0.

Data range

1 ≤ n ≤ 100000

1 ≤ m ≤ 100000

1 ≤ a ≤ b ≤ n

The data ensures that the sum of all the elements in the sequence is within the int range at all times.

Enter a sample:

10 5

1 2 3 4 5 6 7 8 9 10

1 1 5

0 1 3

0 4 8

1 7 5

0 4 8

Sample output:

eleven

thirty

thirty-five

My initial idea for this problem was to use only prefixes and sums, but later found that using only prefixes and sums would time out because of the data range

1 ≤ n ≤ 100000

1 ≤ m ≤ 100000

1 ≤ a ≤ b ≤ n

/ prefix and will time out # includeusing namespace std;const int N = 1000010 leading int S1 [N], S2 [N]; int main () {cin > > n > > m; for (int I = 1; I > S1 [I]; S2 [I] = S2 [I-1] + S1 [I];} while (MYR -) {int k, a, b; cin > k > a > b If (k = = 1) {S1 [a] + = b; for (int I = 1; I 0polii Muslim lowbit (x)) res+=c [I]; return res

How to make a single point of modification through a tree array?

Here we give a conclusion: the parent node of a node a [I] or c [I] is c [I + lowbit (I)]

So when we change the value of a [I], we can recursively update the value of the parent node.

Code:

A [x] + = v for (int iSext.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report