1. 程式人生 > >poj2352+2481 stars+cows 樹狀陣列

poj2352+2481 stars+cows 樹狀陣列

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");
	}
}