当前位置:网站首页>Segment tree & tree array template
Segment tree & tree array template
2022-06-22 04:24:00 【eliforsharon】
Line tree template
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<set>
#define fr(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const int maxn=100005;
int n,q;
ll tree[4*maxn+1];
ll lz[4*maxn+1];
void build(int node,int l,int r)
{
if(l==r)
{
scanf("%lld",&tree[node]);
return;
}
int mid=(l+r)>>1;
build(node*2,l,mid);
build(node*2+1,mid+1,r);
tree[node]=tree[node*2]+tree[node*2+1];
}
void update(int node,int l,int r,int index)
{
if(l==r)
{
tree[node]=index;
return;
}
int mid=(l+r)>>1;
if(index<=mid)
update(node*2,l,mid,index);
else
update(node*2+1,mid+1,r,index);
tree[node]=tree[node*2]+tree[node*2+1];
}
void push_down(int node,int l,int r)
{
if(l==r)
{
lz[node]=0;
return;
}
if(lz[node])
{
int mid=(l+r)>>1;
tree[2*node]+=(mid-l+1)*lz[node];
tree[2*node+1]+=(r-mid)*lz[node];
lz[2*node]+=lz[node];
lz[2*node+1]+=lz[node];
lz[node]=0;
}
}
void update_range(int node,int l,int r,int L,int R,int add)
{
if(l>=L&&R>=r)
{
lz[node]+=add;
tree[node]+=(r-l+1)*add;
return;
}
push_down(node,l,r);
int mid=(l+r)>>1;
if(mid>=L)
update_range(node*2,l,mid,L,R,add);
if(mid<R)
update_range(node*2+1,mid+1,r,L,R,add);
tree[node]=tree[node*2]+tree[node*2+1];
}
ll query_range(int node,int l,int r,int L,int R)
{
if(l>=L&&R>=r) return tree[node];
push_down(node,l,r);
int mid=(l+r)>>1;
ll sum=0;
if(mid>=L)
sum+=query_range(node*2,l,mid,L,R);
if(mid<R)
sum+=query_range(node*2+1,mid+1,r,L,R);
return sum;
}
int main()
{
// freopen("in.txt","r",stdin);
scanf("%d",&n);
build(1,1,n);
memset(lz,0,sizeof(lz));
return 0;
} It's ridiculous to hit the board twice ......
Tree array template
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
using namespace std;
typedef long long ll;
const int maxn=500005;
int C[maxn];
int lowbit(int x)
{
return (-x)&x;
}
void insert(int i,int w)
{
while(i<=n)
{
C[i]+=w;
i+=lowbit(i);
}
}
int query(int i)
{
int sum=0;
while(i>=1)
{
sum+=C[i];
i-=lowbit(i);
}
return sum;
}
int main()
{
// freopen("in.txt","r",stdin);
return 0;
}
Sure enough, the tree array is easy to knock QAQ
边栏推荐
- 哈夫曼树
- Larave 数据库备份 定时任务
- IDS interview questions collection data structure + penetration avalanche + persistence + memory elimination strategy + database double write + Sentinel
- Join waits for synchronization results from multiple threads
- 解决Swagger2显示UI界面但是没内容
- 顺序表的基本操作
- 源代码加密技术科普
- Pytorch之contiguous函数
- The beta version of move protocol is stable, and it is temporarily decided to expand the scale of the prize pool
- 有了这几个刷题网站,还愁跳槽不涨薪?
猜你喜欢

Fonctionnement de base du tableau de séquence

PCM数据格式

解决Swagger2显示UI界面但是没内容

window10无法访问局域网共享文件夹

套用这套模板,玩转周报、月报、年报更省事
![[Niu Ke's questions -sql big factory interview questions] no1 An audio short video](/img/88/b9d5a6d6196f72cf6808d610e3b8fd.png)
[Niu Ke's questions -sql big factory interview questions] no1 An audio short video

Experience and summary of embedded software testing

KS004 基于SSH通讯录系统设计与实现

Redis和MySQL如何保持数据一致性?强一致性,弱一致性,最终一致性

POSIX shared memory
随机推荐
微信小程序 上传七牛云 laravel
With these websites, do you still worry about job hopping without raising your salary?
Laravel implements soft deletion
When the move protocol beta is in progress, the ecological core equity Momo is divided
Calculation of audio frame size
fc2新域名有什么价值?如何解析到网站?
希尔排序
Experience and summary of embedded software testing
树的存储结构
CentOS uses composer install to report an error - phpunitphppunit 8
BFs of figure
Multithread interrupt usage
Cordova 项目中自定义插件--插件创建流程
PHP connection to mysql8.0 reports an error: illuminate\database\queryexception
How do redis and MySQL maintain data consistency? Strong consistency, weak consistency, final consistency
read stream 特别注意
Axios get parameter transfer splicing database fields
window10无法访问局域网共享文件夹
有了这几个刷题网站,还愁跳槽不涨薪?
WPF DataContext usage (2)