POJ2352 Stars(线段树 & 树状数组)

POJ-2352(Stars)树状数组解法 题目原网址:http://poj.org/problem?id=2352又是一道星星的题,下面是题目描述(本人翻译):题目描述天文学家经常检查星图,其中恒星由平面上的点表示,每颗恒星都有笛卡尔坐标。让恒星的水平是恒星的数量,而恒星并不高于恒星的右边。天文学家想知道恒星的分布。例如,查看上图中显示的地图。星号5的水平等于3(它由三颗星形成,数字为1,2和4)。而且,编号为2和4的恒星的等级为1.在该... 阅读详情

Stars
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 37709 Accepted: 16423

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个星星的坐标,星星的级数等于左下角有几个星星,输出n个星星的级数。


简单的线段树,把坐标转化,求和存到level数组即可。


AC代码(线段树):


#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int maxn = 32005;
#define mid ((left + right) >> 1)
#define lson rt << 1, left, mid
#define rson rt << 1 | 1, mid + 1, right
int sum[maxn << 2], level[maxn << 2];
void update(int rt, int left, int right, int data)
{
	++sum[rt];
	if(left == right) return ;
	if(data <= mid) update(lson, data);
	else update(rson, data);
}
int query(int rt, int left, int right, int l, int r)
{
	if(l == left && right == r) return sum[rt];
	int m = mid;
	if(r <= m) return query(lson, l, r);
	else if(l > m) return query(rson, l, r);
	else return query(lson, l, m) + query(rson, m + 1, r);
}
int main(int argc, char const *argv[])
{
	/* code */
	int n, x, y;
	while(scanf("%d", &n) != EOF) {
		memset(sum, 0, sizeof(sum));
		memset(level, 0, sizeof(level));
		for(int i = 0; i < n; ++i) {
			scanf("%d%d", &x, &y);
			++x;
			++level[query(1, 1, maxn, 1, x)];
			update(1, 1, maxn, x);
		}
		for(int i = 0; i < n; ++i)
			printf("%d\n", level[i]);
	}
	return 0;
}


树状数组思路一样的~


AC代码(树状数组):


