POJ 2352 Stars 线段树

Language:Default
Stars
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 24726 Accepted: 10782

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.

Source


 

先贴一个题面,题目意思很简单,给你N个星星的坐标,让你求出每颗星星左下角(包含它的行和列但不包含它本身)有多少颗星星...有几颗被包含了第几层就+1,最后输出从0~n-1层的值

题面中有一个要注意的地方就是X,Y是严格按照先Y升序后X升序的顺序的,所以不需要另外离线排序,可以在线进行线段树的维护

这题很有意思的就是Y坐标给你一点用都没有,因为Y是升序的,所以后出现的点一定有可能包含之前出现的点...不存在之前出现的点能包含之后出现的点的情况

所以按照样例我们得到了一个数列 1 5 7 3 5  这样我们的问题就化简成了一个求a[n]之前有多少比它小的点的问题 

- - 不过我开始还too young too simple以为是个DP什么的...结果也没想出来...也许可以吧但我也没多想...

不过正解肯定是线段树了

构建线段树之后,对于每一个点的插入,该点与该点之后的区间值就应该+1

这样我们在查找某个值的时候,只要从树顶遍历到树根,把所覆盖到的线段加起来就行了...所以使用update(x,32000,1,1); 更新该点之后的线段



举个样例吧...上图是我画图画的很渣别喷

其实就是个区间覆盖了

先插入1 ,1没被覆盖就是第0层...

再插入5, 5被1的线段覆盖了,5就是第1层...

再插入7, 7被1 和 5 两条线段覆盖了, 7 就是第2层..

再插入3, 3被1的线段覆盖了,3也是第1层...

再插入5, 5被 1.5.3 三条线段覆盖了, 5 就是第3层..

OK以上就是样例解释...总之,点被k条线段覆盖,就是k层


线段覆盖问题已经是线段树的标准应用了...我就不多说了...直接贴代码(参考scturtle大神)...就三个操作 建树,更新,查询

对于不会建树不会更新的可以去google线段树  基本就是一个父节点和两个左右子节点的 数据结构,本身不存在什么难度, 现在看看就是属于简单的数据结构了

 

/*Template*/
/* POJ 2352*/
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
using namespace std;

const double EPS = 1e-8;
const int MAXN = 10005;
const int INF = 1<<30;
const int MOD = 99991;

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
//typedef long long LL;
typedef __int64 LL;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
#define L(t) (t << 1)  //左子树
#define R(t) (t << 1 | 1) //右子树
#define Mid(a,b) ((a+b)>>1) //(a+b)/2 
struct node 
{
    int l,r,val;
} tree[32005*4];

void create(int l,int r,int root)//建树
{
    tree[root].l = l;
    tree[root].r = r;
    tree[root].val = 0;
    if(l == r) return;
    int m = Mid(l,r);
    create(l,m,L(root));
    create(m+1,r,R(root));
}
 
void update(int l,int r,int root,int val) //更新线段的value
{
    if(tree[root].l == l && tree[root].r == r)
    {
        tree[root].val+=val;
        return;
    }
    int m=Mid(tree[root].l,tree[root].r);
    if(r <= m) 
        update(l,r,L(root),val);
    else if(l > m) 
        update(l,r,R(root),val);
    else
    {
        update(l,m,L(root),val);
        update(m+1,r,R(root),val);
    }
}
int query(int x,int root) //获取层数,也就是被覆盖的线段数
{
    if(tree[root].l == tree[root].r)
        return tree[root].val;
    int m=Mid(tree[root].l,tree[root].r);
    if(x<=m) 
        return query(x,L(root)) + tree[root].val;
    else 
        return query(x,R(root)) + tree[root].val;
}
int level[32005];
int main()
{
//  freopen("in.txt","r",stdin);
//  freopen("out.txt,"w",stdout);
    int i,n,x,y;
    scanf("%d",&n);
    create(0,32000,1);
    for(i=0;i<n;i++)
    {
        scanf("%d%d",&x,&y);
        level[query(x,1)]++;
        update(x,32000,1,1);
    }
    for(i=0;i<n;i++)
        printf("%d\n",level[i]);    
    return 0;
} 

 


 

转载于:https://www.cnblogs.com/Felix-F/archive/2013/03/08/3223641.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值