您的位置:首页 > 其它

POJ 2352 Stars

2017-05-07 19:24 399 查看
http://poj.org/problem?id=2352

题意:

给出每个星星的x、y坐标,计算每颗星星左方和正下方的星星个数。

思路:
由于星星是根据y坐标递增的基础上再根据x坐标递增的顺序给出,所以我们只需要考虑横坐标上的星星情况,至于正下方的星星个数,我们只需要记录一下最后加上即可。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
using namespace std;

const int maxn=32000+5;

int n;
int c[maxn];
int ans[maxn];
int num[maxn];

int lowbit(int x)
{
return x&-x;
}

int sum(int x)
{
int ret=0;
while(x>0)
{
ret+=c[x];
x-=lowbit(x);
}
return ret;
}

void add(int x,int d)
{
while(x<=maxn)
{
c[x]+=d;
x+=lowbit(x);
}
}

int main()
{
//freopen("D:\\input.txt","r",stdin);
scanf("%d",&n);
for(int i=0;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
x++;   //注意这儿需要+1,因为x为0也是有可能的
add(x,1);
int p=sum(x-1);
p+=num[x];   //加上正下方的星星个数
ans[p]++;
num[x]++;
}
for(int i=0;i<n;i++)
printf("%d\n",ans[i]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: