hdu1541——Stars(偏序问题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541

hdu1541——Stars

二维偏序问题。之前没想过树状数组能解决这样的问题,留个纪念,之后再写一篇洛谷的题洛谷P1020 导弹拦截

注意题目中input:y升序输入  这点非常关键

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 15002;
const int range = 32002;
int an***axn],c[range];
int lowbit(int x)
{
    return x&-x;
}
void update(int x)
{
    while(x<=range)
    {
        c[x]++; x+=lowbit(x);
    }
}
int query(int x)
{
    int sum=0;
    while(x>0)
    {
        sum+=c[x]; x-=lowbit(x);
    }
    return sum;
}
int main()
{
    int n,x,y;
    while(~scanf("%d",&n))
	{
		memset(c,0,sizeof(c));
	    memset(ans,0,sizeof(ans));
	    for(int i=1;i<=n;i++)
	    {
	        scanf("%d%d",&x,&y);
	        ans[query(x+1)]++;
			update(x+1);       
	    }
	    for(int i=0;i<n;i++) printf("%d\n",ans[i]);
    }
	return 0;
}


全部评论

相关推荐

美丽的95后准备进厂:第二个是外卖➕点评吧,很眼熟
点赞 评论 收藏
分享
影04714:把图书管理系统那个项目经验内容适当的减少掉,然后改成据为己有不要说团队项目,因为图书管理系统这类常见的谁来了都能独立写出来,提问能圆过来即可
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务