跳到正文

个人记录

7.20考试

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

文章信息

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

版本

  1. v1当前

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

模板题,上课讲过,使用树状数组。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,q,c[1000006];
int lowbit(int x){
	return (x&(-x));
}
int query(int x){
	int sum=0;
	while(x){
		sum+=c[x];
		x-=lowbit(x);
	}
	return sum;
} 
void modify(int x,int v){
	while(x<=n){
		c[x]+=v;
		x+=lowbit(x);
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin>>n>>q;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		modify(i,x);
	}
	while(q--){
		int op,l,r;
		cin>>op>>l>>r;
		if(op==1){
			modify(l,r);
		}else{
			cout<<query(r)-query(l-1)<<endl;
		}
	}
	return 0;
} 

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

与模板题类似,不过有区间减少数值,模板上改动一下即可。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,k,c[1000006];
int lowbit(int x){
	return (x&(-x));
}
int query(int x){
	int sum=0;
	while(x){
		sum+=c[x];
		x-=lowbit(x);
	}
	return sum;
} 
void modify(int x,int v,char k){
	if(k=='B'){
		while(x<=n){
			c[x]+=v;
			x+=lowbit(x);
		}
	}else{
		while(x<=n){
			c[x]-=v;
			x+=lowbit(x);
		}
	}
	
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin>>n>>k;
	while(k--){
		char op;
		int m,p;
		cin>>op;
		if(op=='A'){
			cin>>m;
			cout<<query(m)<<endl;
		}else{
			cin>>m>>p;
			modify(m,p,op);
		}
	}
	return 0;
} 

U584613 树状数组2

模板区间修改,单点查询,使用树状数组。

C++
#include<bits/stdc++.h>
#define int long long 
using namespace std;
const int maxn=1e6+7;
int n,m,a[maxn],d[maxn],c[maxn];
int lowbit(int x){
	return (x&(-x));
}
int query(int x){
	int sum=0;
	while(x){
		sum+=c[x];
		x-=lowbit(x);
	}
	return sum;
} 
void modify(int x,int v){
	while(x<=n){
		c[x]+=v;
		x+=lowbit(x);
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		d[i]=a[i]-a[i-1];
		modify(i,d[i]);
	}
	while(m--){
		int op,l,r,x,k;
		cin>>op;
		if(op==1){
			cin>>l>>r>>x;
			modify(l,x);
			modify(r+1,-x);
		}else{
			cin>>k;
			cout<<query(k)<<endl;
		}
	}
	
	return 0;
}

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

