树状数组各种模板

因为时间不够了,所以——写一下模板

操作

lowbit

int lowbit(int x) {
	return x & (-x);
}

Update

void Update(int x,int y) {
	for(;x <= n;x += lowbit(x))
	Bit[x] += y;
}

Ask

int Ask(int x) {
	int ans = 0;
	for(;x;x -= lowbit(x))
	ans += Bit[x];
	return ans;
}

模板题目

单点修改,区间查询

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1e6 + 5;
int a[MAXN],Bit[MAXN],l,r,x,n,q;
int lowbit(int x) {
	return x & (-x);
}
void Update(int x,int y) {
	for(;x <= n;x += lowbit(x))
	Bit[x] += y;
}
int Ask(int x) {
	int ans = 0;
	for(;x;x -= lowbit(x))
	ans += Bit[x];
	return ans;
}
int main() {
	scanf("%d %d",&n,&q);
	for(int i = 1;i <= n;i ++) {
		scanf("%d",&a[i]);
		Update(i,a[i]);
	}
	for(int i = 1;i <= q;i ++) {
		int type;
		scanf("%d",&type);
		if(type == 1) {
			int l,x;
			scanf("%d %d",&l,&x);
			Update(l,x);
		}
		else {
			int l,r;
			scanf("%d %d",&l,&r);
			int ans = Ask(r) - Ask(l - 1);
			printf("%d\n",ans);
		}
	}
	return 0;
}

区间修改,单点查询

#include <cstdio>
#include <algorithm>
#include <cstring>
#define ll long long
using namespace std;
const ll MAXN = 1e6 + 5;
ll Bit[MAXN],cf[MAXN],n,q,a[MAXN];
ll lowbit(ll x) {
	return x & (-x); 
}
void Update(ll x,ll y) {
	for(;x <= n;x += lowbit(x))
	Bit[x] += y;
}
ll ask(ll x) {
	ll ans = 0;
	for(;x;x -= lowbit(x))
	ans += Bit[x];
	return ans;
}
int main() {
	scanf("%lld %lld",&n,&q);
	for(ll i = 1;i <= n;i ++) {
		scanf("%lld",&a[i]);
	}
	for(ll i = 1;i <= q;i ++) {
		ll type;
		scanf("%lld",&type);
		if(type == 1) {
			ll l,r,x;
			scanf("%lld %lld %lld",&l,&r,&x);
			Update(l,x);
			Update(r + 1,-x);
		}
		else {
			ll l;
			scanf("%lld",&l);
			printf("%lld\n",ask(l) + a[l]);
		}
	}
	return 0;
}

区间修改,区间查询

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 1e6 + 5;
int Bit[5][MAXN],sum[MAXN],n,q;
int lowbit(int x) {
	return x & (-x);
}
void Update(int type,int x,int y) {
	for(;x <= n;x += lowbit(x))
	Bit[type][x] += y;
}
int ask(int type,int x) {
	int ans = 0;
	for(;x;x -= lowbit(x))
	ans += Bit[type][x];
	return ans;
}
int main() {
	scanf("%d %d",&n,&q);
	for(int i = 1;i <= n;i ++) {
		int x;
		scanf("%d",&x);
		sum[i] = sum[i - 1] + x;
	}
	for(int i = 1;i <= q;i ++) {
		int type;
		scanf("%d",&type);
		if(type == 1) {
			int l,r,x;
			scanf("%d %d %d",&l,&r,&x);
			Update(0,l,x);
			Update(0,r + 1,-x);
			Update(1,l,l * x);
			Update(1,r + 1,-(r + 1) * x);
		}
		else {
			int l,r;
			scanf("%d %d",&l,&r);
			int num1 = sum[r] + (r + 1) * ask(0,r) - ask(1,r);
			int num2 = sum[l - 1] + l * ask(0,l - 1) - ask(1,l - 1);
			printf("%d\n",num1 - num2); 
		}
	}
	return 0;
}

求逆序对数

