当前位置:网站首页>Prefix and (one dimension)
Prefix and (one dimension)
2022-06-28 11:57:00 【Q_ ming_ code】
The prefix and
Preface
Space complexity has a very important influence on the running time of code
effect , In the code, prefix and are properly used for maintenance
It can save time and complexity .
One 、 What is the prefix and ?
The prefix and , Popular said : For an array with some numbers , The prefix is the prefix of the array k Value of item addition , Therefore, the corresponding prefix sum is before the array k Sum of items . For an array , ask [L,R] And ( From L Items to R The sum of the values of the items ), You can use prefix and to solve .
Two 、 One dimensional prefix and explanation
The one-dimensional prefix sum is probably : There is a N An array of numbers arr[ ], On the q Times of inquiry [L,R] And .
Illustrate with examples
Array arr[ ] By 1,3,7,5,2 Form in order , On the 3 The second inquiry is
[2,4],[0,3],[3,4] And .
General solution :
For the first time : Interval and [2,4]=7+2+5=14;
The second time : Interval and [0,3]=1+3+7+5=16;
For the third time : Interval and [3,4]=5+2=7;
It would be too much trouble to add the array one by one before each query , At this point we can introduce prefixes and arrays :
sum[i]=sum[i-1]+arr[i] i>0
sum[0]=arr[0] i=0
Explain it. sum[i]==arr[0]+arr[i]+…+arr[i], namely sum[i] Namely arr Array from 0 Items to i Sum of items .
arr 1 3 7 5 2
sum 1 4 11 16 18
from sum It is much easier to solve the problem by array
For the first time : Interval and [2,4]=sum[4]-sum[1]=18-4=14=7+5+2
reason : take arr The array is divided into two pieces , One piece is made of 0 Item and the first item form the non required part ,
The other is from the 2 Items to 4 The required part of the item , The two together make up the whole sum[4],
From the first 0 The non desired part of the term and the first term can be regarded as sum[1],
Required part = whole sum[4]- Not required sum[1]
| Not required | Required |
|---|---|
| 1 3 | 7 5 2 |
Expand the scope , Open your eyes ,
Set interval and [L,R] use sum[L,R] Express
According to the first solving process , We can launch
sum[L,R]=sum[R]-sum[L-1] L>0
sum[L.R]=sum[R] L=0
The code is as follows ( Example ):
#include<iostream>
using namespace std;
const int n=5;
int sum[n];
int get_sum(int L,int R)
{
if(L!=0)
return sum[R]-sum[L-1];
else
return sum[R];
}
int main()
{
int arr[n]={
1,3,7,5,2};
sum[0]=arr[0];
for(int i=1;i<n;i++)
sum[i]=sum[i-1]+arr[i];
cout<<get_sum(2,4)<<endl;
cout<<get_sum(0,3)<<endl;
cout<<get_sum(3,4)<<endl;
return 0;
}
Code presents , Help to understand
summary
This chapter is about one-dimensional prefixes and simplifications , I passed through b Station learning , It's my own notes , I hope you can also learn .
边栏推荐
- The development and principle of the metacosmic system
- Swin, three degrees! Eth open source VRT: a transformer that refreshes multi domain indicators of video restoration
- Cannot redeclare block range variables
- 2022中国信通院首届业务与应用安全发展论坛成功召开!
- Day29 JS notes 2021.09.23
- 智联招聘基于 Nebula Graph 的推荐实践分享
- day31 js笔记 DOM下 2021.09.26
- Int~long long indicates the maximum and minimum number
- day34 js笔记 正则表达式 2021.09.29
- 2022 开源软件安全状况报告:超41%的企业对开源安全没有足够的信心
猜你喜欢

Graphics view framework for QT learning (to realize startup animation)

无法重新声明块范围变量

Still using simpledateformat for time formatting? Be careful that the project collapses!

Django -- MySQL database reflects the mapping data model to models

Necessary for beginners PR 2021 quick start tutorial, PR green screen matting operation method

Day29 JS notes 2021.09.23

Software test interview classic + 1000 high-frequency real questions, and the hit rate of big companies is 80%

.NET混合开发解决方案24 WebView2对比CefSharp的超强优势

Recommended practice sharing of Zhilian recruitment based on Nebula graph

day36 js笔记 ECMA6语法 2021.10.09
随机推荐
面试步骤的面试技巧
赛尔号抽奖模拟求期望
Day33 JS note event (Part 2) September 28, 2021
day31 js笔记 DOM下 2021.09.26
Oracle date format exception: invalid number
Is it safe to buy stocks and open an account on the account QR code of the CICC securities manager? Ask the great God for help
Day28 strict mode, string JS 2021.09.22
Join hands with cigent: group alliance introduces advanced network security protection features for SSD master firmware
Adding a new user in MySQL 5.7
Timestamp and date conversion "suggested collection"
Interview skills for interview steps
2022中国信通院首届业务与应用安全发展论坛成功召开!
6. calculation index
Scientific research - web of science retrieval skills
Web page tips this site is unsafe solution
Tidb v6.0.0 (DMR): initial test of cache table - tidb Book rush
MySQL cannot query the maximum value using the max function
6.A-B
MySql5.7添加新用户
买股票在中金证券经理的开户二维码上开户安全吗?求大神赐教