#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int maxn = 32005;
int c[maxn], level[maxn], n;
int lowbit(int x)
{
	return x & (-x);
}
int sum(int n)
{
	int sum = 0;
	while(n > 0) {
		sum += c[n];
		n -= lowbit(n);
	}
	return sum;
}
void add(int x)
{
	while(x <= maxn) {
		++c[x];
		x += lowbit(x);
	}
}
int main(int argc, char const *argv[])
{
	int n, x, y;
	while(scanf("%d", &n) != EOF) {
		memset(level, 0, sizeof(level));
		memset(c, 0, sizeof(c));
		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;
}


arduino uno spi驱动12864 arduino uno spi驱动12864 阅读详情

相关推荐

Spatio-Temporal Graph Convolutional Networks: A Deep Learning Framework for Traffic Forecasting 论文笔记

Spatio-Temporal Graph Convolutional Networks: A Deep Learning Framework for Traffic Forecasting 论文笔记-----GCN在交通领域的应用(二) 一、论文翻译: 1、摘要: 及时准确地交通预测对于城市交通控制和指导具有至关重要的意义。由于交通流量的非线性和复杂性,传统的方法不能满足中长期预测任务的需求,并且往往会忽略时空数据的时间和空间依赖特性。在这篇论文中,为了处理在交通领域的时间序列预测问题...

cy_believ的博客 1916

poj2352--Stars--线段树&树状数组

Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26892   Accepted: 11746 Description Astronomers often examine star maps where stars are represented by

ChenLI's blog 847

Bybit面经

这段总结描述了求职者在Bybit面试过程中的经历和相关问题,包括面试内容、求职者向面试官提问的问题以及关于Bybit的信息

郑泽洲的博客 2580

【ICPC-407】poj 2352 Stars

点击打开链接poj2352 思路:树状数组 分析: 1 题目是要求出每一个点的左下(正左+正下)有几个星星,那个这个点就是第几层,最后输出0~n-1层的点的个数。比如样列编号为5的星星,左下有3个星星那么5就处于第三层 2 利用树状数组,我们知道树状数组C中,C[i]表示的是原先数组A中的某一段和。题目明确指出输入的时候是按照y值增大的顺序(y相同是x增大),那么我们应该要用什么做为原先的数组A...

藏经阁 | 玄苦 4103

Matlab_Marr小波尺度变换图

多尺度的连续小波变换分解后时间尺度图 墨西哥Marr小波函数: %delta 小波变换尺度 %N     小波函数的长度 %s     原始信号 %g     原始信号某个尺度下的小波变化系数 function g=Singularity_Detection(delta,N,s); %保持信号长度 n=length(s); % 构造墨西哥小帽小波函数 for index_x=1

桔色小西瓜 6601

保姆级教程:用Code Blocks搞定中科蓝讯AB5768E蓝牙音响SDK开发环境(附资源包)

本文提供了一份详细的Code Blocks配置指南,帮助开发者快速搭建中科蓝讯AB5768E蓝牙音响SDK开发环境。从工具链安装、环境配置到工程导入和调试技巧,涵盖全流程避坑要点,特别适合K12开发板初学者。附资源包下载,助您高效完成蓝牙音响开发。

weixin_28692817的博客 448

poj 2352 Stars(树状数组基础)

http://poj.org/problem?id=2352 题意:有若干个星星,给出每个星星在二维平面上的坐标(输入按y递增顺序给出,y相同时按x递增顺序输出)。定义每个星星的级别为 横纵坐标均不超过自己的星星的个数(不包括自身)。问级别为0~n-1的星星分别有几个。 思路:首先将二维转化为一维。。因为输入是有顺序的,当前星星与后面的星星并没有关系。对于第i次输入的星星(横纵坐标为x,y),

1032

(Touch™)搭建 Windows 开发环境

> OpenHaptics 开发包参考文档。

置心一处,便无事不办 1500

poj 2352 Stars

题目链接:http://poj.org/problem?id=2352 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 s

Vision 1万+

【SABRE 3D, SABRE 3DxT,LAM设备】2025年9月12日-生产期间维护用的双重隔离

生产期间维护用的双重隔离。该程序用于准备将一个或多个双工位设置为维护状态,同时SABRE 3D/3DxT/3D Scimitar保持生产运行。其目的是在不中断生产的情况下将双工位与运行中的设备隔离。

weixin_46735260的博客 574

AI穿透式智慧粮仓

本报告基于 2025-2026 年全国多地智慧粮储实践案例,系统分析了 "AI + 穿透式" 智慧粮储体系的技术架构、AI 算法应用、提质增效成效、区域差异化特点及未来发展趋势。该体系通过多模态感知、智能算法与管控平台的深度融合,实现粮食存储全流程精准感知、智能预警与科学调控,显著降低储粮损耗与运营成本,大幅提升储粮安全水平与管理效率。报告梳理了各地差异化实践路径,提出针对性发展建议,为推动我国粮食仓储高质量发展、筑牢国家粮食安全防线提供参考。

reset2021的博客 229

【树状数组】POJ 2352 Stars

POJ 2352 Stars 题意:在笛卡尔坐标系上给出星星坐标,按照y坐标升序(y坐标相等,x坐标升序)给出。规定星星的等级为其左下方的星星数量,求各等级的星星的个数。 思路:由于星星是按照y坐标升序(y坐标相等,x坐标升序)给出的,所以我们找该星星的左下的星星数量,就是当前输入该星星坐标之后getsum(x)-1,“-1”是因为要减去该星星本身。 注意:为了防止add( 0 ),所以我们每个...

L_X_ 167

【经验&技巧】IBIS AMI模型眼图仿真问题探讨

在ADS的仿真环境中,不论发送和接收的AMI模型是否为true或者false,都可以将通道仿真设置为统计模式,但是,如果有一方的Init函数为false,在统计模式下,处于false状态的模型将不能执行其自带的后处理功能,如下图所示,左边的统计模式和右边的逐比特模式眼图存在明显的差异,原因在于,接收端的init函数为false导致在统计模式中不能对眼图进行DFE处理,换言之,此时统计模式下的眼图就是未经任何处理的“生图”。

一只豌豆象的博客 1766

POJ2352——树状数组——Stars

http://poj.org/problem?id=2352 模板 /************************************************ * Author :Powatr * Created Time :2015-8-13 16:18:10 * File Name :POJ2352stars.cpp ********...

dgjm4087的博客 193

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

POJ2352 HDU1541 Stars【线段树/树状数组】

http://poj.org/problem?id=2352 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 a...

zhaohaibo的博客 441

POJ2352 Stars & POJ2481 Cows 树状数组的应用

POJ2352 题目链接 1.题目大意是输入一堆坐标,然后y坐标是排好序的,定义了一个“高度”,就是某个星星左下角(包括正左和正下)的星星个数,然后按“高度”有小到大输出每个“高度”的星星个数 2.首先看到这题目觉得很玄乎,似乎看起来挺简单但是无可下手,写了个暴力,但转身一想肯定超时。但是自己似乎掌握的技能里边没有能直接应用到这道题的。去网上看发现都是树状数组的解法,线段树也很少。搞懂树状数组后,...

Happig的博客 291

POJ 2352_Stars && POJ-2481 Cows (线段树单点更新+树状数组)

C - Cows Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice POJ 2481 Description Farmer John's cows have discovered that the clover g

K键盘里的青春K 728

POJ - 2352 Stars(线段树/树状数组)

题目链接:点击查看 题目大意:给出n个星星的坐标,规定每个星星的等级为在它左下方的星星的个数,输出0~n-1每个等级共有多少个星星 题目分析:裸的线段树,因为x坐标和y坐标都已经给排好序了,是按照y升序,当y相等时按照x升序排序,那么当输入到任意一个星星的时候,我们看图易知,之前录入过的坐标中x小于等于当前坐标的都在当前坐标的左下角,所以我们可以按x坐标进行统计,有一个地方需要注意的就是,因为...

Falcon的博客 424

POJ 2352 Stars【树状数组】

题目 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

QYQHxyh的博客 616

POJ 2352 Stars 树状数组

Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40175   Accepted: 17446 Description Astronomers often examine star maps where stars are represented by

Houheshuai的专栏 544

poj 2352 Stars 树状数组or线段树

题目大意: 在坐标上有n个星星,如果某个星星坐标为(x, y), 它的左下位置为:(x0,y0),x0&lt;=x 且y0&lt;=y。如果左下位置有a个星星,就表示这个星星属于level x 按照y递增,如果y相同则x递增的顺序给出n个星星,求出所有level水平的数量。 分析: 因为输入是按照按照y递增,如果y相同则x递增的顺序给出的, 所以,对于第i颗星...

114

poj 2352 Stars(树状数组 or 线段树)

链接: http://poj.org/problem?id=2352 题目大意: 在坐标上有n个星星,如果某个星星坐标为(x, y), 它的左下位置为:(x0,y0),x0 按照y递增,如果y相同则x递增的顺序给出n个星星,求出所有level水平的数量。 分析与总结: 因为输入是按照按照y递增,如果y相同则x递增的顺序给出的, 所以,对于第i颗星星,它的level就

D_Double's Journey 8514

POJ-2352-Stars题解(树状数组 or 线段树)

Stars 传送门:Stars 题意 将星空看做一个平面,在平面上建立直角坐标系,这样在这个平面上的每个星星都有一个坐标。天文学家定义了一个星星的水平:一个星星的水平就是,在这个星星正左边的星星、正下方的星星以及左下方的星星数目之和,就是这个星星的水平。 输入:N代表要输入的星星的个数。接下来的 N 行,每行两个整数 x 和 y ,代表这个星星的横纵坐标。这里在输入时有一个规则,就是按照 y ...

还没有简介~ 391

poj 2352 Stars 线段树(先建后查/边建边查)/树状数组三种方法思路详解,带你深入了解线段树难度⭐⭐⭐★

poj 2352 Stars 目录poj 2352 Stars1.树状数组2.线段树,先建树后查找3.线段树,边建树边查找 Description Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. ...

繁凡さん的博客 636

POJ 2352 Stars(树状数组||线段树单点更新)

Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41427   Accepted: 18055 Description Astronomers often examine star maps where stars are represented by po

学 以致用 366

poj 2352-Stars

链接:http://poj.org/problem?id=2352 因为题目给出的数据y是非递减的,所以y可以直接忽略掉,题目不是很难,比较适合练手,给出线段树和树状数组做的代码 线段树: #include #include #define MAXN 32010 int sum[15010]; struct node { int l,r; int s; }t[4*

yjCola 903

POJ 2352 Stars 线段树

Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40218   Accepted: 17470 Description Astronomers often examine star maps where stars are represented by

Houheshuai的专栏 547
上一篇: HDOJ5122 K.Bro Sorting(树状数组 & 模拟)
下一篇: HDOJ1272 小希的迷宫(并查集)
GKHack
博客等级 码龄11年 48粉丝 291原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值