POJ 2352 Stars 树状数组

Stars
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13167 Accepted: 5654

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

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

Source

#include<stdio.h>
int level[15005];//放的是星星的层次
int a[32010];//放的是树状数组,是N的
inline int Lowbit(int x)
{
    return x&(-x);//计算的是2^P,P就是X的0的个数
}
void Update(int x)//更新,每加入一个星星,那条路都要更新
{
    for(int i=x;i<32002;i+=Lowbit(i))//直到最大值,都要更新
        a[i]++;
}
int Getsum(int x)//取和,就是以X为最大值那个点
{
    int temp=0;
    for(int i=x;i>0;i-=Lowbit(i))
        temp+=a[i];//真正明白那个图,这是不会重复计算的
    return temp;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0; i<=n; i++)
            level[i]=a[i]=0;
        int k=n;
        while(k--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            level[Getsum(x+1)]++;//注意是X+1
            Update(x+1);
        }
        for(int i=0;i<n;i++)
        printf("%d/n",level[i]);
    }
    return 0;
}

还在为数据库管理、代码开发和系统优化的繁杂任务焦头烂额?这款软件就是你翘首以盼的 “开发界瑞士军刀”,全方位赋能,让开发工作事半功倍! 1.数据库深度管理,掌控全局 当数据库遭遇死锁,它能快速定位并提供详尽死锁信息,助你秒解危机;实时监控数据库链接状态、用户情况、端口号等,就像给数据库安装了 “健康监测仪”。从设置最大连接数、把控连接超时,到获取服务器 CPU、磁盘等硬件信息,再到内存、缓存、数据库大小的精细化管理,无论是基础查看,还是清理、收缩、优化等操作,都能轻松完成,让数据库时刻保持最佳性能状态。 2.代码开发神器,效率飙升 无需从零开始编写代码,无论是 netframework 经典三层架构,还是 netcore 的经典代码,甚至是高并发 api 接口代码,都能一键自动生成,大幅缩短开发周期。针对 SQL,它提供耗时优化策略,精准定位低效代码,还能保障事务安全回滚,避免数据错误。json 格式化、数据库连接字符串强优化,每一个细节都为提升开发效率而设计。 3.全栈优化专家,性能拉满 不仅专注后端,对 web 前端也能进行 css、js、html 压缩优化,减小页面加载压力,提升用户体验;在服务器端,深入研究安全防护,优化应用程序与缓存,构建牢不可破的安全防线。同时,它还是版权保护卫士,从图片到代码,为你的创意成果保驾护航,杜绝侵权风险。 4.智能解析,便捷高效 面对二维码、条形码,轻松实现解析,助力开发更多实用功能,满足多样化业务需求。 开发之路道阻且长,但有了这款软件,复杂难题迎刃而解,高效开发触手可及!快来体验,开启你的开发新纪元,让每一行代码都都迸发无限可能!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值