关闭

Stars

标签: 树状数组
33人阅读 评论(0) 收藏 举报
分类:
链接:http://poj.org/problem?id=2352

题目: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.

题意:在坐标系的第一象限有几颗星星,每颗星星(m,n)的等级定义为满足(x<=m,y<=n)的星星的个数。

分析:所给的数据是按照y坐标升序的,其实是个提示,主要的思想就是要一层一层按照y轴去数。数据量中等,用暴力做的话,需要维护一个前缀和,我没试过,不过应该是可以做的,当时做的时候刚好学了树状数组,所以就用树状数组来做了。

题解:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <functional>
#include <cmath>
#include <cctype>
#include <cfloat>
#include <climits>
#include <complex>
#include <deque>
#include <list>
#include <set>
#include <utility>
using namespace std;

int cla[32010];
int bit[32010];
pair<int,int> ps[32010];
int n;

void add(int x)
{
	while(x<=32005){
		bit[x]+=1;
		x+=x&-x;
	}
}

int countn(int x)
{
	int an=0;
	while(x){
		an+=bit[x];
		x-=x&-x;
	}
	return an;
}

int main()
{
	//freopen("in.txt","r",stdin);
	scanf("%d",&n);
	memset(bit,0,sizeof bit);
	memset(cla,0,sizeof cla);
	int x,y;
	for(int i=1;i<=n;i++){
		scanf("%d %d",&x,&y);
		add(x+1);
		cla[countn(x+1)-1]++;
	}
	for(int i=0;i<n;i++){
		printf("%d\n",cla[i]);
	}
	return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3192次
    • 积分:597
    • 等级:
    • 排名:千里之外
    • 原创:59篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    友情链接