| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 37709 | Accepted: 16423 |
Description

For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.
You are to write a program that will count the amounts of the stars of each level on a given map.
Input
Output
Sample Input
5 1 1 5 1 7 1 3 3 5 5
Sample Output
1 2 1 1 0
Hint
给出n个星星的坐标,星星的级数等于左下角有几个星星,输出n个星星的级数。
简单的线段树,把坐标转化,求和存到level数组即可。
AC代码(线段树):
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int maxn = 32005;
#define mid ((left + right) >> 1)
#define lson rt << 1, left, mid
#define rson rt << 1 | 1, mid + 1, right
int sum[maxn << 2], level[maxn << 2];
void update(int rt, int left, int right, int data)
{
++sum[rt];
if(left == right) return ;
if(data <= mid) update(lson, data);
else update(rson, data);
}
int query(int rt, int left, int right, int l, int r)
{
if(l == left && right == r) return sum[rt];
int m = mid;
if(r <= m) return query(lson, l, r);
else if(l > m) return query(rson, l, r);
else return query(lson, l, m) + query(rson, m + 1, r);
}
int main(int argc, char const *argv[])
{
/* code */
int n, x, y;
while(scanf("%d", &n) != EOF) {
memset(sum, 0, sizeof(sum));
memset(level, 0, sizeof(level));
for(int i = 0; i < n; ++i) {
scanf("%d%d", &x, &y);
++x;
++level[query(1, 1, maxn, 1, x)];
update(1, 1, maxn, x);
}
for(int i = 0; i < n; ++i)
printf("%d\n", level[i]);
}
return 0;
}树状数组思路一样的~
AC代码(树状数组):
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int maxn = 32005;
int c[maxn], level[maxn], n;
int lowbit(int x)
{
return x & (-x);
}
int sum(int n)
{
int sum = 0;
while(n > 0) {
sum += c[n];
n -= lowbit(n);
}
return sum;
}
void add(int x)
{
while(x <= maxn) {
++c[x];
x += lowbit(x);
}
}
int main(int argc, char const *argv[])
{
int n, x, y;
while(scanf("%d", &n) != EOF) {
memset(level, 0, sizeof(level));
memset(c, 0, sizeof(c));
for(int i = 0; i < n; ++i) {
scanf("%d%d", &x, &y);
++x;
level[sum(x)]++;
add(x);
}
for(int i = 0; i < n; ++i)
printf("%d\n", level[i]);
}
return 0;
}
1916



被折叠的 条评论
为什么被折叠?



