Stars

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 37323 Accepted: 16278

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

Ural Collegiate Programming Contest 1999

线段树和树状数组皆可AC

树状数组(和金巨学的)

#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <queue>
#include <vector>
#include <algorithm>
#define LL long long
using namespace std; const int INF = 0x3f3f3f3f; const int MAX = 32010; int c[MAX]; int level[MAX/2]; int lowbit(int x)
{
return x&(-x);
} int sum(int x)
{
int sum=0;
while(x>0)
{
sum+=c[x];
x-=lowbit(x);
}
return sum;
} void add(int x)
{
while(x<MAX)
{
c[x]++;
x+=lowbit(x);
}
} int main()
{
int n;
int x,y;
memset(level,0,sizeof(level));
memset(c,0,sizeof(c));
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d %d",&x,&y);
++x;
level[sum(x)]++;
add(x);
}
for(int i=0;i<n;i++)
{
printf("%d\n",level[i]);
}
return 0;
}

线段树

#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <queue>
#include <vector>
#include <algorithm>
#define LL long long
using namespace std; const int INF = 0x3f3f3f3f; const LL MAX = 32000; int a[MAX*4];
int level[MAX]; int Query(int L,int R,int l,int r,int site)
{
if(L==l&&R==r)
{
return a[site];
}
int mid=(L+R)>>1;
if(r<=mid)
{
return Query(L,mid,l,r,site<<1);
}
else if(l>mid)
{
return Query(mid+1,R,l,r,site<<1|1);
}
else
{
return Query(L,mid,l,mid,site<<1)+Query(mid+1,R,mid+1,r,site<<1|1);
}
}
void update(int L,int R,int s,int site)
{
a[site]++;
if(L==R)
{
return ;
}
int mid=(L+R)>>1;
if(s<=mid)
{
update(L,mid,s,site<<1);
}
else
{
update(mid+1,R,s,site<<1|1);
}
}
int main()
{
int n;
int x,y;
memset(level,0,sizeof(level));
memset(a,0,sizeof(a));
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d %d",&x,&y);
level[Query(0,MAX,0,x,1)]++;
update(0,MAX,x,1);
}
for(int i=0;i<n;i++)
{
printf("%d\n",level[i]);
}
return 0;
}

Stars(树状数组或线段树)的更多相关文章

  1. bzoj 3110: [Zjoi2013]K大数查询 树状数组套线段树

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1384  Solved: 629[Submit][Stat ...

  2. [BZOJ 3196] 213平衡树 【线段树套set + 树状数组套线段树】

    题目链接:BZOJ - 3196 题目分析 区间Kth和区间Rank用树状数组套线段树实现,区间前驱后继用线段树套set实现. 为了节省空间,需要离线,先离散化,这样需要的数组大小可以小一些,可以卡过 ...

  3. [BZOJ 1901] Dynamic Rankings 【树状数组套线段树 || 线段树套线段树】

    题目链接:BZOJ - 1901 题目分析 树状数组套线段树或线段树套线段树都可以解决这道题. 第一层是区间,第二层是权值. 空间复杂度和时间复杂度均为 O(n log^2 n). 线段树比树状数组麻 ...

  4. POJ 1195 Mobile phones (二维树状数组或线段树)

    偶然发现这题还没A掉............速速解决了............. 树状数组和线段树比较下,线段树是在是太冗余了,以后能用树状数组还是尽量用......... #include < ...

  5. 【BZOJ3196】二逼平衡树(树状数组,线段树)

    [BZOJ3196]二逼平衡树(树状数组,线段树) 题面 BZOJ题面 题解 如果不存在区间修改操作: 搞一个权值线段树 区间第K大--->直接在线段树上二分 某个数第几大--->查询一下 ...

  6. BZOJ.4553.[HEOI2016&TJOI2016]序列(DP 树状数组套线段树/二维线段树(MLE) 动态开点)

    题目链接:BZOJ 洛谷 \(O(n^2)\)DP很好写,对于当前的i从之前满足条件的j中选一个最大值,\(dp[i]=d[j]+1\) for(int j=1; j<i; ++j) if(a[ ...

  7. P3157 [CQOI2011]动态逆序对(树状数组套线段树)

    P3157 [CQOI2011]动态逆序对 树状数组套线段树 静态逆序对咋做?树状数组(别管归并QWQ) 然鹅动态的咋做? 我们考虑每次删除一个元素. 减去的就是与这个元素有关的逆序对数,介个可以预处 ...

  8. HDU 5618 Jam's problem again(三维偏序,CDQ分治,树状数组,线段树)

    Jam's problem again Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  9. BZOJ 1901 Zju2112 Dynamic Rankings 树状数组套线段树

    题意概述:带修改求区间第k大. 分析: 我们知道不带修改的时候直接上主席树就可以了对吧?两个版本号里面的节点一起走在线段树上二分,复杂度是O((N+M)logN). 然而这里可以修改,主席树显然是凉了 ...

  10. HDU 5877 2016大连网络赛 Weak Pair(树状数组,线段树,动态开点,启发式合并,可持久化线段树)

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Tota ...

随机推荐

  1. Java基础之访问文件与目录——列出目录内容(ListDirectoryContents)

    控制台程序,列出目录的全部内容并使用过滤器来选择特定的条目. import java.nio.file.*; import java.io.IOException; public class List ...

  2. ads 错误

    这个问题已经不是第一次碰到了,每次弄周立功的EasyARM2210的时候都会遇见,每次都没有记住.就是要用ADS运行板子配套光盘里面的配套程序的时候会出现: (Fatal)L6002U:Could n ...

  3. ofbiz进击 个人遇到的奇葩问题汇总。

    在本人做退货单生成的时候,因为考虑到要控制通过java类方法去调用 service服务可以方便给出提示消息,所以专门新建了一个java类,然后去重新请求request请求,下面为Java类的代码 pu ...

  4. javascript 一些常用的验证

    只能输入数字          onkeyup="this.value=this.value.replace(/[^\d]/g,'')" onafterpaste="th ...

  5. StringBuffer类总结

    package day13; /* StringBuffer是字符串缓冲区. 是一个容器. 特点: 1,长度是可变化的. 2,可以字节操作多个数据类型. 3,最终会通过toString方法变成字符串. ...

  6. firefox中 checkbox属性checked="checked"已有,但复选框却不显示打钩的原因

    最近在调试复选框的应用,在ie没有问题,考虑到兼容性,试试了firefox,遇到了问题. 复选框绑定了click事件,点一次选中,再点击取消选中,依次类推.这个功能在ie中没问题,但是在firefox ...

  7. MSSQL 判断实例中是否存在某种表

    执行语句 SELECT 'SELECT * FROM '+Name+'..SysObjects Where XType=''U'' and name=''tab_scartrim'' ORDER BY ...

  8. springmvc转发与重定向

    摘自http://elf8848.iteye.com/blog/875830 (1)我在后台一个controller跳转到另一个controller,为什么有这种需求呢,是这样的.我有一个列表页面,然 ...

  9. 由linux下的多进程编程引发的关于进程间隔离的思考

    源代码放到了三个文件中: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include & ...

  10. 《zw版·Halcon-delphi系列原创教程》 邮票艺术品自动分类脚本

    <zw版·Halcon-delphi系列原创教程> 邮票艺术品自动分类脚本 邮票艺术品自动分类脚本,是个综合应用,有不同尺寸图像的自动识别.区域分割 还有作品附近文字的自动分割 此类项目, ...