首页 > 其他分享 >POJ 2352 HDU1541 Stars(树状数组)

POJ 2352 HDU1541 Stars(树状数组)

时间:2023-06-12 14:35:20浏览次数:48  
标签:HDU1541 star Stars level int amount POJ stars include


题意:二维平面给定n个点的坐标,然后要你输出每个点的“等级“。每个点的等级是它的左下放的点个数(包括正下放和正左方的点)。即要你输出对于每个点(x,y)来说,有多少点的坐标(xi, yi)满足xi<=x且yi<=y。

思路:题目给出的坐标中已经是按y升序排列,那么其实只用考虑x轴,那么显然就是在前面的点中找比自己小的。

Trick:注意的是累加的时候while循环的条件要改一下,因为我们这里是用x轴坐标的值作为c数组的下标,而x最大到32000,而不是n。



#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 32000+10
#define LL long long
int cas=1,T;
int c[maxn];
int L[maxn];
int n;
int lowbit(int i)
{
	return i&(-i);
}
int sum(int i)
{
	int ans = 0;
	while (i)
	{
		ans +=c[i];
		i-=lowbit(i);
	}
	return ans;
}
void add(int i,int d)
{
	while (i<=maxn)
	{
		c[i]+=d;
		i+=lowbit(i);
	}
}

int main()
{
	//freopen("in","r",stdin);
	while (scanf("%d",&n)!=EOF && n)
	{
		memset(c,0,sizeof(c));
		memset(L,0,sizeof(L));
		for (int i = 1;i<=n;i++)
		{
			int x,y;
			scanf("%d%d",&x,&y);
			x++;
			L[sum(x)]++;
			add(x,1);
		}
		for (int i = 0;i<n;i++)
			printf("%d\n",L[i]);
	}
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}




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


 





标签:HDU1541,star,Stars,level,int,amount,POJ,stars,include
From: https://blog.51cto.com/u_16156555/6462543

相关文章

  • POJ 3498 March of the Penguins(枚举+最大流)
    题意:在X,Y坐标系中有N(N<=100)个冰块...有些冰块上有若干只企鹅..每只企鹅一次最多跳M距离..一个冰块在有Mi个企鹅离开..就会消失..问有哪些冰块可以作为集合点..就是所有企鹅都能成功到这个冰块上来.思路:枚举每一块冰块,看看最大流能否等于企鹅总数即可      建图:把每块......
  • POJ 2019 Cornfields(简单二维RMQ)
    思路:二维RMQ#include<cstdio>#include<cstring>#include<algorithm>usingnamespacestd;constintMAXN=255;intval[MAXN][MAXN];intdmin[MAXN][MAXN][8][8];intdmax[MAXN][MAXN][8][8];voidinitRMQ(intn,intm){for(inti=1;i<=n;i++)......
  • pojo层
    Answerpackagecom.example.academicadministration.pojo;importlombok.AllArgsConstructor;importlombok.Data;importlombok.NoArgsConstructor;importjava.sql.Timestamp;@Data@AllArgsConstructor@NoArgsConstructorpublicclassAnswer{privateStr......
  • Java开发手册中为什么禁止使用isSuccess作为布尔类型变量名以及POJO中基本类型与包装
    场景Java开发手册中关于POJO的布尔类型的变量名的要求是:【强制】POJO类中的任何布尔类型的变量,都不要加is前缀,否则部分框架解析会引起序列化错误。说明:在本文MySQL规约中的建表约定第一条,表达是与否的变量采用is_xxx的命名方式,所以,需要在<resultMap>设置从is_xxx到......
  • POJO简介【pojo模块】
    DTO(DataTransferObject):数据传输对象,用于接收数据和传输数据,属性和请求参数对应。VO(ViewObject):视图对象,返回给客户端展示用的数据,例如分页对象PageResult{total,List}。PO(PersistantObject):持久化对象,对象属性和数据库表中的字段一一对应,一张表对应一个PO。POJO(PlainOrdi......
  • POJ3608(旋转卡壳--求两凸包的最近点对距离)
    题目:BridgeAcrossIslands  考虑如下的算法,算法的输入是两个分别有m和n个顺时针给定顶点的凸多边形P和Q。1.计算P上y坐标值最小的顶点(称为yminP)和Q上y坐标值最大的顶点(称为ymaxQ)。2.为多边形在yminP和ymaxQ处构造两条切线LP和LQ使得他们对应的多边形位于他们的右侧......
  • POJ1151(线段树+扫描线求矩形面积并)
    题目:http://poj.org/problem?id=1151 #include<iostream>#include<string.h>#include<algorithm>#include<stdio.h>usingnamespacestd;constintN=10005;structnode{intl,r;intcov;doublelen;};structline{......
  • POJ2369 置换群
    题目:http://poj.org/problem?id=2369题意:给定一个序列,问需要最少需要置换多少次才能变为有序序列.分析:对于每一位,算出最少的置换到自己应该的数字。每一位都有这样的数字,取最小公倍数就可以。#include<iostream>#include<string.h>#include<stdio.h>usingnamespacestd......
  • POJ3695(矩形切割中等题)
    题目:Rectangles 题意:给N个矩形,他们可能会重叠,然后给M个询问,每个询问给出指定的矩形位置,然后分别计算每个询问中选中的矩形的并。 本题跟求所有矩形的并一个思路,只是再增加一个数组来保存选中矩形的位置,然后直接求并即可。#include<stdio.h>#include<string.h>#include<algor......
  • POJ1151(矩形切割入门题)
    题目:Atlantis 我的上一篇文章已经讲明了线段切割的思想,矩形切割就是把线段切割从一维推到二维就行了,思想都一样。#include<stdio.h>#include<string.h>constintN=205;typedefstruct{doublex1,y1;doublex2,y2;doublesum;}Node;NodeT[N];intn......