具体内容在代码里面都有了
树状数组:
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define lc p<<1
#define rc p<<1|1
#define N 500005
int n,w[N];
struct node{
int l,r,sum;
}tr[N*4];
void pushup(int p){//向上更新
tr[p].sum=tr[lc].sum+tr[rc].sum;
}
void build(int p,int l,int r){//建树
tr[p]={l,r,w[l]};
if(l==r) return; //是叶子则返回
int m=l+r>>1; //不是叶子则裂开
build(lc,l,m);
build(rc,m+1,r);
pushup(p);
}
void update(int p,int x,int k){//点修改
if(tr[p].l==x&&tr[p].r==x){//叶子则修改
tr[p].sum+=k;
return;
}
int m=tr[p].l+tr[p].r>>1;//非叶子则裂开
if(x<=m) update(lc,x,k);
if(x>m) update(rc,x,k);
pushup(p);
}
int query(int p,int x,int y){//区间查询
if(x<=tr[p].l&&tr[p].r<=y)//覆盖则返回
return tr[p].sum;
int m=tr[p].l+tr[p].r>>1;//不覆盖则裂开
int sum=0;
if(x<=m) sum+=query(lc,x,y);
if(y>m) sum+=query(rc,x,y);
return sum;
}
int main(){
ios::sync_with_stdio(0);
int m,op,x,y;
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>w[i];
build(1,1,n);
while(m--){
cin>>op>>x>>y;
if(op==1) update(1,x,y);
else cout<<query(1,x,y)<<endl;
}
return 0;
}
带懒标记的就是理解为父母存钱,等用了再下发给每个孩子
//线段树浅学习
//基于分治思想,用来维护区间信息(区间和,区间最值,区间gcd)
//可以在logn的时间内执行区间修改和区间查询
#include<iostream>
using namespace std;
//定义左儿子,右儿子
#define lc p<<1//这个是2 * p
#define rc p<<1|1//这个是2 * p + 1
const int N = 10010;
int w[N];
struct node
{
int l,int r,int sum;
}tree[N * 4];
//构造线段树
void built(int p,int l,int r)//p表示根节点
{
tree[p] = {l,r,w[l]};//刚开始w[l]没有意义
if(l == r)return;
//开始裂开
int mid = l+r>>1;
built(lc,l,mid);
built(lr,mid+1,r);
tree[p].sum = tree[lc].sum + tree[rc].sum;
}
//点修改
void xiugai(int p,int x,int k)//k表示要修改的值
{
if(tree[p].l == x && tree[p].r == x)
{
tree[p].sum += k;
return;
}
int mid = tree[p].l + tree[p].r >> 1;
if(x <= m)xiugai(lc,x,k);
if(x > m)xiugai(rc,x,k);
tree[p].sum = tree[lc].sum + tree[rc].sum;
}
int query(int p,int x,int y)
{
if(x <= tree[p].l && tree[p].r <= y)return tree[p].sum;
int mid = tree[p].l + tree[p].r >> 1;
int sum = 0;
if(x <= m)sum += query(lc,x,y);
if(x > m) sum += query(rc,x,y);
} return sum;
//带有懒标记的线段树
#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int w[N];
int m,n;
#define lc p << 1
#define rc p<<1 | 1
struct node
{
int l,int r,int add;
}tree[N * 4];
void pushup(int p)
{
tree[p].sum = tree[lc].sum + tree[rc].sum;
}
void pushdown(int p,int x,int y,int k)
{
if(tree[p].add)
{
tree[lc].sum += tree[p].add * (tree[lc].r - tree[lc].l+1);
tree[rc].sum += tree[p].add * (tree[rc].r - tree[rc].l+1);
tree[lc].add += tree[p].add;
tree[rc].add += tree[p].add;
tree[p].add = 0;
}
}
void built(int p,int l,int r)
{
tree[p] = {l,r,w[l],0};
if(l == r)return;
int mid = l+r>>1;
built(lc,l,mid);
built(rc,mid+1,r);
pushup(p);
}
void xiugai(int p,int x,int y,int k)
{
if(x <= tree[p].l && tree[p].r <= y)
{
tree[p].sum += (tree[p].r - tree[p].l+1)*k;
tree[p].add += k;
return;
}
int mid = tree[p].l + tree[p].r >> 1;
pushdown(p);
if(x <= m)xiugai(lc,x,y,k);
if(x > m)xiugai(rc,x,y,k);
pushup(p);
}
int query(int p,int x,int y)
{
if(x <= tree[p].l && tree[p].r <= y)return tree[p].sum;
int mid = tree[p].l + tree[p].r >> 1;
pushdown(p);
if(x <= m)sum += query(lc,x,y);
if(x > m)sum += query(rc,x,y);
return sum;
}
signed main()
{
scanf("%d",&n);//n个节点
build(1,1,n);//建树
scanf("%d",&m);//m种操作
for(int i=1;i<=m;i++)
{
scanf("%d",&p);
ans = 0;
if(p == 1)
{
scanf("%d",&x);
//单点查询,输出第x个数
printf("%d",ans);
}
else if(p==2)
{
scanf("%d%d",&x,&y);
}
else if(p==3)
{
scanf("%d%d",&a,&b);//区间查询
printf("%d\n",ans);
}
else
{
scanf("%d%d%d",&a,&b,&y);//区间修改
}
}
return 0;
}
实战模式:
线段树模板1
//带有懒标记的线段树
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define lc p<<1
#define rc p<<1|1
#define N 100005
#define LL long long
LL n,w[N];
struct node{
LL l,r,sum,add;
}tr[N*4];
void pushup(LL p){
tr[p].sum=tr[lc].sum + tr[rc].sum;
}
void pushdown(LL p){
auto &u=tr[p],&l=tr[lc],&r=tr[rc];
if(u.add){
l.sum+=u.add*(l.r-l.l+1),
r.sum+=u.add*(r.r-r.l+1),
l.add+=u.add,
r.add+=u.add,
u.add=0;
}
}
void build(LL p,LL l,LL r){
tr[p]={l,r,w[l],0};
if(l==r) return; //是叶子则返回
LL m=l+r>>1; //不是叶子则裂开
build(lc,l,m);
build(rc,m+1,r);
pushup(p);
}
void update(LL p,LL x,LL y,LL k){
if(x<=tr[p].l&&tr[p].r<=y)
{
//覆盖则修改
tr[p].sum+=(tr[p].r-tr[p].l+1)*k;
tr[p].add+=k;
return;
}
LL m=tr[p].l+tr[p].r>>1; //不覆盖则裂开
pushdown(p);
if(x<=m) update(lc,x,y,k);
if(y>m) update(rc,x,y,k);
pushup(p);
}
LL query(LL p,LL x,LL y){
if(x<=tr[p].l&&tr[p].r<=y)//覆盖则返回
return tr[p].sum;
LL m=tr[p].l+tr[p].r>>1;//不覆盖则裂开
pushdown(p);
LL sum=0;
if(x<=m) sum+=query(lc,x,y);
if(y>m) sum+=query(rc,x,y);
return sum;
}
int main()
{
LL m,op,x,y,k;
cin>>n>>m;
for(LL i=1; i<=n; i ++) cin>>w[i];
build(1,1,n);
while(m--)
{
cin>>op>>x>>y;
if(op==2)cout<<query(1,x,y)<<endl;
else cin>>k,update(1,x,y,k);
}
return 0;
}