文章目录
原理图
作用:
动态维护区间操作
性质:
- 每个根节点 t [ x ] 所覆盖的长度为 lowbit(x)
- 每个节点 t [ x ] 的父节点是 t [ x + lowbit(x) ]
- 整棵树的深度为 logn + 1
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.区间修改,单点查询:
区间修改 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;
}