当前位置:网站首页>P1438 boring sequence line segment tree + difference
P1438 boring sequence line segment tree + difference
2022-07-27 04:25:00 【Snow_ raw】
P1438 Boring series Line segment tree + Difference
Ideas : The question is nothing more than Line segment tree 1 A differential array is added on the basis of , So what we need to focus on is How to maintain a differential array . When known o p t = 1 opt=1 opt=1 when , We need to be in a range [ l , r ] [l,r] [l,r] The first is k k k The tolerance is d d d A series of equal proportions . The segment tree can be modified by the following derivation :
- l = 1 l=1 l=1 r = 5 r=5 r=5 k = 1 k=1 k=1 d = 2 d=2 d=2
- In subscript [ 1 , 5 ] [1,5] [1,5] Add..., respectively 1 1 1 3 3 3 5 5 5 7 7 7 9 9 9
- Convert it into a differential array : 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 − 9 -9 −9
- According to the above formula, we only need to 3 3 3 Time m o d i f y modify modify operation , Namely m o d i f y ( 1 , l , l ) modify(1,l,l) modify(1,l,l) 、 m o d i f y ( 1 , l + 1 , r ) modify(1,l+1,r) modify(1,l+1,r) 、 m o d i f y ( 1 , r + 1 , r + 1 ) modify(1,r+1,r+1) modify(1,r+1,r+1)
- Details to note , If l = = r l==r l==r Then there is no need m o d i f y ( 1 , l + 1 , r ) modify(1,l+1,r) modify(1,l+1,r) If r + 1 > n r+1>n r+1>n Then there is no need m o d i f y ( 1 , r + 1 , r + 1 ) modify(1,r+1,r+1) modify(1,r+1,r+1) .
Code :
/* * @author: Snow * @Description: Algorithm Contest * @LastEditTime: 2022-07-26 09:43:44 */
#include<bits/stdc++.h>
using namespace std;
#define int long long
// #pragma GCC optimize(3)
#define re register int
typedef pair<int,int>PII;
#define pb emplace_back
#define debug(a) cout<<a<<' ';
#define fer(i,a,b) for(re i=a;i<=b;i++)
#define der(i,a,b) for(re i=a;i>=b;i--)
int n,m;
const int N = 1e5+10;
int w[N];
struct Node{
int l,r,sum,lazy;
}tr[N*4];
void pushup(int u){
tr[u].sum=tr[u<<1].sum+tr[u<<1|1].sum;
}
void pushdown(int u){
auto &root=tr[u],&left=tr[u<<1],&right=tr[u<<1|1];
if(tr[u].lazy){
left.lazy+=root.lazy,left.sum+=(left.r-left.l+1)*root.lazy;
right.lazy+=root.lazy,right.sum+=(right.r-right.l+1)*root.lazy;
root.lazy=0;
}
}
void build(int u,int l,int r){
if(l==r){
tr[u]={
l,r,w[l],0};
}
else{
tr[u]={
l,r};
int mid=l+r>>1;
build(u<<1,l,mid);
build(u<<1|1,mid+1,r);
pushup(u);
}
}
void modify(int u,int l,int r,int d){
if(tr[u].l>=l&&tr[u].r<=r){
tr[u].sum+=(tr[u].r-tr[u].l+1)*d;
tr[u].lazy+=d;// Delay devolution
}
else{
pushdown(u);
int mid=tr[u].l+tr[u].r>>1;
if(l<=mid)modify(u<<1,l,r,d);
if(r>mid)modify(u<<1|1,l,r,d);
pushup(u);
}
}
int query(int u,int l,int r){
if(tr[u].l>=l&&tr[u].r<=r){
return tr[u].sum;
}
pushdown(u);
int mid=tr[u].l+tr[u].r>>1;
int sum=0;
if(l<=mid)sum=query(u<<1,l,r);
if(r>mid)sum+=query(u<<1|1,l,r);
return sum;
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>m;
fer(i,1,n)cin>>w[i];
der(i,n,1)w[i]=w[i]-w[i-1];
build(1,1,n);
while(m--){
int op;
cin>>op;
if(op==1){
int l,r,k,d;
cin>>l>>r>>k>>d;
modify(1,l,l,k);
if(l<r)modify(1,l+1,r,d);
if(r<n)modify(1,r+1,r+1,-(k+(r-l)*d));
}
else{
int p;
cin>>p;
cout<<query(1,1,p)<<endl;
}
}
return 0;
}
边栏推荐
- EVT interface definition file of spicy
- BigDecimal pit summary & Best Practices
- [Code] sword finger offer 04 search in two-dimensional array
- Using LCD1602 to display ultrasonic ranging
- Remember the major performance problems caused by a TCP packet loss
- Rust:axum learning notes (1) Hello World
- 【无标题】
- 【比赛参考】PyTorch常用代码段以及操作合集
- 大咖说·图书分享|精益产品开发:原则、方法与实施
- BigDecimal踩坑总结&最佳实践
猜你喜欢

2022 operation of simulated examination question bank and simulated examination platform for safety production management personnel of hazardous chemical production units

Deep analysis - dynamic memory management

Session&Cookie&token

你了解微信商户分账吗?

Brightcove任命Dan Freund为首席营收官

从零开始C语言精讲篇4:数组

e.target与e.currentTarget的区别

Learning route from junior programmer to architect + complete version of supporting learning resources

Analysis of three common kinematic models of mobile chassis

Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns
随机推荐
JVM调优中的一些常见指令
P1438 无聊的数列 线段树+差分
Preliminary understanding of NiO
[small sample segmentation] msanet: multi similarity and attention guidance for boosting few shot segmentation
spark练习案例(升级版)
Stm32cubemx learning notes (41) -- eth interface +lwip protocol stack use (DHCP)
[untitled]
Redis面试题(2022)
BSN IPFS(星际文件系统)专网简介、功能、架构及特性、接入说明
Internet of things smart home project - Smart bedroom
从根到叶的二进制数之和
法解析的外部符号 “public: virtual __cdecl nvinfer1::YoloLayerPlugin::~YoloLayerPlugin(void)“ “public: virtua
【软件工程期末复习】知识点+大题详解(E-R图、数据流图、N-S盒图、状态图、活动图、用例图....)
Cool Lehman VR panorama paves the way for you to start a business
每日一题:从链表中删去总和值为零的连续节点
Elastic certification test: 30 day FastPass Study Guide
The external symbol parsed by the method "public: virtual _ucdecl nvinfer1:: yololayerplugin:: ~yololayerplugin (void)" "public: virtual
Rust:axum学习笔记(1) hello world
Okaleido ecological core equity Oka, all in fusion mining mode
centos如何安装mysqldump