Stars

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6581    Accepted Submission(s):
2625

Problem 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
 
题意:一个星星的地图,每个星星都有对应的等级,星星等级的计算符合以下规律:一颗星星的  左下方(包括正左方和正下方)的星星个数就是这个星星的等级
题解:先对星星的纵坐标从小到大排列,如果纵坐标相同将横坐标从小到大排列,在进行数据输入时进行对此操作的处理,
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAX 32010
using namespace std;
int sum[MAX<<2];
int pos[MAX];//记录各等级星星的个数
void pushup(int o)
{
sum[o]=sum[o<<1]+sum[o<<1|1];
}
void gettree(int o,int l,int r)
{
sum[o]=0;
if(l==r)
return ;
int mid=(l+r)>>1;
gettree(o<<1,l,mid);
gettree(o<<1|1,mid+1,r);
pushup(o);
}
void update(int o,int l,int r,int L)
{
if(l==r)
{
sum[o]+=1;
return ;
}
int mid=(l+r)>>1;
if(L<=mid)//因为建树的过程是按照星星的纵坐标从小到大建立的所以此处
//只需要考虑横坐标的情况,当横坐标小于mid对左子树进行操作
//否则对右子树进行操作。
update(o<<1,l,mid,L);
else
update(o<<1|1,mid+1,r,L);
pushup(o);
}
int find(int o,int l,int r,int L,int R)
{
if(L<=l&&R>=r)
return sum[o];
int ans=0;
int mid=(l+r)>>1;
if(L<=mid)
ans+=find(o<<1,l,mid,L,R);
if(R>mid)
ans+=find(o<<1|1,mid+1,r,L,R);
return ans;
}
int main()
{
int n,m,j,i;
int x,y;
int level;
while(scanf("%d",&n)!=EOF)
{
memset(pos,0,sizeof(pos));
gettree(1,1,MAX);
for(i=0;i<n;i++)
{
scanf("%d%d",&x,&y);
level=find(1,1,MAX,1,x+1);
pos[level]++;
update(1,1,MAX,x+1);
}
for(i=0;i<n;i++)
printf("%d\n",pos[i]);
}
return 0;
}

  

hdoj 1541 Stars【线段树单点更新+最大值维护】的更多相关文章

  1. HDU 1754 I Hate It 线段树单点更新求最大值

    题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...

  2. HDU 1166 敌兵布阵(线段树单点更新)

    敌兵布阵 单点更新和区间更新还是有一些区别的,应该注意! [题目链接]敌兵布阵 [题目类型]线段树单点更新 &题意: 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N< ...

  3. poj 2892---Tunnel Warfare(线段树单点更新、区间合并)

    题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

  4. HDU 1166 敌兵布阵(线段树单点更新,板子题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  5. POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)

    Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10575   Accepted: 5489 Descrip ...

  6. HDU 1166 敌兵布阵(线段树单点更新,区间查询)

    描述 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况 ...

  7. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  8. HDUOJ----1166敌兵布阵(线段树单点更新)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  9. HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)

    HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...

随机推荐

  1. Win7 + VS2015 + CMake3.6.1-GUI编译库

    CMake生成Unicode版本VC工程 Just add this line in your top CMakeLists.txt file:     add_definitions(-DUNICO ...

  2. WPF中嵌入Flash(ActiveX)

    1. 建立 WPF Application. 首先,建立一个名为 FlashinWPF 的 WPF Application 2. 设置 Window 属性. 在 XAML 中修改 Window 的属性 ...

  3. std::copy的使用

    看到有人在用std::copy这个东西,很简洁和爽啊,,所以找些帖子学习学习 http://blog.sina.com.cn/s/blog_8655aeca0100t6qe.html https:// ...

  4. Spring和Hibernate相遇

    Spring是一个很贪婪的家伙,看到他的长长的jar包列表就知道了,其实对于hibernate的所有配置都是可以放在Spring中来进行得,但是我还是坚持各自分明,Spring只是负责自动探测声明类( ...

  5. java MYSQL做分页

    MySql中查询语句实现分页功能 语句: select * from 表名 where 条件 limit 要找第几页,每页多少行; import java.util.*; import java.sq ...

  6. JS简单仿QQ聊天工具的制作

    刚接触JS,对其充满了好奇,利用刚学到的一点知识,写了一个简单的仿QQ聊天的东西,其中还有很多的不足之处,有待慢慢提高. 功能:1.在输入框中输入内容,点击发送,即可在上方显示所输入内容. 2.点击‘ ...

  7. 四校训练 warm up 14

    A:Pythagoras's Revenge 代码: #include<cstdio> #define ll long long using namespace std; int main ...

  8. phpstorm 强大的活动模板 可以自定义注释,代码段,根据cms订制自动提示

    http://jingyan.baidu.com/article/8275fc86badd6346a03cf6aa.html [PHP] phpstorm的使用(1) http://v.youku.c ...

  9. WordPress 全方位优化指南(下)

    上一篇 WordPress 全方位性能优化指南(上)主要从网站性能指标.优化缓存.MySQL 等方面给大家介绍了如何进行 WordPress 性能优化,但还远远不够,毕竟像 WordPress 这样的 ...

  10. PHP 切割字符串 点号 不用双斜杠

    $name = "tupian.png"; $nameArr = explode(".", $name); 习惯了Java的程序员容易写成 $nameArr = ...