hdoj 1541 Stars【线段树单点更新+最大值维护】
Stars
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6581 Accepted Submission(s):
2625
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.
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.
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.
#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【线段树单点更新+最大值维护】的更多相关文章
- HDU 1754 I Hate It 线段树单点更新求最大值
题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...
- HDU 1166 敌兵布阵(线段树单点更新)
敌兵布阵 单点更新和区间更新还是有一些区别的,应该注意! [题目链接]敌兵布阵 [题目类型]线段树单点更新 &题意: 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N< ...
- poj 2892---Tunnel Warfare(线段树单点更新、区间合并)
题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...
- HDU 1166 敌兵布阵(线段树单点更新,板子题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)
Brainman Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10575 Accepted: 5489 Descrip ...
- HDU 1166 敌兵布阵(线段树单点更新,区间查询)
描述 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- HDUOJ----1166敌兵布阵(线段树单点更新)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)
HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...
随机推荐
- ezSQL 数据库操作类
http://justinvincent.com 官网,一切尽在里面的下载帮助文件中,妙哉也!! ez_sql_core.php <?php /************************* ...
- C#实现记事本查找功能
private void button1_Click(object sender, EventArgs e) { if (!(String.IsNullOrEmpty(this.textBox1.Te ...
- 两个由于php.ini配置错误导致的报错:ajax图片上传报错和exec报错
遇到了两个由于php.ini配置错误导致的报错:ajax图片上传报错和exec报错 首先第一个: 在做一个用ajax图片上传的功能中,php报了这样一个错误:File upload error - u ...
- 005 Python的数值类型
005 Python的数值类型 BIF 指的是内置函数,一般不作为变量命名.如 input,while,if,else,float,等等.整型:整数.(python3.0版本把整型和长整型结合在 ...
- 方便mac os 10.9系统中phpstorm配置php运行环境
自己安装php,不用mac安装,这样就有php开发环境了. 安装很简单,直接运行一个命令, 需要几分钟,请慢慢等待. curl -s http://php-osx.liip.ch/install.sh ...
- Mongoengine 使用笔记
1.直接将某个document对象导出对应的json数据. #models class Feed(Document): """ @summary: 所有订阅内容 &quo ...
- Intel项目Java小记
cannot be cast to javax.servlet.Filter添加provided即可 install -X是什么意思? Unsupported major.minor version ...
- 百度预测 及 maven pom搜索地址
http://trends.baidu.com/ http://mvnrepository.com/artifact/net.sourceforge.htmlcleaner/htmlcleaner/2 ...
- sqltext sqlarea
sqltext 中sql 有完整的sql, sqlarea没有
- 【网络流24题】No. 20 深海机器人问题 (费用流)
[题意] 深海资源考察探险队的潜艇将到达深海的海底进行科学考察.潜艇内有多个深海机器人. 潜艇到达深海海底后, 深海机器人将离开潜艇向预定目标移动. 深海机器人在移动中还必须沿途采集海底生物标本. 沿 ...