poj 2352 Stars(线段树)

poj 2352 Stars(线段树)
Time Limit: 1000ms Memory Limit: 65536kB

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
Ural Collegiate Programming Contest 1999


首先题意非常清楚,可以无视y坐标,不断一边计算一边维护x坐标,可以视为在线算法(online)。朴素写了一下,TLE。
根据题目特点,我突然脑海中回忆起来听说过一种数据结构——线段树,虽然没写过,但是其原理听过,所以这个题用线段树做。用数组模拟树结点。线段树是一种很常见的大众数据结构,没什么好说的,只是我这回写的太生疏,TLE了好久。
要注意两个问题:
1.下标+1,-1,会不会在区间[1,2],[1,1],[0,0]上死循环的细节问题。
2.查询时候一定要注意,如果看到了区间恰好符合就不要分了,如果一直分到叶子才罢休,那么查询数据规模x的问题复杂度从O(log(x))变成了O(x),当然TLE。如果忘了这一点就是忘了线段树的精髓,看来我只是听说过线段树,对精髓理解地不够深刻,所以会忘了关键一步。
第二个问题是调试出来的,我TLE后调试,尝试做了连续插入与连续查询,发现查询不会死循环,但是耗时极大,所以发现了查询的bug。


Accepted    2116kB  91ms    919 B   G++
#define MAX_N 15000
#define MAX_X (2<<14)

#include<stdio.h>

int x,n;
int level;
int num[MAX_N];
int tree[MAX_X<<1];

int get(int l,int r,int node,int node_l,int node_r)
{
    if (node_l==l && node_r==r)
        return tree[node];
    int mid=(node_l+node_r)>>1,l_son=(node<<1)+1,r_son=(node<<1)+2;
    if (r<=mid)
        return get(l,r,l_son,node_l,mid);
    if (mid+1<=l)
        return get(l,r,r_son,mid+1,node_r);
    return get(l,mid,l_son,node_l,mid)
          +get(mid+1,r,r_son,mid+1,node_r);
}

void insert(int x,int node,int node_l,int node_r)
{
    int mid=(node_l+node_r)>>1,l_son=(node<<1)+1,r_son=(node<<1)+2;
    tree[node]++;
    if (node_l==node_r)
        return;
    if (x<=mid)
        insert(x,l_son,node_l,mid);
    else
        insert(x,r_son,mid+1,node_r);
    return;
}

int main()
{
    scanf("%d",&n);
    for (int i=0;i<n;i++)
    {
        scanf("%d%*d",&x);
        num[get(0,x,0,0,MAX_X-1)]++;
        insert(x,0,0,MAX_X-1);
    }
    for (int i=0;i<n;i++)
        printf("%d\n",num[i]);
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值