树状数组の笔记(update : 22.11.23)

原理图

在这里插入图片描述

作用:

动态维护区间操作

性质:

  1. 每个根节点 t [ x ] 所覆盖的长度为 lowbit(x)
  2. 每个节点 t [ x ] 的父节点是 t [ x + lowbit(x) ]
  3. 整棵树的深度为 logn + 1

1.单点修改 , 区间查询:

P3374 【模板】树状数组 1

单点修改 logn
区间查询 logn
n 次操作复杂度 nlogn 1e6 左右

#include<bits/stdc++.h>
using namespace std;
       
#define fi first
#define se second  
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
typedef unsigned long long ull;
typedef long long ll;
const int N = 1e6+10;
const int NN = 1e5+100;
const int pp = 1e7+7;
typedef pair<int,int>PII;
const int inf = 2147483647;

ll a[N],t,op,l,r;
int n,m;
int lowbit(int x){
	return x&-x;
}

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

ll getnum(int x){
	ll sum=0;
	for(;x;x-=lowbit(x)){
		sum+=a[x];	
	}
	return sum;
}//求取点x的前缀和

int main()
{
	IOS;
	cin>>n>>m;
	
	for(int i=1;i<=n;i++){
		cin>>t;
		add(i,t);
	}
	
	for(int i=1;i<=m;i++){
		cin>>op>>l>>r;
		
		if(op==1){
			add(l,r);
		}else{
			cout<<getnum(r)-getnum(l-1)<<"\n";
		}
	}
	
}

2.区间修改,单点查询:

P3368 【模板】树状数组 2

区间修改 logn
单点查询 logn

区间修改是利用树状数组维护一个修改的差分数组,查询的时候用原数组加上修改值即可;

#include<bits/stdc++.h>
using namespace std;
       
#define fi first
#define se second  
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
typedef unsigned long long ull;
typedef long long ll;
const int N = 1e6+10;
const int NN = 1e5+100;
const int pp = 1e7+7;
typedef pair<int,int>PII;
const int inf = 2147483647;

ll a[N],b[N],op,x,y,k;
int n,m;

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

void add(int x,int k){
	for(;x<=n;x+=lowbit(x)){
		b[x]+=k;
	}
}

ll get(int x){
	ll sum=0;
	for(;x;x-=lowbit(x)){
		sum+=b[x];	
	}
	return sum;
}//差分数组前缀和

int main()
{
	IOS;
	cin>>n>>m;
	
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	
	for(int i=1;i<=m;i++){
		cin>>op;
		
		if(op==1){
			cin>>x>>y>>k;
			add(x,k);
			add(y+1,-k);
		}else{
			cin>>x;
			cout<<a[x]+get(x)<<"\n";
		}
	}
	
}

3.求逆序对的个数

逆序对

思路:

当插入一个值后,逆序对的数量就是这个数前面的比其大的数的数量也可以等价为
前面的数的总个数 - 前面小于等于自己的数的个数

前面小于等于自己的数的个数也就是前缀和可以用树状数组来维护

当数的范围较大时,可以先离散化再用树状数组 , 注意离散化的时候要处理一下相等的数离散化后的大小。

#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e7+10;
const int NN = 1e6+100;
const int p = 1e5 + 3;
typedef pair<int,int>PII;
const int inf = 1e9 + 10;
const ll linf = 1e18 + 10;
const double eps = 1e-9;
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);

inline int read() {
	bool sym = 0; int res = 0; char ch = getchar();
	while (!isdigit(ch)) sym |= (ch == '-'), ch = getchar();
	while (isdigit(ch)) res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
	return sym ? -res : res;
}

//区间加 , 单点求值

ll n ;

struct node{
	int x;
	int id;
	int ls;
}b[N];

int a[N]; 

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

void add(ll x,ll k){
	for(;x<=n;x+=lowbit(x)){
		a[x] += k;
	}
}

ll get(ll x){
	ll sum = 0;
	for(;x;x-=lowbit(x)){
		sum += a[x];
	}
	return sum;
}

bool cmp(node a,node b){
	return a.x < b.x || (a.x == b.x && a.id < b.id);
}//离散化的时候 , 相同大小的数据离散化后编号小的要出现在前面,这里很重要
bool cmpx(node a,node b){
	return a.id < b.id;
}

int main(){

	IOS
	
	ll ans = 0;
	
	cin >> n;
	
	for(int i=1;i<=n;i++){
		cin >> b[i].x;b[i].id = i;
	}
	sort(b+1,b+1+n,cmp);
	
	for(int i=1;i<=n;i++){
		b[i].ls = i;
	}//离散化
	
	sort(b+1,b+1+n,cmpx);//恢复原数组
	
	for(int i=1;i<=n;i++){
		add(b[i].ls , 1);
		ans += i - get(b[i].ls);
	} //插入然后计数
	
	cout << ans ;
	
	return 0;
}

4.维护区间最大值和最小值

维护区间最大值和最小值

思路 :

维护一个最大值树状数组和一个最小值树状数组,每次查询优先跳树状数组其次跳数组
通过lowbit操作减少比较次数 ,时间复杂度降低到 logx

#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5+10;
const int NN = 1e6+100;
const int p = 1e5 + 3;
typedef pair<int,int>PII;
const int inf = 1e9 + 10;
const ll linf = 1e18 + 10;
const double eps = 1e-9;
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);

