Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of C++ prefix and and difference algorithm

Shulou Source: shulou.com Published: 2022-06-01 08:10:01 10月27日 Update

This article will explain in detail the example analysis of C++ prefix and difference algorithm. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

1, prefix and

The prefix sum refers to the sum of the first n terms of a sequence, which can be understood as the sum of the first n terms of the sequence in mathematics, while the difference can be regarded as the inverse operation of the prefix sum. Some complex problems can be simplified by reasonable use of prefixes and differences.

2. What are the benefits of prefixes and algorithms?

Let's start with this question:

Enter a sequence of integers of length n. Next, enter m queries, each with a pair of l, r. For each query, the sum of the first number to the r number in the original sequence is output.

It is easy for us to come up with a violent solution, traversing the interval for summation.

The code is as follows:

Int for (int ionomer 1 Teng I)

Tags: Array integer prefix area matrix input element output two-dimensional upper left corner sequence rectangle complexity next number interval time complexity format one-dimensional Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi OPPO Reno Linux macOS NVidia