题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541
刚开始知道了先按列排序再按行排序,一直没明白输出是怎么回事,可以联想到是左下角的所有的星的个数
注意题目上的描述: There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.
这句话的意思就是只比较小于等于这个x的星的个数,而不用比较y,不行的话画画图理解一下
另外一个坑: 0<=X,Y<=32000 这个范围,注意树状数组不可以处理x为0 的数据 要从1开始,所有输入x+1
代码:
import java.util.Scanner;
class first{
static int n= 16000;
static int [] c = new int[n];//答案的数组
static int [] a = new int[n]; //维护树状数组的数组
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N= sc.nextInt();
for(int i =0;i<N;i++){
int x = sc.nextInt();
int y = sc.nextInt();
insert(x+1,1);
int p = getsum(x+1)-1;
c[p]++;
}
for(int i = 0;i<N;i++){
System.out.println(c[i]);
}
}
static void insert(int t,int d){
while(t<=n){
a[t]+= d;
t+= lowbit(t);
}
}
static int getsum(int t){
int as = 0;
while (t>0){
as+= a[t];
t-= lowbit(t);
}
return as;
}
static int lowbit(int x){
return x&(-x);
}
}
有点自闭,,,,wa了哎,找了好久不知道哪里错了,,,,qwq
再看看改过来