当前位置:网站首页>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
边栏推荐
- Ansible-大总结(六)
- 动态规划之如何将问题抽象转化为0-1背包问题(详解利用动态规划求方案数)
- talent showing itself! Oceanbase was selected into the 2021 "sci tech innovation China" open source innovation list
- SQL tuning guide notes 14:managing extended statistics
- Role of volatile keyword
- How do I create a daemon thread? And where to use it?
- SQL tuning guide notes 16:managing historical optimizer statistics
- SQL调优指南笔记15:Controlling the Use of Optimizer Statistics
- NPOI 创建Word
- Oracle LiveLabs实验:Introduction to Oracle Spatial Studio
猜你喜欢

Ansible summary (VI)

MySQL体系结构及基础管理(二)

ICML2022 | GALAXY:极化图主动学习

Ansible基础和常用模块(一)

如何自己动手写一个vscode插件,实现插件自由!

SQL调优指南笔记16:Managing Historical Optimizer Statistics

SQL tuning guide notes 17:importing and exporting optimizer statistics

关于 安装Qt5.15.2启动QtCreator后“应用程序无法正常启动0xc0000022” 的解决方法

Leetcode: the maximum number of building change requests that can be reached (if you see the amount of data, you should be mindless)

About the solution to "the application cannot start normally 0xc00000022" after qt5.15.2 is installed and qtcreator is started
随机推荐
DRF receives nested data and creates objects. Solution: DRF not NULL constraint failed
Jdbctemplate inserts and returns the primary key
六月集训(第12天) —— 链表
SQL tuning guide notes 9:joins
A puzzle about + =
在同花顺开户证券安全吗,证券开户怎么开户流程
Thread safe level
脱颖而出!OceanBase 入选 2021“科创中国”开源创新榜单
Turing prize winner: what should I pay attention to if I want to succeed in my academic career?
June training (day 12) - linked list
大学期间零基础如何开展编程学习
logstash时间戳转换为unix 纳秒nano second time
User guide for JUC concurrency Toolkit
Build a highly available database
最近公共祖先问题你真的学会了吗?
What is embedded
CVPR 2022 | 应对噪声标签,西安大略大学、字节跳动等提出对比正则化方法
疼痛分级为什么很重要?
How to write a vscode plug-in by yourself to realize plug-in freedom!
SQL调优指南笔记14:Managing Extended Statistics