跳到正文

个人记录

25.7.20-树状数组测试总结

Xiao_QwQ约 12 分钟
原文
文章信息与版本

文章信息

发布
2025年7月20日
收录
2025年12月12日
更新
2026年11月6日
篇幅
约 12 分钟

版本

  1. v1当前

树状数组测试总结

自我总结

做的还算可以,也是拿到的接近 500500 的分数。
只不过在T6,T7暴力的时候没有想清楚具体的暴力方法,导致我只是通过了样例,并没有实际分数。
总之,我还要在暴力得分上多加总结和小心。

测试题目

T1——U438237 [树状数组+线段树]-模板

思路

原题+模板,不必多说。

代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int Max=1e6+10;
const int INF=1e18;
int c[Max];
int n,q;
int low(int x){
	return (x&(-x));
}
int dis(int x){
	int sum=0;
	while(x){
		sum+=c[x];
		x-=low(x);
	}
	return sum;
}
void modify(int x,int v){
	while(x<=n){
		c[x]+=v;
		x+=low(x);
	}
	return ;
}
signed main(){
	cin>>n>>q;
	for(int i=1;i<=n;i++){
		int x;cin>>x;
		modify(i,x);
	}
	while(q--){
		int op,x,y;
		cin>>op>>x>>y;
		if(op==1){
			modify(x,y);
		}else{
			cout<<dis(y)-dis(x-1)<<"\n";
		}
	}
	return 0;
} 

T2——U438249 [树状数组]-清点人数

思路

模板,我们只需要在输入 opop 的时候进行多次 ifif 判断即可。

代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int Max=1e6+10;
int n,k;
int c[Max];
int low(int x){
	return (x&(-x));
}
int dis(int x){
	int sum=0;
	while(x){
		sum+=c[x];
		x-=low(x);
	}
	return sum;
}
void modify(int x,int v){
	while(x<=n){
		c[x]+=v;
		x+=low(x);
	}
	return ;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
//	freopen("T2.in","r",stdin);
//	freopen("T2.out","w",stdout);
	cin>>n>>k;
	for(int i=1;i<=k;i++){
		char op;cin>>op;
		int x,y;
		if(op=='A'){
			cin>>x;
			cout<<dis(x)-dis(0)<<"\n";
		}else if(op=='B'){
			cin>>x>>y;
			modify(x,y);
		}else{
			cin>>x>>y;
			modify(x,-y);
		}
	}
	return 0;
} 

T3——U584613 树状数组2

思路

模板题,只不过当中的 cc 数组存储的是类似于 dpdp 的差分,而非前缀和。

代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int Max=1e6+10;
int n,q;
int c[Max],a[Max];
int low(int x){
	return (x&(-x));
}
int dis(int x){
	int sum=0;
	while(x){
		sum+=c[x];
		x-=low(x);
	}
	return sum;
}
void modify(int x,int v){
	while(x<=n){
		c[x]+=v;
		x+=low(x);
	}
	return ;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>n>>q;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		modify(i,a[i]-a[i-1]);
	}
	for(int i=1;i<=q;i++){
		int op,x,y;
		cin>>op;
		if(op==1){
			int l,r,k;
			cin>>l>>r>>k;
			modify(l,k);
			modify(r+1,-k);
		}else{
			int x;cin>>x;
			cout<<dis(x)<<"\n";
		}
	}
	return 0;
} 

T4——U438285 [树状数组]-数星星 Stars

思路

原题,一边输入一边查找,记住我们需要先行进行查找操作,不然下一步修改都无从下手。
第二点,当数据 xx 为 00 的时候,这个 disdis 会进入死循环。所以我们要将输入时的 xx 进行自增操作,以防数据溢出。

代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int Max=1e6+10;
int n,q;
int c[Max],ans[Max];
int low(int x){
	return (x&(-x));
}
int dis(int x){
	int sum=0;
	while(x){
		sum+=c[x];
		x-=low(x);
	}
	return sum;
}
void modify(int x,int v){
	while(x<=40000){
		c[x]+=v;
		x+=low(x);
	}
	return ;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
//	freopen("T4.in","r",stdin);
//	freopen("T4.out","w",stdout);
	cin>>n;
	for(int i=1;i<=n;i++){
		int x,y;
		cin>>x>>y;
        x++;
		ans[dis(x)]++;
		modify(x,1);
	}
	for(int i=0;i<n;i++){
		cout<<ans[i]<<"\n";
	}
	return 0;
} 

