题目大意:给你N颗星星的坐标,问每个等级的星星有多少颗
解题思路:因为y是升序的,所以可以不考虑y了
求解星星所在的等级,其实就是求解前面有几个星星的x坐标是小于等于当前的x坐标的,也就是以该点为右端点的逆序对有多少个
#include <cstdio>
#include <cstring>
const int N = 15010;
const int M = 32010;
int n;
int bit[M], ans[N];
inline int lowbit(int x) {
return x & (-x);
}
void Modify(int x, int w) {
while (x < M) {
bit[x] += w;
x += lowbit(x);
}
}
int Query(int x) {
int ans = 0;
while (x) {
ans += bit[x];
x -= lowbit(x);
}
return ans;
}
void solve() {
memset(bit, 0, sizeof(bit));
memset(ans, 0, sizeof(ans));
int x, y;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &x, &y);
x++;
Modify(x, 1);
ans[Query(x) - 1]++;
}
for (int i = 0; i <= n - 1; i++)
printf("%d\n", ans[i]);
}
int main() {
while (scanf("%d", &n) != EOF) solve();
return 0;
}