✔「 树状数组 」单点修改区间查询、区间修改单点查询、 区间修改区间查询 ☑全

单点修改 区间查询

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define LL long long
#define N 500006
#define lowbit(x) x&-x

using namespace std;

inline LL wread(){
	char c(getchar ());LL wans(0),flag(1);
	while (c<'0' || c>'9'){if (c=='-') flag=-1;c=getchar ();}
	while (c>='0' && c<='9'){wans=wans*10+c-'0';c=getchar ();}
	return wans*=flag;
}

int n,m;
LL a[N];

//单点修改 
void add (int pos,LL x){
	while (pos<=n){
		a[pos]+=x;
		pos+=lowbit(pos);
	}
}

//查询 [1,pos] 之和 
LL askd(int pos){
	LL ans(0);
	while (pos>0){
		ans+=a[pos];
		pos-=lowbit(pos);
	}
	return ans;
}

int main (){
	scanf ("%d%d",&n,&m);
	for (int i(1);i<=n;++i){
		LL x(wread());
		//加入原数组 
		add(i,x);
	}
	while (m--){
		int opr;
		scanf ("%d",&opr);
		if (opr==1){
			int pos;
			scanf ("%d",&pos);LL x(wread());
			//修改单个值 
			add(pos,x);
		}
		else {
			int x,y;
			scanf ("%d%d",&x,&y);
			//相减 
			printf("%lld\n",askd(y)-askd(x-1));
		}
	}
	return 0;
}

 

区间修改 单点查询

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define lowbit(x) x&-x
#define LL long long
#define N 500007

using namespace std;

inline int wread(){
    char c(getchar ());int wans(0),flag(1);
    while (c<'0' || c>'9'){if (c=='-') flag=-1;c=getchar ();}
    while (c>='0' && c<='9'){wans=wans*10+c-'0';c=getchar ();}
    return wans*=flag;
}

inline LL ll_wread(){
    char c(getchar ());LL wans(0),flag(1);
    while (c<'0' || c>'9'){if (c=='-') flag=-1;c=getchar ();}
    while (c>='0' && c<='9'){wans=wans*10+c-'0';c=getchar ();}
    return wans*=flag;
}

int n,m;
LL a[N],cha[N];
//原数组 查分数组

//所有操作是基于查分数组来做的,不是原数组哦 
 
void add (int pos,LL x){
	while (pos<=n){
		cha[pos]+=x;
		pos+=lowbit(pos);
	}
}

LL askd(int pos){
	LL ans(0);
	while (pos>0){
		ans+=cha[pos];
		pos-=lowbit(pos);
	}
	return ans;
}
 
int main (){
	n=wread();m=wread();
	for (int i(1);i<=n;++i)
		a[i]=ll_wread();
	for (int i(1);i<=n;++i)
		add(i,a[i]-a[i-1]);//把查分后的值加入数组 
	while (m--){
		int opr(wread());
		if (opr==1){
			int x(wread()),y(wread());LL k(ll_wread());
			add(x,k);//同样利用查分思想 
			add(y+1,-k);
		}
		else {
			int pos(wread());
			printf("%lld\n",askd(pos));
		}
	}
	return 0;
}

 

区间修改 区间查询

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#define LL long long
#define lowbit(x) x&-x

using namespace std;

inline int wread(){
    char c(getchar ());int wans(0),flag(1);
    while (c<'0' || c>'9'){if (c=='-') flag=-1;c=getchar ();}
    while (c>='0' && c<='9'){wans=wans*10+c-'0';c=getchar ();}
    return wans*=flag;
}

inline LL ll_wread(){
    char c(getchar ());LL wans(0),flag(1);
    while (c<'0' || c>'9'){if (c=='-') flag=-1;c=getchar ();}
    while (c>='0' && c<='9'){wans=wans*10+c-'0';c=getchar ();}
    return wans*=flag;
}

LL c[200005][2];
int n,q;

void add(int pos,LL x,int f)
{
    while(pos<=n)
    {
        c[pos][f]+=x;
        pos+=lowbit(pos);
    }
}

LL query(int pos,int f)
{
    LL res=0;
    while(pos>0)
    {
        res+=c[pos][f];
        pos-=lowbit(pos);
    }
    return res;
}

LL ask(int pos)
{
    LL res=(pos+1)*query(pos,0)-query(pos,1);
    return res; 
}
int main()
{
    n=wread();q=wread();
    LL a(0);
    for(int i=1;i<=n;i++)
    {
        LL b(ll_wread());
        add(i,b-a,0);
        add(i,(b-a)*i,1);
        a=b;
    }

    while (q--)
    {
        int opt(wread());
        if(opt==1)
        {
            int a(wread()),b(wread());LL x(ll_wread());
            add(a,x,0);
            add(b+1,-x,0);
            add(a,x*a,1);
            add(b+1,-x*(b+1),1);
        }
        else if(opt==2)
        {
            int a(wread()),b(wread());
            printf("%lld\n",ask(b)-ask(a-1));
        }
    }
    return 0;
}  

不得不说,树状数组区间修改,区间查询比线段树要快一些,但是,下一次,我还是要打线段树!!!

不过,对于 单点修改 区间查询区间修改 单点查询 还是要膜拜树状数组!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
树状数组(Fenwick Tree)是一种用于高效处理区间修改单点查询的数据结构。下面我将介绍如何使用树状数组实现区间修改单点查询。 首先,我们需要定义树状数组的数据结构。树状数组由一个数组和一组操作组成,其中数组用于存储数据,操作用于更新和查询数组中的值。 下面是一个示例的树状数组实现: ```python class FenwickTree: def __init__(self, n): self.size = n self.tree = [0] * (n + 1) def update(self, idx, delta): while idx <= self.size: self.tree[idx] += delta idx += idx & -idx def query(self, idx): res = 0 while idx > 0: res += self.tree[idx] idx -= idx & -idx return res def range_update(self, l, r, delta): self.update(l, delta) self.update(r + 1, -delta) def point_query(self, idx): return self.query(idx) ``` 在上面的代码中,我们定义了一个FenwickTree类,通过构造函数`__init__`来初始化树状数组,并使用`size`来表示数组的大小。`tree`数组用于存储数据。`update`方法用于更新指定位置的值,`query`方法用于查询指定位置之前的求和结果。`range_update`方法用于对指定区间进行修改,`point_query`方法用于查询单个位置的值。 下面是一个示例的使用场景: ```python # 示例使用 n = 10 tree = FenwickTree(n) # 区间修改 [2, 6] 的值加 3 tree.range_update(2, 6, 3) # 查询位置 5 的值 value = tree.point_query(5) print(value) ``` 在上面的示例中,我们创建了一个大小为10的树状数组,并对区间[2, 6]的值进行了修改,将其加3。然后,我们查询了位置5的值,结果为3。 希望以上内容能够帮助到你实现树状数组区间修改单点查询。如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值