当前位置:网站首页>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] << ' ';
}
边栏推荐
- [thinking about the process of structure optimization] how to build the evaluation ability of the impact of technical solutions
- The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.6 实例(目录列表)
- Chapter I linear table
- The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.4 随机访问(lseek)
- Project training of Software College of Shandong University rendering engine system basic renderer (VII)
- Interview: why do integer wrapper classes try to use equals() to compare sizes
- Step by step steps to create an ABAP program with a custom screen
- The market share of packaged drinking water has been the first for eight consecutive years. How does this brand DTC continue to grow?
- Global and Chinese market for commercial ceiling fans 2022-2028: Research Report on technology, participants, trends, market size and share
- C packing and unpacking
猜你喜欢

Huawei equipment is configured with CE dual attribution

Project training of Software College of Shandong University rendering engine system basic renderer (6)

Kill program errors in the cradle with spotbugs

acwing794 高精度除法

RTOS RT thread bare metal system and multi thread system

Explore the Apache shardingsphere SQL parse format function

acwing796 子矩阵的和

acwing 798二维差分(差分矩阵)
![In 2020, the demand for strain sensors in China will reach 9.006 million, and the market scale will reach 2.292 billion yuan [figure]](/img/a8/dd5f79262fe6196dd44ba416a4baac.jpg)
In 2020, the demand for strain sensors in China will reach 9.006 million, and the market scale will reach 2.292 billion yuan [figure]

办公室VR黄片,骚操作!微软HoloLens之父辞职!
随机推荐
The nohup command uses
Multimix:从医学图像中进行的少量监督,可解释的多任务学习
一步步创建包含自定义 Screen 的 ABAP 程序的详细步骤试读版
RTOS rt-thread裸机系统与多线程系统
acwing 800. 数组元素的目标和
位运算例题(待续)
[thinking about the process of structure optimization] how to build the evaluation ability of the impact of technical solutions
Browsercontext class of puppeter
5-5 configuring MySQL replication log point based replication
RTOS RT thread bare metal system and multi thread system
Review of the development of China's medical beauty (medical beauty) industry in 2021: the supervision is becoming stricter, the market scale is expanding steadily, and the development prospect is bro
Scanpy (VI) analysis and visualization of spatial transcriptome data
Project training of Software College of Shandong University rendering engine system point cloud processing (10)
华为设备配置CE双归属
Global and Chinese markets for air sampling calibration pumps 2022-2028: Research Report on technology, participants, trends, market size and share
联通网管协议框图
leetcode-54. Spiral matrix JS
Step by step to create a trial version of ABAP program containing custom screen
< 山东大学软件学院项目实训 > 渲染引擎系统——点云处理(十)
acwing796 子矩阵的和