求调线段树
  • 板块学术版
  • 楼主RNTBW
  • 当前回复5
  • 已保存回复5
  • 发布时间2022/10/7 12:01
  • 上次更新2023/10/27 08:21:06
查看原帖
求调线段树
643735
RNTBW楼主2022/10/7 12:01

RT,悬赏一关注

区间修改+区间查询

#include<bits/stdc++.h>
using namespace std;
#define int long long
int a[100001];
int n,m,i;
struct evd
{
	int x,y,z,l;
} tr[2000001];
void make_tree(int l,int r,int p)
{
	tr[p].x=l;tr[p].y=r;
	if(l==r)
	{
		tr[p].z=a[l];
		return;
	}
	int mid=(l+r)>>1;
	make_tree(l,mid,p<<1);
	make_tree(mid+1,r,p<<1|1);
	tr[p].z=tr[p<<1].z+tr[p<<1|1].z;
}
void pushd(int p)
{
	tr[p<<1].l+=tr[p].l;
	tr[p<<1|1].l+=tr[p].l;
	tr[p<<1].z+=tr[p].l;
	tr[p<<1|1].z+=tr[p].l;
	tr[p].l=0;
} 
void add(int l,int r,int p,int d)
{
	if(tr[p].l)pushd(p);
	tr[p].z+=d;
	if(tr[p].x==tr[p].y)return;
	if(tr[p<<1].x<=k&&tr[p<<1].y>=k)add(k,p<<1,d);
	if(tr[p<<1|1].x<=k&&tr[p<<1|1].y>=k)add(k,p<<1|1,d);
}
int query(int p,int l,int r)
{
	if(tr[p].l)pushd(p);
	if(l>tr[p].y||r<tr[p].x)return 0;
	if(tr[p].x>=l&&tr[p].y<=r)return tr[p].z;
	int ans=0;
	ans=query(p<<1,l,r)+query(p<<1|1,l,r);
	return ans;
}
signed main()
{
	scanf("%lld%lld",&n,&m);
	for(i=1;i<=n;i++) scanf("%lld",&a[i]);
	make_tree(1,n,1);
	while(m--)
	{
		int op,x,y,z;
		cin>>op;
		scanf("%lld%lld",&x,&y);
		if(op==1)
		{
			scanf("%lld",&z);
			add(x,y,1,z);
		}
		else printf("%lld\n",query(1,x,y));
	}
	return 0;
}
2022/10/7 12:01
加载中...