Stars
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 54352   Accepted: 23386

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.
题目意思:
给你n给点的star的坐标,star左下角star的个数是star的等级(包括左下边界,即x相同或者y相同
最好输出等级1到等级n的star个数
分析:树状树主要用来解决区间问题
所以这题可以用树状数组写
一开始给出的数据是已经按照y升序排序好了的,如果y相同则按照x升序排序
所以统计某个等级的个数就是统计x前面比它小的星星的数量
但是有需要注意的地方
树状数组是不能统计0的
所以每个x都要++
这也也不会影响统计的结果
code:
#include<queue>
#include<set>
#include<cstdio>
#include <iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define max_v 320010
int a[max_v];
int c[max_v];
//C数组C[i] = a[i – 2^k + 1] + … + a[i] ,k为二进制下某尾0的个数。
int lowbit(int x)//返回2的k次方的值
{
return x&(-x);
}
void update(int x,int d)//更新树状数组
{
while(x<max_v)
{
c[x]=c[x]+d;
x=x+lowbit(x);
}
}
int getsum(int x)//就是把C数组全部加起来
{
int res=;
while(x>)
{
res=res+c[x];
x=x-lowbit(x);
}
return res; }
int main()
{
int n,x,y;
while(~scanf("%d",&n))
{
memset(c,,sizeof(c));
memset(a,,sizeof(a));
for(int i=;i<n;i++)
{
scanf("%d %d",&x,&y);//下标可能从0开始,所以要x+1
a[getsum(x+)]++;//求出其左下角star个数
update(x+,);//更新树状数组
}
for(int i=;i<n;i++)
{
printf("%d\n",a[i]);
}
}
return ;
}
/*
题目意思:
给你n给点的star的坐标,star左下角star的个数是star的等级(包括左下边界,即x相同或者y相同
最好输出等级1到等级n的star个数 分析:树状树主要用来解决区间问题
所以这题可以用树状数组写 一开始给出的数据是已经按照y升序排序好了的,如果y相同则按照x升序排序
所以统计某个等级的个数就是统计x前面比它小的星星的数量
但是有需要注意的地方
树状数组是不能统计0的
所以每个x都要++
这也也不会影响统计的结果 */

POJ 2352 stars (树状数组入门经典!!!)的更多相关文章

  1. POJ 2352 【树状数组】

    题意: 给了很多星星的坐标,星星的特征值是不比他自己本身高而且不在它右边的星星数. 给定的输入数据是按照y升序排序的,y相同的情况下按照x排列,x和y都是介于0和32000之间的整数.每个坐标最多有一 ...

  2. hdu 1541/poj 2352:Stars(树状数组,经典题)

    Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  3. POJ 2352 &amp;&amp; HDU 1541 Stars (树状数组)

    一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~ ...

  4. 树状数组入门 hdu1541 Stars

    树状数组 树状数组(Binary Indexed Tree(B.I.T), Fenwick Tree)是一个查询和修改复杂度都为log(n)的数据结构.主要用于查询任意两位之间的所有元素之和,但是每次 ...

  5. POJ 2299 Ultra-QuickSort 求逆序数 (归并或者数状数组)此题为树状数组入门题!!!

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 70674   Accepted: 26538 ...

  6. POJ-2352 Stars 树状数组

    Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39186 Accepted: 17027 Description A ...

  7. Stars(树状数组或线段树)

    Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37323 Accepted: 16278 Description A ...

  8. poj 2229 Ultra-QuickSort(树状数组求逆序数)

    题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...

  9. Stars(树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=1541 Stars Time Limit: 2000/1000 MS (Java/Others)    Memor ...

随机推荐

  1. 基于jQuery日历插件制作日历

    这篇文章主要介绍了基于jQuery日历插件制作日历的相关资料,需要的朋友可以参考下 来看下最终效果图吧: 是长得丑了一点,不要吐槽我-.- 首先来说说这个日历主要的制作逻辑吧: ·一个月份最多有31天 ...

  2. RegExp对象的exec方法

    RegExp对象的exec方法和String对象的match方法用法十分相似,分两篇博客讲讲其各自的用法和它们之间的异同. 下一篇讨论match方法的用法和两者的异同. 定义及语法 [定义] exec ...

  3. <Android 基础(二十三)> Android Studio快捷键

    前言 Android Studio对于快捷键的设置比较的灵活,开发者在从不同的平台转移到Android Studio进行Android开发的时候,应该都能找到合适的KeyMap和快捷键使用方式,因为A ...

  4. Monitorix:一款面向Linux的轻型系统和网络监测工具

    Monitorix是一款功能非常强大的免费开源轻型工具,目的在于监测Linux中的系统和网络资源.它可以定期收集系统和网络数据,并使用自己的Web界面,通过图形显示相关信息.Monitorix让用户可 ...

  5. Python爬虫教程-03-使用 chardet 检测编码

    Spider-03-使用chardet 继续学习python爬虫,我们经常出现解码问题,因为所有的页面编码都不统一,我们使用chardet检测页面的编码,尽可能的减少编码问题的出现 网页编码问题解决 ...

  6. Python3利用Dlib19.7实现摄像头人脸识别的方法

    0.引言 利用python开发,借助Dlib库捕获摄像头中的人脸,提取人脸特征,通过计算欧氏距离来和预存的人脸特征进行对比,达到人脸识别的目的: 可以自动从摄像头中抠取人脸图片存储到本地,然后提取构建 ...

  7. [Swift] 创建一个对象

    创建一个对象 先写一个People类 // // People.swift // Class // // Created by YouXianMing on 15/3/18. // Copyright ...

  8. Python实例---游戏人生[类的学习]

    # -*- coding:utf-8 -*- # ##################### 定义实现功能的类 ##################### class Person: def __in ...

  9. windows系统镜像 微软官方资源便捷下载教程

    今天跟小师弟学到了一个下载软件的好办法,省得到各种网站下载带有病毒,插件的资源. 这个神奇的网站叫做   MSDN, 我告诉你,这是一个私人维护的网站,里面有各种官方软件的下载地址.可以直接用下载工具 ...

  10. 英语零散笔记Note整理

    无意之间整理电脑发现还存放着以前自己看视频做的一些笔记,关于新概念英语的笔记,觉得不错,放于博客,以供学习. English Note1 定语从句 将不重要的动作放在定语从句中,重要的放在主干中. 倒 ...