AcWing 1265. 数星星
原题链接
中等
作者:
冷丁Hacker
,
2020-09-19 17:07:20
,
所有人可见
,
阅读 480
树状数组
#include<bits/stdc++.h>
using namespace std;
const int N = 32010;
int tr[N],level[N];
int lowbit(int x) {
return x & (-x);
}
//加操作 i从x~N i+=lowbit(i)
void add(int x, int y) {
for (int i = x; i < N; i += lowbit(i)) {
tr[i] += y;
}
}
//求和操作 i从x~0 i-=lowbit(i)
int sum(int x) {
int res = 0;
for (int i = x; i > 0; i-=lowbit(i)) {
res +=tr[i] ;
}
return res;
}
int n;
int main()
{
cin >> n;
int temp = n;
while (n--) {
int x, y;
cin >> x >> y;
x++;
level[sum(x)]++;
add(x, 1);
}
for (int i = 0; i < temp; i++) {
cout << level[i] << endl;
}
return 0;
}