inline int read() {
	bool sym = 0; int res = 0; char ch = getchar();
	while (!isdigit(ch)) sym |= (ch == '-'), ch = getchar();
	while (isdigit(ch)) res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
	return sym ? -res : res;
}

int n , q;

int tx[N] , tn[N] ,a[N];

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

void add(int x,int k){
	for(;x<=n;x+=lowbit(x)){
		tx[x] = max(tx[x] , k);
		tn[x] = min(tn[x] , k);
	}
}

void init(){
	for(int i=1;i<=n;i++){
		tx[i] = -inf;
		tn[i] = inf;
	}
}

int get(int l ,int r){
	
		
	int max1 = -inf , min1 = inf;
	
	while(l <= r){
		
		//优先处理树状数组
		while(r - lowbit(r) >= l){
			max1 = max(max1 , tx[r]);
			min1 = min(min1 , tn[r]);
			r -= lowbit(r);
		}	
		//单独处理离散数据
		max1 = max(max1 , a[r]);
		min1 = min(min1 , a[r]);
		r--;
		//树状数组和普通数组互相跳
	}
	
	
	return max1 - min1 ;
				
}


int main(){

	n = read();
	q = read();
	
	init();
	
	for(int i=1;i<=n;i++){
		a[i] = read();
		add(i,a[i]);
	}
	
	int l , r;
	
	for(int i=1;i<=q;i++){
		l = read();
		r = read();
		printf("%d\n",get(l,r));
	}

	return 0;
}

5.树状数组维护异或和

异或橘子

#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
const int N = 2e5+10;
const int NN = 1e6+100;
const int p = 1e5 + 3;
typedef pair<int,int>PII;
const int inf = 1e9 + 10;
const ll linf = 1e18 + 10;
const double eps = 1e-9;
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);

inline int read() {
	bool sym = 0; int res = 0; char ch = getchar();
	while (!isdigit(ch)) sym |= (ch == '-'), ch = getchar();
	while (isdigit(ch)) res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
	return sym ? -res : res;
}

ll n , m ;
ll a[N] , b[N] ,c[N];
ll lowbit(ll x){
	return x & (-x);
}

void add(ll s,ll x,ll k){
	if(s == 1){
		for(;x<=n;x+=lowbit(x)){
			a[x] ^=  k;
		}
	}else{
		for(;x<=n;x+=lowbit(x)){
			b[x] ^=  k;
		}
	}
}

ll get(ll s,ll x){
	
	ll ans = 0;
	
	if(s == 1){
		for(;x;x-=lowbit(x)){
			ans ^= a[x];
		}
	}else{
		for(;x;x-=lowbit(x)){
			ans ^= b[x];
		}
	}
	
	return ans ;
}

int main(){
	
	IOS
	
	cin >> n >> m;
	
	for(int i=1;i<=n;i++){
		cin >> c[i];
		if(i % 2) add(1,i,c[i]);
		else add(2,i,c[i]);
	}
	
	ll op , x , y;
	
	for(int i=1;i<=m;i++){
		cin >> op >> x >> y;
		if(op == 1){
			if(x % 2){
				add(1,x,y^c[x]);
			}else{
				add(2,x,y^c[x]);
			}
			c[x] = y;
		}else{
			ll cnt = y - x + 1 , ans = 0;
			if(cnt % 2 == 0){
				cout << "0\n";
			}else{
				if(x % 2){
					ans = get(1,y) ^ get(1,x-1);
				}else{
					ans = get(2,y) ^ get(2,x-1);
				}
				cout << ans << "\n";
			}
		}
	}

	return 0;
}

6.结构体维护多个树状数组

树状数组

#include<bits/stdc++.h>
using namespace std;

#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long  
const int N = 2e5 + 10;
const int p = 998244353;

int n , t , a[N];

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

int qp(int a,int b){
	a %= p;
	int res = 1;
	while(b){
		if(b % 2 != 0) res = res * a % p;
		a = a * a % p;
		b /= 2;
	}
	return res;
}

struct node{
	int b[N];
	void add(int x,int k){
		for(;x<=N-10;x+=lowbit(x)){
			b[x] += k; b[x] %= p;
		}
	}
	int get(int x){
		int sum = 0;
		for(;x;x-=lowbit(x)){
			sum += b[x]; sum %= p;
		}
		return sum;
	}
}c1,c2;

signed main(){

	IOS
	
	cin >> n;
	
	int sum = 0 , k = 0;
	
	for(int i=1;i<=n;i++){
		cin >> a[i];
		sum += a[i-1];  
		sum %= p;
		k += 2 * a[i] * c1.get(a[i]) % p;
		k %= p;
		k += ((sum - c2.get(a[i])) % p + p) % p * 2;
		k %= p;
		k += a[i];
		k %= p;
		cout << k * qp(i*i%p,p-2) % p << "\n" ;
		c1.add(a[i],1);
		c2.add(a[i],a[i]);
	}

	return 0;
}

7.维护前缀最大值和最小值

维护前缀最值就不需要数组和树状数组分别处理 , 直接用树状数组就能全部处理

int q(int r){	
	int maxx = 0;
	while(r > 0){	maxx = max(maxx , b[r]);	r -= lowbit(r); 	}
	return maxx;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

草莓猫猫软糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值