跳到正文

个人记录

树状数组

dsj2012约 8 分钟
原文
文章信息与版本

文章信息

发布
2025年5月24日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 8 分钟

版本

  1. v1当前

不会的先借鉴一下他人的树状数组

U438249 ١١(❛ᴗ❛)【树状数组】-清点人数

这道题目我们发现又有上车又有下车的,似乎不好写。但是,我们知道上车的人数,也知道下车的人数,求还剩。难道很难吗?

也就是说,我们分两个部分,一个统计下车人数,一个统计上车人数。最后两个部分相减就是结果了。

code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,k;
int c[500005];
int d[500005];
int lowbit(int x){
	return x & (-x);
}
void modify1(int x,int k,int val){//c去加
	while(x <= k){
		c[x] += val;
		x += lowbit(x);
	}
	return ;
}
void modify2(int x,int k,int val){//d去加
	while(x <= k){
		d[x] += val;
//		cout << x << "\n";
		x += lowbit(x);
	}
	return ;
}
int query1(int x){//c的总和
	int ans = 0;
	while(x > 0){
//		cout << x << "\n";
		ans += c[x];
		x -= lowbit(x);
	}
	return ans; 
} 
int query2(int x){//d的总和
	int ans = 0;
	while(x > 0){
		ans += d[x];
		x -= lowbit(x);
	}
	return ans; 
} 
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin >> n >> k;
	for(int i = 1 ; i <= k ; i ++){
		char ch;
		int x,y;
		cin >> ch;
		if(ch == 'A'){
			cin >> x;
			cout << query1(x) - query2(x) << "\n";
//			cout << query1(x) - query2(x) << "\n"; 
		}else if(ch == 'B'){
			cin >> x >> y;
			modify1(x , n , y);//x ~ n c 加上 y 
		}else{
			cin >> x >> y;
			modify2(x , n , y);//x ~ n d 加上 y 
		}
//		cout << "\n";
	}
	return 0;
} 

U438237 ١١(❛ᴗ❛)【树状数组+线段树】-模板(单点修改+区间查询)

没啥好讲的,最后肯定相当与求前缀和一样求答案即可。

code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,q;
int a[1000005];
int b[1000005];
int c[1000005];
int lowbit(int x){
	return x & (-x);
}
void modify(int x,int k,int val){
	while(x <= k){
		c[x] += val;
		x += lowbit(x);
	}
	return ;
}
int query(int x){
	int ans = 0;
	while(x > 0){
		ans += c[x]; 
		x -= lowbit(x);
	}
	return ans; 
} 
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin >> n >> q;
	for(int i = 1 ; i <= n ; i ++){
		cin >> a[i];
		modify(i , n , a[i]);
	}
	while(q --){
		int pos,l,r;
		cin >> pos >> l >> r;
		if(pos == 1){
			modify(l , n , r);
		}else{
			cout << query(r) - query(l - 1) << "\n";
		}
	}
	return 0;
} 

U438285 ١١(❛ᴗ❛)【树状数组】-数星星 Stars

题目说按照 yy 增序给出。那么,之前的星星一定在左边。那么,每次我们就看看这颗星星前面有多少颗星星比这颗星星的 xx 小即可。

这样不仅是在下方还是在左边。那么,怎么统计呢?其实,就相当于把当前的 xx 加 11。下次看有多少个的时候直接看看有几个 11 即可。

code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int a[500005];
int b[500005];
int c[500005];
int ans[500005];
int lowbit(int x){
	return x & (-x);
}
void modify(int x,int k,int val){
	while(x <= k){
		c[x] += val;
		x += lowbit(x);
	}
	return ;
}
int query(int x){
	int ans = 0;
	while(x > 0){
		ans += c[x]; 
		x -= lowbit(x);
	}
	return ans; 
} 
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin >> n;
	for(int i = 1 ; i <= n ; i ++){
		cin >> a[i] >> b[i];
		a[i] ++;
		ans[query(a[i])] ++;
		modify(a[i] , 32005 , 1);
	}
	for(int i = 0 ; i < n ; i ++){
		cout << ans[i] << "\n";
	}
	return 0;
} 

U438255 ١١(❛ᴗ❛)【树状数组】-校门外的树

这道题目因为种树是有一个区间的。那么,可以根第一题一样,分成两个部分。其中一个当成减数,把多余的减掉。

code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
int a[500005];
int c[500005];
int d[500005];
int lowbit(int x){
	return x & (-x);
}
void modify1(int x,int k,int val){
	while(x <= k){
		c[x] += val;
		x += lowbit(x);
	}
	return ;
}
void modify2(int x,int k,int val){
	while(x <= k){
		d[x] += val;
		x += lowbit(x);
	}
	return ;
}
int query1(int x){
	int ans = 0;
	while(x > 0){
		ans += c[x];
		x -= lowbit(x);
	}
	return ans; 
} 
int query2(int x){
	int ans = 0;
	while(x > 0){
		ans += d[x];
		x -= lowbit(x);
	}
	return ans; 
} 
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin >> n >> m;
	while(m --){
		int ch,x,y;
		cin >> ch >> x >> y;
		if(ch == 1){
			modify1(x , n , 1);
			modify2(y , n , 1);
		}else{
			cout << query1(y) - query2(x - 1) << "\n";
		}
	}
	return 0;
} 
原文