T5——U438255 [树状数组]-校门外的树

思路

原题。
我们使用括号标记法,弄两个树状数组,一个记录左括号,一个记录右括号。
最后将处在 ll -> rr 区间的左右括号相减即可。

代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int Max=1e6+10;
int n,m;
int c[Max][5],ans[Max];
int low(int x){
	return (x&(-x));
}
int dis(int x,int k){
	int sum=0;
	while(x){
		sum+=c[x][k];
		x-=low(x);
	}
	return sum;
}
void modify(int x,int v,int k){
	while(x<=n){
		c[x][k]+=v;
		x+=low(x);
	}
	return ;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
//	freopen("T5.in","r",stdin);
//	freopen("T5.out","w",stdout);
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int op,x,y;
		cin>>op>>x>>y;
		if(op==1){
			modify(x,1,1);
			modify(y,1,2);
		}else{
			cout<<dis(y,1)-dis(x-1,2)<<"\n";
		}
	}
	return 0;
} 

T6——P8165 [eJOI 2021] AddK

思路

沟槽的addk让我没有上500
区间修改+区间查找。
差分树状数组即可,只不过推导公式有点费时间。

上图是公式推导,我没画图,直接用的陈老师的图片。

代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int Max=1e6+10;
int n,k,q;
int c[Max][3];
int id[Max];
int a[Max];
int low(int x){
	return (x&(-x));
}
int dis(int x,int tmp){
	int sum=0;
	while(x){
		sum+=c[x][tmp];
		x-=low(x);
	}
	return sum;
}
void modify(int x,int v,int tmp,int typ){
	while(x<=tmp){
		c[x][typ]+=v;
		x+=low(x);
	}
	return ;
}
int cal(int x){
	if(x<0)return 0;
	return x*dis(x,0)-dis(x,1);
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		modify(i,a[i],n,0);
		modify(i,1ll*a[i]*(i-1),n,1);
	}
	cin>>q;
	for(int i=1;i<=q;i++){
		int op;
		cin>>op;
		if(op==1){
			for(int i=1;i<=k;i++)cin>>id[i];
			int tmp=a[id[1]];
			for(int i=1;i<k;i++){
				modify(id[i],a[id[i+1]]-a[id[i]],n,0);
				modify(id[i],1LL*(a[id[i+1]]-a[id[i]])*(id[i]-1),n,1);
				a[id[i]]=a[id[i+1]];
			}
			modify(id[k],tmp-a[id[k]],n,0);
			modify(id[k],1ll*(tmp-a[id[k]])*(id[k]-1),n,1);
			a[id[k]]=tmp;
		}else{
			int l,r,m;
			cin>>l>>r>>m;
			cout<<cal(r)-cal(l+m-2)-(cal(r-m)-cal(l-2))<<"\n";
		}
	}
	return 0;
} 

T7——P7629 [COCI 2011/2012 #1] SORT

思路

下次能不能把题目翻译一下?
给我们的是不规则的数列,让我们通过更改逆序对的个数确定最后的答案。
但是我们要先行变成一个区间单调减少的数列,方便我们进行逆序对的操作。

具体解释见上图。

代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int Max=1e6+10;
int n,m;
int a[Max];
struct bt{
	int c[Max];
	int low(int x){
		return (x&(-x));
	}
	int dis(int x){
		int sum=0;
		while(x){
			sum+=c[x];
			x-=low(x);
		}
		return sum;
	}
	void modify(int x,int v){
		while(x<=n){
			c[x]+=v;
			x+=low(x);
		}
		return ;
	}
}T;
int ans=0;
void tf(int l,int r){
	while(l<=r){
		swap(a[l],a[r]);
		++l,--r;
	}
	ans++;
	return ;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	int las=1;
	for(int i=2;i<=n;i++){
		if(a[i]>a[i-1]){
			tf(las,i-1);
			las=i;
		}
	}
	tf(las,n);
	for(int i=n;i>=1;--i){
		ans+=T.dis(a[i]);
		T.modify(a[i],1);
	}
	cout<<ans<<"\n";
	return 0;
} 

完结撒花!

洛谷图库内存不够了,就没有每日一图了。

原文