当前位置:网站首页>AcWing 243. A simple integer problem 2 (tree array interval modification interval query)
AcWing 243. A simple integer problem 2 (tree array interval modification interval query)
2022-07-06 03:51:00 【Jacob* ̄▽ ̄*】


The question :

This problem requires the use of tree array Interval modification and Interval query .
Ideas :
stay Last question in , We maintain an original array with a tree array a The differential array of b, For every instruction “C l r d”, hold b[i] add d, And then b[r+1] subtract d.
What has been discussed is :b Of 1~x The prefix and b[1~x] After these instructions ,a[x] Increased value .
that a The prefix and of the sequence a[1~x] The overall value added is 
The above formula can be further rewritten into :

Derivation process :

Therefore, in this problem, we can consider adding a tree array , For maintenance i*b[i] And
, The above formula can be calculated directly .
To be specific , We build two tree arrays , Respectively c1, c2, Initially, all values are 0, For every instruction “C l r d”, Perform four operations :
- ①
c1OflPosition plusd. - ②
c1Ofr+1Position subtractiond. - ③
c2OflPosition plusl*d. - ④
c2Ofr+1Position subtraction(r+1)*d.
For every instruction “Q l r”, We can directly apply the formula derived above to calculate .
This problem brings us a kind of thought , Separate items that contain multiple variables , Make different variables in the formula independent of each other , This idea is very important .
Time complexity :
O(mlogn)
Code :
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5+10;
int n, m;
int a[N], c1[N], c2[N];
int read() {// Fast reading
int x=0,f=1; char c=getchar();
while(c<'0' || c>'9') {if(c=='-') f=-1; c=getchar();}
while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
return x*f;
}
inline int lowbit(int x) {return x & (-x);}
int ask(int c[], int x)
{
int ans = 0;
while(x) {ans+=c[x], x-=lowbit(x);}
return ans;
}
int add(int x, int y, int c[])
{
for(; x<=n; x+=lowbit(x)) c[x]+=y;
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=n;++i)
{
a[i] = read();
add(i, a[i]-a[i-1], c1), add(i, i*(a[i]-a[i-1]), c2);
}
while(m--)
{
char op[2];
int l, r, d;
scanf("%s", op);
l = read(), r = read();
if(*op=='C')
{
d = read();
add(l, d, c1), add(l, l*d, c2);
add(r+1, -d, c1), add(r+1, -(r+1)*d, c2);
}
else
{
printf("%lld\n", (r+1)*(ask(c1, r)) - ask(c2, r) - l*(ask(c1, l-1)) + ask(c2, l-1));
}
}
return 0;
}
边栏推荐
- [analysis of variance] single factor analysis and multi factor analysis
- Pytoch foundation - (1) initialization of tensors
- 在字节做测试5年,7月无情被辞,想给划水的兄弟提个醒
- Thread sleep, thread sleep application scenarios
- [Qt5] QT QWidget immediately appears and disappears
- 51nod 1130 n factorial length V2 (Stirling approximation)
- 给新人工程师组员的建议
- 2.2 STM32 GPIO operation
- Record the process of reverse task manager
- Flask learning and project practice 8: introduction and use of cookies and sessions
猜你喜欢

【PSO】基于PSO粒子群优化的物料点货物运输成本最低值计算matlab仿真,包括运输费用、代理人转换费用、运输方式转化费用和时间惩罚费用

【按键消抖】基于FPGA的按键消抖模块开发

C#(三十)之C#comboBox ListView treeView

2.13 weekly report

给新人工程师组员的建议
![[slam] orb-slam3 parsing - track () (3)](/img/87/b580837778c2c9f6bac5ba49403d6b.png)
[slam] orb-slam3 parsing - track () (3)

施努卡:3d视觉检测应用行业 机器视觉3d检测

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

SAP ALV color code corresponding color (finishing)

C#(二十七)之C#窗体应用
随机推荐
Ks008 SSM based press release system
ESP32(基于Arduino)连接EMQX的Mqtt服务器上传信息与命令控制
Pandora IOT development board learning (HAL Library) - Experiment 9 PWM output experiment (learning notes)
Blue Bridge Cup - Castle formula
Schnuka: what is visual positioning system and how to position it
简述C语言中的符号和链接库
Simple blog system
2.13 weekly report
Ybtoj coloring plan [tree chain dissection, segment tree, tarjan]
2.2 STM32 GPIO operation
Cubemx transplantation punctual atom LCD display routine
【FPGA教程案例11】基于vivado核的除法器设计与实现
51nod 1130 n factorial length V2 (Stirling approximation)
[prediction model] difference method model
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
WPF效果第一百九十一篇之框选ListBox
Indicator system of KQI and KPI
Codeforces Global Round 19
【PSO】基于PSO粒子群优化的物料点货物运输成本最低值计算matlab仿真,包括运输费用、代理人转换费用、运输方式转化费用和时间惩罚费用
简易博客系统