POJ2352 Stars 【树状数组】or【线段树】

Stars

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 31172   Accepted: 13595

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.

树状数组节点保存区间和,总区间为[0, maxn)。

树状数组:

//#define DEBUG
#include <stdio.h>
#define maxn 32002

int tree[maxn], level[maxn], n;

int lowBit(int x)
{
	return x & (-x);
}

void update(int pos)
{
	while(pos < maxn){
		++tree[pos];
		pos += lowBit(pos);
	}
}

int getSum(int pos)
{
	int sum = 0;
	while(pos > 0){
		sum += tree[pos];
		pos -= lowBit(pos);
	}
	return sum;
}

int main()
{
	#ifdef DEBUG
		freopen("stdin.txt", "r", stdin);
		freopen("stdout.txt", "w", stdout);
	#endif

	int x, y, i;
	scanf("%d", &n);

	for(i = 0; i < n; ++i){
		scanf("%d%d", &x, &y);
		++level[getSum(++x)];
		update(x);
	}

	for(i = 0; i < n; ++i)
		printf("%d\n", level[i]);

	return 0;
}

线段树:

#include <stdio.h>
#define maxn 32002
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1

int tree[maxn << 2], level[maxn];

void pushUp(int rt)
{
	tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
}

void update(int pos, int l, int r, int rt)
{
	if(l == r){
		++tree[rt]; return;
	}

	int mid = (l + r) >> 1;
	if(pos <= mid) update(pos, lson);
	else update(pos, rson);

	pushUp(rt);
}

int query(int left, int right, int l, int r, int rt)
{
	if(l == left && r == right) return tree[rt];

	int mid = (l + r) >> 1;
	if(right <= mid) return query(left, right, lson);
	else if(left > mid) return query(left, right, rson);
	else return query(left, mid, lson) + query(mid + 1, right, rson);
}

int main()
{
	int n, x, y, i;
	scanf("%d", &n);

	for(i = 0; i < n; ++i){
		scanf("%d%d", &x, &y);
		++level[query(0, x, 0, maxn - 1, 1)];
		update(x, 0, maxn - 1, 1);
	}

	for(i = 0; i < n; ++i)
		printf("%d\n", level[i]);

	return 0;
}

POJ2352 Stars 【树状数组】or【线段树】

时间: 2025-01-09 07:56:29

POJ2352 Stars 【树状数组】or【线段树】的相关文章

浅谈二维中的树状数组与线段树

一般来说,树状数组可以实现的东西线段树均可胜任,实际应用中也是如此.但是在二维中,线段树的操作变得太过复杂,更新子矩阵时第一维的lazy标记更是麻烦到不行. 但是树状数组在某些询问中又无法胜任,如最值等不符合区间减法的询问.此时就需要根据线段树与树状数组的优缺点来选择了. 做一下基本操作的对比,如下图. 因为线段树为自上向下更新,从而可以使用lazy标记使得矩阵的更新变的高校起来,几个不足就是代码长,代码长和代码长. 对于将将矩阵内元素变为某个值,因为树状数组自下向上更新,且要满足区间加法等限制

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

3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1384  Solved: 629[Submit][Status] Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少. Input 第一行N,M接下来M行,每行形如1 a b c或2 a b

树状数组与线段树

一:树状数组 树状数组是对一个数组改变某个元素和求和比较实用的数据结构.两中操作都是O(logn). 需求:有时候我们需要频繁地求数组的前k项和或者求数组从小标i到j的和,这样每次最坏情况下的时间复杂度就会为O(N),这样效率太低了.而树状数组主要就是为了解决这样一个问题.树状数组在求和及修改都可以在O(lgN)时间内完成. 树状数组需要额外维护一个数组,我们设为C[N],原数组为A[N], 其中每个元素C[i]表示A[i-2^k+1]到A[i]的和,这里k是i在二进制时末尾0的个数.注意通过位

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

题目链接:BZOJ - 3196 题目分析 区间Kth和区间Rank用树状数组套线段树实现,区间前驱后继用线段树套set实现. 为了节省空间,需要离线,先离散化,这样需要的数组大小可以小一些,可以卡过128MB = = 嗯就是这样,代码长度= =我写了260行......Debug了n小时= = 代码 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #in

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

题目链接:BZOJ - 1901 题目分析 树状数组套线段树或线段树套线段树都可以解决这道题. 第一层是区间,第二层是权值. 空间复杂度和时间复杂度均为 O(n log n). 代码 树状数组套线段树 #include <iostream> #include <cstdlib> #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> usin

HDU 1566 Color the ball(树状数组or线段树)

Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11387    Accepted Submission(s): 5680 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"

BZOJ 3295 [Cqoi2011]动态逆序对 树状数组套线段树

题意:链接 方法:树状数组套线段树 解析: 这题基本上写的都是什么CDQ点分治,主席树之类的,然而这我都并不会,所以写了一发平衡树套线段树想卡时卡过去,然而我并没有得逞,T的不要不要的,这里用平衡树套线段树的方法参见我的题解:排队.这道题比那道更要简单. 然后我就打算弃坑了~不过看140142做这道题做的热火朝天的,还是打算回来做一下,yy下树状数组套线段树,然后去看hz的题解,只看懂他写理论部分了,代码部分不知所云,所以还是还是得yy.引用理论部分. 删除某个数,只要统计它之前还存在的比它大的

HDU 1166 敌兵布阵 (我的树状数组加线段树点修改模板)

思路:本题因为是点修改,所以我们可以用线段树或者是树状数组了.线段树的基本操作我在我的代码中会具体体现,关键是要理解下面这幅图,具体的思想大家可以去看看其他的资料 线段树AC代码: #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; #define N 50005 int num

【POJ】1990-MooFest(树状数组or线段树)

树状数组和线段树的解法比较,感觉不论在内存还是时间上都是树状数组占优 大题思路对 v从小到大进行排序,之后找之前的(也就是比v小的坐标) v * sum(abs(xi - x)) 这样的话 abs无法处理,我们用另外一个树状数组记录在x ~ y区间牛的个数,之前那个记录在x ~ y区间内牛的坐标和 Accepted 484 79 C++ 1131   树状数组代码: #include<cstdio> #include<algorithm> #include<cstring&g

求左下角星星之和 树状数组或线段树 poj 2352 Stars

http://poj.org/problem?id=2352 Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37696   Accepted: 16419 Description Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coo