HDU 1541 Stars (线段树)

Problem 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

Source

Ural Collegiate Programming Contest 1999

题目意思: 给你n个坐标,然后其中任意一个点,假如有s个点的横坐标和纵坐标都不大于这个点,那么这个点的价值为s,

最后让你输出 价值从1~n-1,的点的个数

比赛的时候有一个队做过这个题,很快做出来了,我纠结好久(太菜了%>_<%),一直想着给x排序,然后再给y排序,

希望得出什么规律,最后想到了给x排序后,判断这个点的价值,需要快速查询前面点(按x以排序)不比他大的的点的个数(数据很大,易超时),终于想到线段树(⊙o⊙)…,用线段树保存le~ri已经存在的个数

具体的思路说完了,上代码了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
#define N 32005

struct stud1{
int le,ri;
int va;
}f[N*4]; //刚开始没有乘4,搞得答案一直对不上

struct stud{
int x,y;
}s[N];

int ans[N];

int cmp(stud a,stud b)
{
    if(a.x==b.x)
        return a.y<b.y;  //记得x相同时排y
    return a.x<b.x;
}

void build(int pos,int le,int ri)
{
    f[pos].le=le;
    f[pos].ri=ri;
    f[pos].va=0;
    if(le==ri)  return ;

    int mid=MID(le,ri);

    build(L(pos),le,mid);
    build(R(pos),mid+1,ri);
}

void update(int pos,int le)
{
    f[pos].va++;
    if(f[pos].le==le&&le==f[pos].ri)
        return ;

    int mid=MID(f[pos].le,f[pos].ri);

    if(mid>=le)
        update(L(pos),le);
    else
        update(R(pos),le);
}

int query(int pos,int le,int ri)
{
    if(f[pos].le==le&&f[pos].ri==ri)
        return f[pos].va;

    int mid=MID(f[pos].le,f[pos].ri);

    if(mid>=ri)
        return query(L(pos),le,ri);
    else
        if(mid<le)
        return query(R(pos),le,ri);
    return query(L(pos),le,mid)+query(R(pos),mid+1,ri);
}

int main()
{
     int i,n;
     while(~scanf("%d",&n))
     {
         for(i=0;i<n;i++)
            scanf("%d%d",&s[i].x,&s[i].y);

          memset(ans,0,sizeof(ans));

          sort(s,s+n,cmp);

          build(1,0,N-1);

          for(i=0;i<n;i++)
          {
              int x=query(1,0,s[i].y);
              ans[x]++;
              update(1,s[i].y);
          }

          for(i=0;i<n;i++)
            printf("%d\n",ans[i]);
     }
     return 0;
}
时间: 2024-08-10 19:10:04

HDU 1541 Stars (线段树)的相关文章

HDU 1541 Stars (线段树||树状数组)

Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4897    Accepted Submission(s): 1934 Problem Description Astronomers often examine star maps where stars are represented by points on a plan

HDU 1541 Stars (树状数组)

Problem 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

HDU 1541 Stars(树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 解析: 题意:大概就是计算每颗星星左下边包括了多少颗星星,这个数值就是level.左下边不包括本身,不超过本身的x,y的坐标,可以等于.问每种level有多少颗星星. 这题,一开始想不到怎么用到树状数组,后来看了一下,发现题目给的数据是已经按x,y排好序的,所以我们可以不用管y的值. 注意: 1.每次输入一个坐标对之后,都要计算一下这个它的level. 2.此题的x坐标可以为0,而树状数组是从

hdu 1541 Stars 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目意思:有 N 颗星星,每颗星星都有各自的等级.给出每颗星星的坐标(x, y),它的等级由所有比它低层(或者同层)的或者在它左手边的星星数决定.计算出每个等级(0 ~ n-1)的星星各有多少颗. 我只能说,题目换了一下就不会变通了,泪~~~~ 星星的分布是不是很像树状数组呢~~~没错,就是树状数组题来滴! 按照题目输入,当前星星与后面的星星没有关系.所以只要把 x 之前的横坐标加起来就可以了

HDU 1542 Atlantis 线段树+离散化+扫描线

题意:给出一些矩形的最上角坐标和右下角坐标,求这些矩形的面积并. NotOnlySuccess 线段树专辑中扫描线模板题,弱智的我对着大大的代码看了一下午才搞懂. 具体见思路见注释=.= #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #define lson rt<<1,l,mid #define rson rt<<1|1,mid

HDU 1828 Picture 线段树+扫描线

题意:给你一些矩形的左上角点的坐标和右下角点的坐标,求周长并 最显而易见的思路就是对于x轴和y轴做两次扫描线,对于负数的坐标进行离散化.每次增加的值是线段变化量的绝对值.具体写法和求面积并的差不多. #include <cstdio> #include <algorithm> #include <cstring> #include <vector> using namespace std; #define lson rt << 1 , l , m

HDU 1542 Atlantis(线段树扫描线)

http://acm.hdu.edu.cn/showproblem.php?pid=1542 Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6788    Accepted Submission(s): 2970 Problem Description There are several ancient Greek

POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算

求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的,这样理论上是对的 要注意处理高度相同的线段,把底边优先处理(在代码里就是f标记为1的线段),因为若是一个矩形的底边和另一个矩形的上边重合,则这个轮廓肯定不能算 不过POJ和HDU的数据好像都比较弱,我没进行上面的细节处理也AC了,不过一个很简单的数据就会不对,所以还是要处理一下才是真正正确的代码 我

hdu 1542 Atlantis(线段树&amp;扫描线&amp;面积并)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6386    Accepted Submission(s): 2814 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

hdu 1828 Picture(线段树&amp;扫描线&amp;周长并)

Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2578    Accepted Submission(s): 1363 Problem Description A number of rectangular posters, photographs and other pictures of the same shap