跳到正文

个人记录

#10114. 「一本通 4.1 例 2」数星星 Stars

yzxoi约 2 分钟
原文
文章信息与版本

文章信息

发布
2019年1月27日
收录
2026年3月20日
更新
2026年11月12日
篇幅
约 2 分钟

版本

  1. v1当前

#10114. 「一本通 4.1 例 2」数星星 Stars

题意

给定 nn 个点,定义每个点的等级是在该点左下方(含正左、正下)的点的数目,试统计每个等级有多少个点。

思路

这不就是裸的树状数组吗?

题目都按顺序(y的增序)。。。

C++
#include<bits/stdc++.h>
using namespace std;
inline int read(){
	char ch=getchar();int res=0,f=1;
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar();
	return res*f;
}
inline void write(int x){
	if(x<0) putchar('-'),x=-x;
	if(x<10) putchar(x+'0');
	else{
		write(x/10);
		putchar(x%10+'0');
	}
}
int n,x,y,ans[1500010];
int a[1500010];
void add(int x,int y){
	for(int i=x;i<=1500000;i+=i&(-i)){
		a[i]+=y;
	}
}
int getsum(int x){
	int sum=0;
	for(int i=x;i;i-=i&(-i)){
		sum+=a[i];
	}
	return sum;
}
int main(){
	n=read();
	for(int i=1;i<=n;i++){
		x=read();y=read();
		int t=getsum(x+1);
		ans[t]++;
		add(x+1,1);
	}
	for(int i=0;i<n;i++){
		write(ans[i]);putchar('\n');
	}
	return 0;
}
原文