poj 2352 Stars 树状数组

简介: 树状数组,简单题,我刚刚开始学的时候就a了,不多说什么了,直接贴代码。

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.………………


树状数组,简单题,我刚刚开始学的时候就a了,不多说什么了,直接贴代码。

#include<stdio.h>
#include<string.h>
int a[32002];
int level[15002];
int lowbit(int x)
{
  return x&(-x);
}
void insert(int x)
{
  while(x<32002)
  {
    a[x]++;
    x += lowbit(x);
  }
}
int sum(int x)
{
  int s=0;
  while(x)
  {
    s += a[x];
    x -= lowbit(x);
  }
  return s;
}
int main()
{
  int n, i, x, y;
  scanf("%d",&n);
  memset(a,0,sizeof(level));
  memset(level,0,sizeof(level));
  for(i = 0; i < n; i++)
  {
    scanf("%d%d",&x,&y);
    x++;
    level[sum(x)]++;
    insert(x);
  }
  for(i= 0; i < n; i++)
    printf("%d\n",level[i]);
  return 0;
}
目录
相关文章
|
7月前
华为机试HJ44:Sudoku(数独问题,深度优先遍历DFS解法)
华为机试HJ44:Sudoku(数独问题,深度优先遍历DFS解法)
|
7月前
|
人工智能
poj 2299 Ultra-QuickSort 求逆序数 树状数组解法
所谓离散化,我们的了解就是对原数组排序,然后用所在位置的下标代替原数,这样我们就可以把数据范围缩小到1-500000,这个数组是开的下的。
22 0
|
9月前
|
存储 算法 C语言
【动态规划】LeetCode 312. 戳气球 --区间DP问题
因为一些事,最近状态不是很好,加上今天的每日一题有点难,看的烦躁(就是菜 ,就来更新一下今天与每日一题相关的区间Dp问题(戳气球),这篇也是关于区间Dp的问题 uu可以看看
74 0
|
12月前
51nod 1711 平均数(二分 + 树状数组好题)
51nod 1711 平均数(二分 + 树状数组好题)
76 0
|
机器学习/深度学习
HDOJ(HDU) 2083 简易版之最短距离(中位数)
HDOJ(HDU) 2083 简易版之最短距离(中位数)
117 0