当前位置:网站首页>Acwing 797 differential
Acwing 797 differential
2022-06-12 16:17:00 【_ Liuxiaoyu】
Enter a length of n Integer sequence of .
Next input m Operations , Each operation contains three integers l,r,c, Indicates that... Will be in the sequence [l,r] Add... To each number between c.
Please output the sequence after all operations .
Input format
The first line contains two integers n and m.
The second line contains n It's an integer , Represents a sequence of integers .
Next m That's ok , Each line contains three integers l,r,c, Represents an operation .
Output format
All in one line , contain n It's an integer , Represents the final sequence .
Data range
1≤n,m≤100000,
1≤l≤r≤n,
−1000≤c≤1000,
−1000≤ The value of an element in an integer sequence ≤1000
sample input :
6 3
1 2 2 1 2 1
1 3 1
3 5 1
1 6 1
sample output :
3 4 5 3 4 2
Difference principle :
In the array a1、a2、a3 .... On , Construct a new array , bring ai = b1 + b2 + ..... + bi, At this time, the array a Prefix and array , Array b It's a differential array 
effect 1 : If you have constructed a differential array , You can be in O(n) Get the original array in the time of ( Find... Directly from the prefix and )
effect 2 : stay O(1) In time , Add a value to all the ranges of an array
In an interval 【 l, r】 Inside , All numbers plus c, If you use the common method , You need to traverse , The time complexity is O(n), If differential is used , Can be reduced to O(1).

step :
- to b[l] add c, because a An array is b The prefix of the array and , here a[l] All the following numbers are added c
- In addition, you need to give b[r + 1] subtract c, such a[r + 1] The latter will offset the former c 了 , This makes it possible to [l,r] All of the arrays in are added with c
Interval means :
How to construct a differential array
The whole prefix and array can be regarded as all 0, So the difference array is all 0, After that, the prefix and array each come to a number , The difference is to add a number to the interval . In this way, the differential array is successfully constructed .
#include <iostream>
using namespace std;
const int N = 100010;
int a[N], b[N];
int n,m;
// Analog insert construction difference array
void insert(int l, int r, int c)
{
b[l] += c, b[r + 1] -= c;
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++ ) cin >> a[i]; // Read in the original array
for(int i = 1; i <= n; i ++ ) insert(i, i, a[i]); // Insert each value , Construct differential arrays
while(m --)
{
int x, y, z;
cin >> x >> y >> z;
// Add value in the interval
insert(x, y , z);
}
// Find the prefix sum of the difference fraction group , Get the final value of the original array in the interval sum
for(int i = 1; i <= n; i ++) b[i] += b[i - 1];
for(int i = 1; i <= n; i ++) cout << b[i] << ' ';
}
边栏推荐
- Global and Chinese market for material injection 2022-2028: Research Report on technology, participants, trends, market size and share
- Saga architecture pattern: implementation of cross service transactions under microservice architecture
- < 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(三)
- C packing and unpacking
- Analysis of China's cargo transport volume, cargo transport turnover and port cargo in 2021 [figure]
- Global and Chinese market of vascular prostheses 2022-2028: Research Report on technology, participants, trends, market size and share
- 盒马,最能代表未来的零售
- 批量--04---移动构件
- UE4 常用类型转换
- 看《梦华录》上头的人都该尝试下这款抖音特效
猜你喜欢

Step by step steps to create an ABAP program with a custom screen

小飞页升级变智能修复Bug更快速了

< 山东大学软件学院项目实训 > 渲染引擎系统——辐射预计算(九)

HEMA is the best representative of future retail
![In 2021, China's lottery sales generally maintained a rapid growth, and the monthly sales generally tended to be stable [figure]](/img/dd/1bf44d284c709b6bebd4b308ba2cee.jpg)
In 2021, China's lottery sales generally maintained a rapid growth, and the monthly sales generally tended to be stable [figure]

The market share of packaged drinking water has been the first for eight consecutive years. How does this brand DTC continue to grow?

Multimix: small amount of supervision from medical images, interpretable multi task learning

Applet: how to get the user's mobile number in the plug-in

盒马,最能代表未来的零售

Step by step to create a trial version of ABAP program containing custom screen
随机推荐
国产CPLD中AG1280Q48进行开发的实践之一:思路分析
Thinking about the probability of drawing cards in the duel link of game king
Unicom Network Management Protocol block diagram
ER diagram made by StarUML based on the last student achievement management system
RTOS rt-thread裸机系统与多线程系统
连续八年包装饮用水市占率第一,这个品牌DTC是如何持续增长的?
华为设备配置CE双归属
Project training of Software College of Shandong University rendering engine system basic renderer (IV)
Global and Chinese market of vascular prostheses 2022-2028: Research Report on technology, participants, trends, market size and share
puppeteer入门之 Page 类
Interview: what are shallow copy and deep copy?
Browsercontext class of puppeter
Analysis on the current situation of China's antiarrhythmic drug industry in 2021: domestic R & D is further [figure]
同源?跨域?如何解决跨域?
C packing and unpacking
The common hand, the original hand and the excellent hand from the sum of Fibonacci sequence
Global and Chinese markets of three-phase induction motors 2022-2028: Research Report on technology, participants, trends, market size and share
acwing 797 差分
Solution to idea Chinese prism garbled code error -- console Chinese output prism garbled code
Saga architecture pattern: implementation of cross service transactions under microservice architecture