ACM: 线段树 poj 2352

来源:互联网 发布:mac安装jdk 编辑:程序博客网 时间:2024/06/05 05:08
Stars

Description

Astronomers often examine starmaps where stars are represented by points on a plane and each starhas Cartesian coordinates. Let the level of a star be an amount ofthe stars that are not higher and not to the right of the givenstar. Astronomers want to know the distribution of the levels ofthe stars.
For example, look at the mapshown 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 thelevels of the stars numbered by 2 and 4 are 1. At this map thereare only one star of the level 0, two stars of the level 1, onestar of the level 2, and one star of the level 3.

You are to write a program that will count the amounts of the starsof each level on a given map.

Input

The first line of the input filecontains a number of stars N(1<=N<=15000). The following N linesdescribe coordinates of stars (two integers X and Y per lineseparated by a space, 0<=X,Y<=32000).There can be only one star at one point of the plane. Stars arelisted in ascending order of Y coordinate. Stars with equal Ycoordinates are listed in ascending order of Xcoordinate.

Output

The output should contain Nlines, one number per line. The first line contains amount of starsof the level 0, the second does amount of stars of the level 1 andso on, the last line contains amount of stars of the levelN-1.

Sample Input

5
1 1
5 1
7 1
3 3
5 5

Sample Output

1
2
1
1
0

 

题意: 计算每种等级的星星的个数, 每个星星的等级是它坐标左下方的星星个数.

 

解题思路:

    1.题意很明显, 计算个数的问题. 第一个想到的是线段树+树状数组解决此类问题.

    2.如果每次输入一个星星坐标就查找前面的星星时间复杂度是O(n^2),1<=N<=15000

       范围有点大.显然不可行.

    3.引入一种数据结构---线段树

      问题分析:

      定义:线段树是一种二叉搜索树, 在一个区间[1,n]中,最终划分为一些[1,1], [2,2], ... ,[n,n];

            这些成为单元区间,当然划分还有一种划分方法是[i,i+1], 这个看具体问题具体分析.

            并且每个单元区间对应线段树中的每一个叶子结点.并且每个结点num来记录覆盖这个结点的

           线段条数.顾名思义:线段树     下图是一个区间为[1,7]的线段树

      ACM: <wbr>线段树 <wbr>poj <wbr>2352

      处理对象: 在一个较狭窄的区间上, 统计相应的变量. 例如[a,b]区间, 可以分成[a,(a+b)/2],

      [(a+b)/2,b]左右子树进行某个参数的统计.这样就不用每次统计的时候都访问每个区间,时间

      复杂度可以从O(n)降到O(logN). 其实可以跟二分查找想类比.这样会好理解.

       相应的操作:建树, 删除, 增加, 查询.

      建树: 可以采用树状数组.左右子树递归建树, 我采用第一种划分方法:[i,i];

             即:pt[pos].left = pos*2,  pt[pos].right = pos*2+1,pt[pos].num = 0

      删除: 假设一个点的关键码为X, 至上而下地搜索;

            结束标志 pt[pos].right == pt[pos].left&& pt[pos].right == X

            再左右递归二分每个区间相应的pt[pos].num--; 每个区间删除X所在的线段.

       增加:是删除的逆过程. 只要改动pt[pos].num++; 将X加入所在的区间即可;

      查询: 查询一个区间线段[l,r], 在整个集合出现的次数;

             结束标志:pt[pos].left==l &&pt[pos].right==r;

            假设: mid = (pt[pos].left+pt[pos].right)/2;

                 当l> mid 时, 表示线段在当前子树根的右子树上,递归右子树find(l,r,pos*2+1);

                 当r<= mid 时, 表示线段在当前子树根的左子树上, 递归左子树find(l,r,pos*2);

                 当l<=mid < r时,表示当前线段被划分在当前子树根的左子树和右子树上,

                            递归的时候左右子树都要find(l,mid,pos*2)+find(mid+1,r,pos*2+1);

      4. 有了线段树的知识,解决这题就简单了. 输入的坐标是有序的, 因此只考虑x坐标即可.

         每输入一个就查询一个星星的等级, 然后将这个星星插入线段树. 并且保持星星相应等级个数.

         到这里问题完美解决.

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 32005

struct node
{
 int left, right;
 int num;
}pt[MAX*3];

int n;
int result[MAX/2];
int x, y;

void build_tree(int l, int r, int pos)
{
 pt[pos].left = l;
 pt[pos].right = r;
 pt[pos].num = 0;
 if(l == r) return ;
 int mid = (l+r)/2;
 build_tree(l,mid,pos*2);
 build_tree(mid+1,r,pos*2+1);
}

int find(int l, int r, int pos)
{
 if(pt[pos].left == l&& pt[pos].right == r)
  return pt[pos].num;

 int sum = 0;
 int mid = (pt[pos].left+pt[pos].right)/2;
 if(l > mid)
  sum += find(l,r,pos*2+1);
 else if(r <= mid)
  sum += find(l,r,2*pos);
 else
  sum += (find(l,mid,2*pos) +find(mid+1,r,2*pos+1));
 return sum;
}

void add(int x, int pos)
{
 pt[pos].num++;
 if(pt[pos].left == pt[pos].right&& pt[pos].right == x)
  return ;
 int mid = (pt[pos].left+pt[pos].right)/2;
 if(x <= mid)
  add(x,2*pos);
 else
  add(x,2*pos+1);
}

int main()
{
// freopen("input.txt","r",stdin);
 while(scanf("%d",&n) !=EOF)
 {
  memset(result,0,sizeof(result));
  build_tree(0,MAX,1);
  for(int i = 0; i< n; ++i)
  {
   scanf("%d%d",&x, &y);
   result[find(0,x,1)]++;
   add(x,1);
  }
  for(int k = 0; k< n; ++k)
   printf("%d\n",result[k]);
 }
 return 0;
}
   

0 0