线段树板子

节点结构(维护区间和与最大值,最小值)

struct node{
	int l;
	int r;
	int sum;
	int maxn;
	int minn;
};
const int maxn=1e5+5;
int arr[maxn];
node tree[maxn];

build方法

void build(int p,int l,int r){
	tree[p].l=l;
	tree[p].r=r;
	if(l==r){
	tree[p].sum=arr[l];
	tree[p].maxn=arr[l];
	tree[p].minn=arr[l];
	return;	
	}
	int mid=l+r>>1;
	build(2*p,l,mid);
	build(2*p+1,mid+1,r);
	tree[p].sum=tree[2*p].sum+tree[2*p+1].sum;
	tree[p].maxn=max(tree[2*p].maxn,tree[2*p+1].maxn);
	tree[p].minn=min(tree[2*p].minn,tree[2*p+1].minn);
}

change方法(改变原数组下标为x(开始为1)的元素为k)

void change(int p,int x,int k){
	if(tree[p].l==x&&tree[p].r==x){
		tree[p].sum=k;
		tree[p].maxn=k;
		tree[p].minn=k;
		return;
	}
	int mid=tree[p].l+tree[p].r>>1;
	if(x<=mid)change(2*p,x,k);
	if(x>mid)change(2*p+1,x,k);
	tree[p].sum=tree[2*p].sum+tree[2*p+1].sum;
	tree[p].maxn=max(tree[2*p].maxn,tree[2*p+1].maxn);
	tree[p].minn=min(tree[2*p].minn,tree[2*p+1].minn);
}

query_range方法(查询数组l->r范围的元素和)

int query_range(int p,int l,int r){
	if(l<=tree[p].l&&tree[p].r<=r){
		return tree[p].sum;
	}
	int mid=tree[p].l+tree[p].r>>1;
	int sum=0;
	if(l<=mid)sum+=query_range(2*p,l,r);
	if(r>mid)sum+=query_range(2*p+1,l,r);
	return sum;
}

query_max方法(查询数组l->r范围的元素最大值)

int query_max(int p,int l,int r){
	if(l<=tree[p].l&&tree[p].r<=r){
		return tree[p].maxn;
	}
	int ans=0;
	int mid=tree[p].l+tree[p].r>>1;
	if(l<=mid)ans=max(query_max(2*p,l,r),ans);
	if(r>mid)ans=max(query_max(2*p+1,l,r),ans);
	return ans;
}```
## 懒标记版本(区间增加v)
```c++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const ll llmax=LLONG_MAX;
const int maxn=1e5+5;
int n,m;
struct node{
	int l;
	int r;
	int sum;
	int lztag;
};
int a[maxn];
node tr[4*maxn];
void build(int p,int l,int r)
{
	tr[p].l=l;
	tr[p].r=r;
	tr[p].lztag=0;
	if(l==r){
		tr[p].sum=a[l];
		return;
	}
	int mid=l+r>>1;
	build(2*p,l,mid);
	build(2*p+1,mid+1,r);
	tr[p].sum=tr[2*p].sum+tr[2*p+1].sum;
}


void update(int p,int num)
{
	tr[p].sum+=(tr[p].r-tr[p].l+1)*num;
	tr[p].lztag+=num;
}
void pushdown(int p)
{
	update(p*2,tr[p].lztag);
	update(p*2+1,tr[p].lztag);
	tr[p].lztag=0;
}

void change(int p,int l,int r,int v)
{
	if(tr[p].l>=l&&tr[p].r<=r){
		update(p,v);
		return;
	}
	pushdown(p);
	int mid=tr[p].l+tr[p].r>>1;
	if(l<=mid){
		change(2*p,l,r,v);
	}
	if(r>mid){
		change(2*p+1,l,r,v);
	}
	tr[p].sum=tr[2*p].sum+tr[2*p+1].sum;
}

int query(int p,int l,int r)
{
	if(l<=tr[p].l&&tr[p].r<=r){
		return tr[p].sum;
	}
	int ans=0;
	int mid=tr[p].l+tr[p].r>>1;
	pushdown(p);
	if(l<=mid){
		ans+=query(2*p,l,r);
	}
	if(r>mid)ans+=query(2*p+1,l,r);
	return ans;
}
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	build(1,1,n);
	while(m--){
		int op;cin>>op;
		if(op==1){
			int x,y,k;cin>>x>>y>>k;
			change(1,x,y,k);
		}else{
			int x,y;
			cin>>x>>y;
			cout<<query(1,x,y)<<endl;
		}
	}
	return 0;
}


posted @   Marinaco  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示