上课讲过,题目中保证了星星坐标按 yy 坐标增序给出,以 xx 为下标,找所有比当前点小的点的数量。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5;
int n,k,c[maxn],ans[maxn];
int lowbit(int x){
	return (x&(-x));
}
int query(int x){
	int ans=0;
	while(x){
		ans+=c[x];
		x-=lowbit(x);
	}
	return ans;
} 
void modify(int x,int v){
	while(x<=40000){
		c[x]+=v;
		x+=lowbit(x);
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

//	system("fc T4.out T4.ans");
	cin>>n;
	for(int i=1;i<=n;i++){
		int xx,y;
		cin>>xx>>y;
		int sum=0;
		xx++;
		sum=query(xx);
		ans[sum]++;
		modify(xx,1);
	}
	for(int i=0;i<n;i++){
		cout<<ans[i]<<endl;
	}
	return 0;
} 

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

上课讲过,使用左右括号标记一种数种植的开始与结束,最后答案是 [1,R][1,R] 中左括号的数量 −− 区间 [1,L−1][1,L−1] 中右括号的数量。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=5e4+7;
int n,m,c[maxn][2];
int lowbit(int x){
	return (x&(-x));
}
int query(int x,int k){
	int sum=0;
	while(x){
		sum+=c[x][k];
		x-=lowbit(x);
	}
	return sum;
} 
void modify(int x,int v,int k){
	while(x<=n){
		c[x][k]+=v;
		x+=lowbit(x);
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

//	system("fc T5.out T5.ans");
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int op,l,r;
		cin>>op>>l>>r;
		if(op==1){
			modify(l,1,0);
			modify(r,1,1);
		}else{
			cout<<query(r,0)-query(l-1,1)<<endl;
		}
	}
	return 0;
} 

P8165 [eJOI 2021] AddK

操作一:由于 K<=10K<=10,直接暴力修改。

操作二:使用树状数组加速求区间和,区间 [l,r][l,r]内所有长度为 mm的连续子序列和为:区间 [l,l+m−1]+[l+1,l+m]+[l+2,l+m+1]......[l,l+m-1]+[l+1,l+m]+[l+2,l+m+1]......。令

bj=∑i=1jb_j=\sum_{i = 1}^{j}

答案就是

∑i=l+m−1r(bi−bi−m)\sum_{i=l+m-1}^r(b_i-b_{i-m})

令

ci=∑j=1ibjc_i=\sum_{j=1}^i b_j

答案可以转为

cr+cl+m−2−(cr−m+cl−2)c_r+c_{l+m-2}-(c_{r-m}+c_{l-2})
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=5e5+4;
int a[maxn],id[maxn],c[maxn][2],n,k,t;
int lowbit(int x){
	return (x&(-x));
}
int query(int x,int k){
	int sum=0;
	while(x){
		sum+=c[x][k];
		x-=lowbit(x);
	}
	return sum;
} 
void modify(int x,int v,int k){
	while(x<=n){
		c[x][k]+=v;
		x+=lowbit(x);
	}
}
int f(int x){
	if(x<0){
		return 0;
	}
	return x*query(x,0)-query(x,1);
}
signed main(){
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		modify(i,a[i],0);
		modify(i,a[i]*(i-1),1);
	}
	cin>>t;
	while(t--){
		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]],0);
				modify(id[i],(a[id[i+1]]-a[id[i]])*(id[i]-1),1);
				a[id[i]]=a[id[i+1]];
			}
			modify(id[k],tmp-a[id[k]],0);
			modify(id[k],(tmp-a[id[k]])*(id[k]-1),1);
			a[id[k]]=tmp;
		}else{
			int l,r,m;
			cin>>l>>r>>m;
			cout<<f(r)-f(l+m-2)-f(r-m)+f(l-2)<<endl;
		}
	}
	
	return 0;
}

P7629 [COCI 2011/2012 #1] SORT

题面翻译后就是:

代码
给定一个需要排序的数组a[]
while(a不是一个排序好的序列){
  将a划分成多个连续的下降区间
  将所有这些区间(翻转)
}

将连续非严格的递减子序列当做一个翻转区间,如果 a[i−1]<a[i]a[i-1]<a[i] 说明 a[i]a[i]需要弄一个区间。

排序后要翻转的区间长度一定为22,只需要冒泡排序就行了,每冒泡排序一次,逆序对的个数就减少一个,排序的次数为逆序对的个数。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1e5+7;
int n,m;
int c[maxn];
int a[maxn];
bool vis[maxn];
int lowbit(int x){
	return (x&(-x));
}
int query(int x){
	int sum=0;
	while(x){
		sum+=c[x];
		x-=lowbit(x);
	}
	return sum;
} 
void modify(int x,int v){
	while(x<=n){
		c[x]+=v;
		x+=lowbit(x);
	}
}
int ans=0;
void fz(int l,int r){
	while(l<=r){
		swap(a[l],a[r]);
		l++;
		r--;
	}
	ans++;
	return ;
}
signed main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	int last=1;
	for(int i=2;i<=n;i++){
		if(a[i]>a[i-1]){
			fz(last,i-1);
			last=i;
		}
	}
	fz(last,n);
	for(int i=n;i>=1;i--){
		ans+=query(a[i]);
		modify(a[i],1);
	}
	cout<<ans;
	return 0;
}
原文