当前位置:网站首页>Prefix sum and difference
Prefix sum and difference
2022-06-12 21:56:00 【0x3f3f3f3f】
Prefix and introduction
Prefix and strictly speaking, it is not an algorithm , It's a mathematical idea , Its function is to To find the sum of an interval .
One dimensional prefixes and
Prefixes and definitions :
Let the original sequence be :
Prefix and sequence s by
How to find prefix and array
Direct use for Loop recursion to find
Pay attention to the boundary :
In general , The subscript of the prefix and should start from 1 Start , And I will Defined as 0( See the next article for the reasons )
The role of prefixes and arrays
It can quickly find the sum of a section of the original array
Suppose we ask for the original array The sum of this paragraph
If violence does for The time complexity of the cycle is
But if you use prefixes and arrays, you can To find the interval sum within the time complexity of
Through the formula
prove :
because
Again because
You can see That is to say Value
The reasons for the above boundary problems
If we ask And , According to the formula, it should be
Because this and should be Is precisely Value
So you get
So the prefix and should start from 1 Start counting ( Prevent cross-border subtraction ), And put 0 The position is initialized to 0( Avoid handling special situations )
One dimensional prefix and template
Two dimensional prefixes and
One dimensional prefix sum is the sum of a range , alike , We can extend this idea to two dimensions , That is, finding the sum of a submatrix .
The two-dimensional prefix and refer to , With (x,y) Is the end of the lower right corner , The sum of all its upper left elements , As shown in the figure below , The sum of all elements in the red line area is Value 
How to find the sum of two-dimensional prefixes
Here's the picture 
Let the original sequence be , The prefix and sequence are
be The value of is equivalent to , The area enclosed by the red frame plus the area enclosed by the black frame , here , The gray part of the picture , It's been added twice , So subtract the gray area once , Then add This point itself , According to the definition of two-dimensional prefix sum, we can see , The area enclosed by the red box is subscript The corresponding prefix and , Similarly, the area enclosed by the black frame is subscript The corresponding prefix and , The gray area is subscript The corresponding prefix and
Therefore, the operation of initializing the two-dimensional prefix sum is
How to find the sum of submatrix
The sum of submatrixes refers to , With (x2,y2) Is the lower right coordinate ,(x1,y1) The sum of the matrices circled by the coordinates of the upper left corner
The gray area shown in the figure below is the submatrix to be solved 
The sum of submatrix is shown in the figure below 
The sum of the grey areas is , With Is the prefix and of the matrix of coordinates , Subtract the sum enclosed in the green box , Subtract the sum enclosed by the black frame , At this point, the purple area in the figure is subtracted twice , So add the sum of the purple areas in the above figure , This is the sum of the gray areas in the figure . The area corresponding to the green box is Value , The black area is Value , The purple area is Corresponding value
So the sum of the two-dimensional prefix and submatrix is
Introduction to difference
Difference is the inverse of prefix sum . Difference is a clever and simple way to process data , It is used to modify intervals and ask questions . Put a given set of data elements A Divided into many sections , Do many operations on these intervals , Each operation is the same addition and subtraction operation for all elements in a certain interval , If you modify each element in this interval one by one , It's very time consuming . The time complexity is among For the number of operations
So we're going to introduce differential arrays B, When modifying an interval , Just do a few operations on the difference group , This will affect the modification of the original interval , And the operation is very easy , yes Complexity . When all modification operations are completed , Then use the difference array , Calculate the new prefix and array, that is, the sequence after the operation .
One dimensional difference
One dimensional difference definition :
We set the original sequence as , The difference sequence is
The original sequence :
structure :
bring
At this time we call it by The difference between , by And
So as long as we have Array , You can go to Under the complexity of Array , That is, find the prefix and operation
So we can easily get
One dimensional difference solution to the problem
The topic will give us a lot of operation , Let us in Plus or minus a constant , Ask us the sequence after several operations
If violence does , The time complexity of a single operation is undoubtedly Of , Then we will construct a group of difference fractions to find , The complexity of the differential operation is
Operation for
Because if add , When finding prefix and sequence , because , as well as All subsequent prefixes and must be counted This point , So it's equivalent to adding the prefix to the array
and , When finding prefix and sequence , Similarly, from Every element after is subtracted , namely .
In this way, these two operations are equivalent to , That's what the title asks for
Construction of one-dimensional difference
At first we can assume that The array is all 0, Then the differential array All for 0, But at this time Array has value , We can see it as a process The first modification operation
For the first time Interval plus
The second time was in Interval plus
All the way to Interval plus
prove :
If we assume to give Interval plus , amount to
to Interval plus , amount to
to Interval plus , amount to
After the above operations, we can see
b[1] = a[1]
b[2] = a[2] - a[1]
b[3] = a[3] - a[2]
Conform to the construction of the difference group , So after n After the first operation , You can get the difference array
Differential template
Two dimensional difference
alike , The two-dimensional difference here can be compared with the two-dimensional prefix and , Is to know a matrix , We are going to construct a difference matrix , bring , Save all Sum of all elements in the upper left matrix . namely Matrix is Matrix prefix and matrix . Similarly, the construction of two-dimensional difference can also be compared with one-dimensional difference , First assume that it can be seen as a The elements in the matrix are all 0, be b The elements in the matrix are all 0, So if a The elements in the matrix are not 0 了 , It can be seen as Insert an element , It is too troublesome to prove the two-dimensional difference structure , It is proved that one can write by analogy with the structure of one-dimensional difference .
The problem solved by two-dimensional difference
Give Is the coordinate of the upper left corner , With Add or subtract a constant for each element in the submatrix of the coordinates of the lower right corner .
If violence does it , Because you have to cycle through the elements of the entire matrix , So the time complexity is zero .
But if we construct a difference matrix to do it , You can go to Add a constant to the submatrix within the time complexity of . Then we can find out the original matrix ( Do two-dimensional prefix and operation ).
Operation for
prove
As shown in the figure below 
If we give Plus a constant Words , So all the elements in the lower right corner of this point , That is, all elements in the area enclosed by the red line in the figure will be added with a constant , But we just need to add a constant to the gray area of the graph , therefore , We can see , Subtract the area enclosed by the yellow line and the area enclosed by the dark blue line in the figure , The pink area in the picture has been subtracted once more , So add the pink area in the above figure , At this point, the result is that all the elements in the grey matrix are added with constants . According to the difference definition , We can easily see , The area enclosed by the yellow line is affected by the difference matrix influence , Dark blue areas are affected by Influence , The pink area is affected by influence , Therefore, the above operation can be obtained
Two dimensional difference template
Three dimensional difference
Three dimensional difference template code is relatively rare . Three dimensional difference is complex , And it will not appear in the general written examination or competition , Therefore, we are here only as an understanding content , You don't have to care . If you encounter , It can be generalized according to two-dimensional difference .
The value of the element is a three-dimensional array To define , Difference array It's also three-dimensional . Imagine three-dimensional difference as an operation in three-dimensional space . A one-dimensional interval is a line segment , Two dimensions are rectangles , So three-dimensional is a three-dimensional block . A small solid block has vertices , So three-dimensional interval modification , Need modification individual value . It can be seen by analogy with the following figure ( The picture is from csdn)
Analogy to two-dimensional difference , The operation of three-dimensional difference is to add or subtract a constant to all elements in a cube
We give the operation directly
Note reference
Acwing Basic algorithm course
边栏推荐
- PCB封装下载网站推荐及其详细使用方法
- 【QNX Hypervisor 2.2 用户手册】4.3 获取host组件
- June training (day 12) - linked list
- Ansible roles project case (IV)
- June training (day 10) - bit operation
- Jdbctemplate inserts and returns the primary key
- Ansible foundation and common modules (I)
- Build a highly available database
- SQL调优指南笔记14:Managing Extended Statistics
- [qnx hypervisor 2.2 manuel de l'utilisateur] 4.2 environnement de construction pris en charge
猜你喜欢

SQL调优指南笔记18:Analyzing Statistics Using Optimizer Statistics Advisor

User guide for JUC concurrency Toolkit

孙老师版本JDBC(2022年6月12日21:34:25)

SQL tuning guide notes 8:optimizer access paths

Jin AI her power | impact tech, she can

SQL调优指南笔记6:Explaining and Displaying Execution Plans

SQL tuning guide notes 17:importing and exporting optimizer statistics

Graphics2d class basic use

Icml2022 | Galaxy: apprentissage actif des cartes de polarisation

Smart management of green agriculture: a visual platform for agricultural product scheduling
随机推荐
Ansible summary (VI)
SQL tuning guide notes 10:optimizer statistics concepts
How to abstract a problem into a 0-1 knapsack problem in dynamic programming
[sword finger offer] sword finger offer 58 - ii Rotate string left
Unity 常用3D数学计算
My struggle: my years in foreign enterprises (1)
PE安装win10系统
Can tonghuashun open an account? Is it safe to open an account in tonghuashun? How to open a securities account
Unity commonly used 3D mathematical calculation
How do I create a daemon thread? And where to use it?
Oracle LiveLabs实验:Introduction to Oracle Spatial Studio
How to implement a simple publish subscribe mode
【QNX Hypervisor 2.2 用户手册】4.4 构建Host
VagrantBox重新安装vboxsf驱动
linux备份mysql
脱颖而出!OceanBase 入选 2021“科创中国”开源创新榜单
How to develop programming learning with zero foundation during college
A puzzle about + =
SQL tuning guide notes 16:managing historical optimizer statistics
June training (day 11) - matrix