一、题目链接
二、题目分析
(一)算法标签
树状数组
(二)解题思路
每输入一个坐标(x, y), 先求当前小于等于x的点的个数(query()), 再把该点加入到tr数组中(update()).
三、AC代码
解法一:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 32010;
int n;
int tr[N], level[N];
int lowbit(int x)
{
return x & -x;
}
void update(int x, int c)
{
for (int i = x; i < N; i += lowbit(i))
tr[i] += c;
}
int query(int x)
{
int res = 0;
for (int i = x; i; i -= lowbit(i) )
res += tr[i];
return res;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i ++ )
{
int x, y;
scanf("%d%d", &x, &y);
x ++ ;
level[query(x)] ++ ;
update(x, 1);
}
for (int i = 0; i < n; i ++ )
printf("%d\n", level[i]);
return 0;
}