#include <cstdio>
#include <algorithm>
#include <cstring>
#define ll long long
using namespace std;
const ll MAXN = 1e6 + 5;
ll a[MAXN],b[MAXN],c[MAXN],Bit[MAXN],n,ans,m;
ll lowbit(ll x) {
	return x & (-x);
}
void Update(ll x,ll y) {
	for(;x <= n;x += lowbit(x))
	Bit[x] += y;
}
ll Sum(ll x) {
	ll ans = 0;
	for(;x;x -= lowbit(x))
	ans += Bit[x];
	return ans;
}

int main() {
	scanf("%lld",&n);
	for(ll i = 1;i <= n;i ++)
	scanf("%lld",&a[i]),b[i] = a[i];
	sort(b + 1,b + n + 1);
	int m = unique(b + 1,b + n + 1) - b - 1;
	for(int i = 1;i <= n;i ++) {
		c[i] = lower_bound(b + 1,b + m + 1,a[i]) - b;
	}
	for(ll i = n;i >= 1;i --) {
		ans += Sum(c[i] - 1);
		Update(c[i],1);
	}
	printf("%lld",ans);
	return 0;
}

二维树状数组:单点修改,区间查询

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const long long MAXN = 1e4 + 5;
long long a[MAXN][MAXN],Bit[MAXN][MAXN],n,m;
long long lowbit(long long x) {
	return x & (-x);
}
void Update(long long x,long long y,long long z) {
	for(;x <= n;x += lowbit(x)) {
		for(long long j = y;j <= m;j += lowbit(j)) {
			Bit[x][j] += z;
		}
	}
}
long long Sum(long long x,long long y) {
	long long ans = 0;
	for(;x;x -= lowbit(x)) {
		for(long long j = y;j;j -= lowbit(j)) {
			ans += Bit[x][j];
		}
	}
	return ans;
}
int main() {
	scanf("%lld %lld",&n,&m);
	long long type;
	while(scanf("%lld",&type) != EOF) {
		if(type == 1) {
			long long x,y,k;
			scanf("%lld %lld %lld",&x,&y,&k);
			Update(x,y,k);
		}
		else {
			long long a,b,c,d;
			scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
			printf("%lld\n",Sum(a - 1,b - 1) + Sum(c,d) - Sum(a - 1,d) - Sum(c,b - 1));
		}
	}
	return 0;
}

二维树状数组 :区间修改,区间查询

#include <cstdio>
#include <algorithm>
#include <cstring>
#define ll long long
using namespace std;
const ll MAXN = 2e3 + 55;
ll n,m,Bit1[MAXN][MAXN],Bit2[MAXN][MAXN],Bit3[MAXN][MAXN],Bit4[MAXN][MAXN];
ll lowbit(ll x) {
	return x & (-x);
}
void Update(ll x,ll y,ll z) {
	ll p = x;
	for(;x <= n;x += lowbit(x)) {
		for(ll j = y;j <= n;j += lowbit(j)) {
			Bit1[x][j] += z;
			Bit2[x][j] += z * p;
			Bit3[x][j] += z * y;
			Bit4[x][j] += p * y * z;
		}
	}
}
ll Sum(ll x,ll y) {
	ll ans = 0,p = x;
	for(;x;x -= lowbit(x)) {
		for(ll j = y;j;j -= lowbit(j)) {
			ans += (p + 1) * (y + 1) * Bit1[x][j] - (y + 1) * Bit2[x][j] - (p + 1) * Bit3[x][j] + Bit4[x][j]; 
		}
	}
	return ans;
}
int main() {
	scanf("%lld %lld",&n,&m);
	ll type;
	while(scanf("%lld",&type) != EOF) {
		if(type == 1) {
			ll a,b,c,d,x;
			scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&x);
			Update(a,b,x);
			Update(c + 1,b,-x);
			Update(a,d + 1,-x);
			Update(c + 1,d + 1,x);
		}
		else {
			ll a,b,c,d;
			scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
			printf("%lld\n",Sum(a - 1,b - 1) - Sum(a - 1,d) - Sum(c,b - 1) + Sum(c,d));
		}
	}
	return 0;
}

实际做题需要很多技巧,需要灵机应变,不能死记模板。本文只包括了模板,给初学者的。
又水了一篇题解
END

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值