poj2352+2481 stars+cows 树状数组

本文介绍了一种使用树状数组解决天文学中星图分布问题的方法,并通过两个实例展示了如何计算不同级别星星的数量及判断奶牛偏好的草地范围内的强者数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars. 

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

The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). 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. 

Output

The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.

Sample Input

5
1 1
5 1
7 1
3 3
5 5

Sample Output

1
2
1
1

0

新学了树状数组,试试手

#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
#define lowbit(i) ((-i)&i)
int c[50005],level[50005],n;
void add(int x)
{
	while(x<=33000){
		c[x]++;
		x+=lowbit(x);
	}
}
int sum(int x)
{
	int s=0;
	while(x){
		s+=c[x];
		x-=lowbit(x);
	}
	return s;
}
int main()
{
	int n,x,y,i;
	while(~scanf("%d",&n))
	{
		memset(level,0,sizeof(level));
		memset(c,0,sizeof(c));
		for(i=0;i<n;i++)
		{
			scanf("%d%d",&x,&y);
			x++;
			level[sum(x)]++;
			add(x);
		}
		for(i=0;i<n;i++)printf("%d\n",level[i]);
	}
}

Description

Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good. 

Farmer John has N cows (we number the cows from 1 to N). Each of Farmer John's N cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval [S,E]. 

But some cows are strong and some are weak. Given two cows: cowi and cowj, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cowi is stronger than cowj

For each cow, how many cows are stronger than her? Farmer John needs your help!

Input

The input contains multiple test cases. 
For each test case, the first line is an integer N (1 <= N <= 105), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 105) specifying the start end location respectively of a range preferred by some cow. Locations are given as distance from the start of the ridge. 

The end of the input contains a single 0.

Output

For each test case, output one line containing n space-separated integers, the i-th of which specifying the number of cows that are stronger than cowi

Sample Input

3
1 2
0 3
3 4
0

Sample Output

1 0 0
差不多的....但是我还是没看出来....

#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
#define lowbit(i) (-i)&i
struct node{
	int x,y,id;
}a[100005];
int n,b[100005],c[100005];
bool cmp(node a,node b)
{
	if(a.y!=b.y)return a.y>b.y;
	return a.x<b.x;
}
void add(int i){
	while(i<=100005){
		b[i]++;
		i+=lowbit(i);
	}
}
int sum(int i)
{
	int s=0;
	while(i)
	{
		s+=b[i];
		i-=lowbit(i);
	}
	return s;
}
int main()
{
	int i;
	while(~scanf("%d",&n),n)
	{
		memset(b,0,sizeof(b));
		memset(c,0,sizeof(c));
		for(i=0;i<n;i++)
		{
			scanf("%d%d",&a[i].x,&a[i].y);
			a[i].id=i;
			a[i].x++,a[i].y++;
		}
		sort(a,a+n,cmp);
		c[a[0].id]=sum(a[0].x);
		add(a[0].x);
		for(i=1;i<n;i++)
		{
			if(a[i].x==a[i-1].x&&a[i].y==a[i-1].y)c[a[i].id]=c[a[i-1].id];
			else c[a[i].id]=sum(a[i].x);
			add(a[i].x);
		}
		printf("%d",c[0]);
		for(i=1;i<n;i++)printf(" %d",c[i]);
		printf("\n");
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值