星星的等级
Time Limit: 3000ms, Special Time Limit:6000ms, Memory Limit:65536KB
Total submit users: 93, Accepted users: 43
Problem 10069 : No special judgement
Problem description
天空中有很多星星,我们把天空看作一个二维的平面,则可以将每一个星星按照物理位置给一个坐标值(x,y)。
并且定义两个星星a,b的等级高低关系可以有如下约束来确定:
如果 Xa <= Xb 且 Ya >= Yb ,则认为星星a的等级比b高(若两个星星在同一位置,则认为它们等级相同)。
现在的问题是:假定天空中有N颗星星,并且知道每一颗星星的坐标。你能够很快计算出来对于每一颗星星,有多少星星等级比他高吗?
我们希望你的计算速度越快越好,同时不希望你用暴力搜索的方法解决该问题,因为天空中的星星很多很多,以现在计算机的速度也要很久的,我们只取了一小部分都有100000个了,暂时就计算这么多。 
Input
输入将有很多组测试数据,每个测试数据都是以一个整数N(1 <= N <= 100000)开始, 随后有N行, 每行2个整数X, Y (1<=X,Y<=10^9), 则是N颗行星的位置坐标。 一直计算到输入结束为止。
Output
对于每一组测试数据,输出一行,由N个数字组成,按照输入的星星的顺序,每一个数字表示有多少星星等级大于该星星,数字之间有且只有一个空格,行尾请不要输出多余的空格。
Sample Input
3
1 2
0 3
3 4
Sample Output
1 0 0
Problem Source
HNU

一个比较类似的问题就是POJ_2352 Stars

Stars

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 22464

Accepted: 9787

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

可以用线段数做,也可以用树状数组。 还是感觉树状数组比较简单一些。

/*
POJ 2352 Stars
就是求每个小星星左小角的星星的个数。坐标按照Y升序,Y相同X升序的顺序给出
由于y轴已经排好序,可以按照x坐标建立一维树状数组
*/
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int MAXN=15010;
const int MAXX=32010;
int c[MAXX];//树状数组的c数组
int cnt[MAXN];//统计结果
int lowbit(int x)
{return x&(-x);
}
void add(int i,int val)
{while(i<=MAXX){c[i]+=val;i+=lowbit(i);}
}
int sum(int i)
{int s=0;while(i>0){s+=c[i];i-=lowbit(i);}return s;
}
int main()
{//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);int n;int x,y;while(scanf("%d",&n)!=EOF){memset(c,0,sizeof(c));memset(cnt,0,sizeof(cnt));for(int i=0;i<n;i++){scanf("%d%d",&x,&y);//加入x+1,是为了避免0,X是可能为0的int temp=sum(x+1);cnt[temp]++;add(x+1,1);}for(int i=0;i<n;i++)printf("%d\n",cnt[i]);}return 0;
}

思路:先对星星按照x的值由小到大排序,x相等的按照y排序(就是在插入y的时候把x相等,y较小的先插入)。然后我们在插入y的值,因为x已经排好序了,我们要求得就是y之前有多少数已经插入了。这样求得的就是这颗星的等级。

补充:用数状数组时,我们其实虚构了一个数组B(初始元素都为0,表示没有数插入,有一个数加入就加1,我们求得就是插入的数的个数的总和)。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<cstring>
using namespace std;
const int NUM = 15005;
const int MAXSIZE = 32005;
struct node{int x;int y;friend bool operator<(const node& e1,const node& e2){if(e1.x == e2.x) return e1.y<e2.y;return e1.x<e2.x;}
};
node data[NUM];
int count1[MAXSIZE];
int degree[NUM];
int n;
int max1;
int lowbit(int l){return l&(-l);
}
void update(int l){while(l<=max1){count1[l]+=1;l += lowbit(l);}
}
int getSum(int l){int sum = 0;while(l>0){sum+=count1[l];l-=lowbit(l);}return sum;
}
int main(){scanf("%d",&n);max1 = 0;memset(count1,0,sizeof(int)*MAXSIZE);memset(degree,0,sizeof(int)*NUM);for(int i =1;i<=n;i++){scanf("%d%d",&data[i].x,&data[i].y);data[i].y+=1;if(data[i].y>max1) max1 = data[i].y;}sort(data+1,data+n+1);for(int i =1;i<=n;i++){degree[getSum(data[i].y)]++;update(data[i].y);}for(int i = 0;i<=n-1;i++){cout<<degree[i]<<endl;}return 0;
}

结论:树状数组的实现,空间复杂度低,编程复杂度低,容易扩展到多维情况,是一个可以很高效的进行区间统计的数据结构。

但它的适用范围小,对其可以进行的运算也有限制。比如说如果每次修改的是某一段区间,而非一个元素,维护代价还是相当高的。

关于树状数组

http://lib.csdn.net/article/c/41020

http://blog.csdn.net/int64ago/article/details/7429868

http://www.cnblogs.com/iwantstrong/p/5925595.html

树状数组

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;const int MAXN = 32005;int c[MAXN],level[MAXN],n;int lowbit(int x){return x & (-x);}// 求前n项的和
int sum(int n){int sum = 0;while(n > 0){sum += c[n];n -= lowbit(n);}return sum;
}
// 增加某个元素的大小
void add(int x){while(x <= MAXN){++c[x];x += lowbit(x);}
}int main(){int n,x,y;while(~scanf("%d",&n)){memset(level, 0, sizeof(level));memset(c, 0, sizeof(c));for(int i=0; i<n; ++i) {scanf("%d%d",&x,&y);++x;level[sum(x)]++;add(x);}for(int i=0; i<n; ++i)printf("%d\n",level[i]);}return 0;
}

线段树

#include<iostream>
#include<cstdio>
#include<cstring>
#define mid ((left+right)>>1)
#define lson rt<<1,left,mid
#define rson rt<<1|1,mid+1,right
using namespace std;const int MAXN = 32005;int sum[MAXN<<2],level[MAXN<<2];void update(int rt,int left,int right,int data){++sum[rt];if(left==right) return;if(data <= mid) update(lson,data);else update(rson,data);
}
int query(int rt,int left,int right,int l,int r){if(left==l && right==r) {return sum[rt];}int m = mid;if(r <= m) return query(lson,l,r);else if(l > m) return query(rson,l,r);else return query(lson,l,m)+query(rson,m+1,r);
}int main(){int n,x,y;while(~scanf("%d",&n)){memset(sum, 0, sizeof(sum));memset(level, 0, sizeof(level));for(int i=0; i<n; ++i){scanf("%d%d",&x,&y);++x;++level[query(1,1,MAXN,1,x)];update(1,1,MAXN,x);}for(int i=0; i<n; ++i)printf("%d\n",level[i]);}return 0;
}

HNUOJ_10069相关推荐

最新文章

  1. 负载测试值mpstat的使用技巧
  2. 数字进度条组件NumberProgressBar
  3. python中分支结构与c语言中有何区别_C语言的分支语句有哪几种?C语言分支结构的基本形式是什么,区别是什么?...
  4. .NET base与this
  5. Python入门100题 | 第080题
  6. 部标JT1078协议RTP包解析
  7. python with语句_python中的with语句
  8. Linux用ICMP协议实现简单Ping网络监测功能
  9. 项目实战-药品采购系统-day01
  10. python参数检验框架_python-wtforms框架如何自定义校验器的原理和方法总结
  11. SAP License:烟草行业ERP选型
  12. JavaScript学习(五十八)—作用域链
  13. python内置函数用来返回序列中所有元素之和_Python内置函数——compile
  14. 卷毛机器人抢大龙_LOL:机器人史诗级加强,如果他还没退役,SKT都不敢放机器人...
  15. matlab 图案 柱状图_如何用matlab画柱形图 - 卡饭网
  16. ios html5 app上架,H5封装的IOS应用上架App Store被拒怎么办
  17. c语言中的各种变量是如何存储的(-)
  18. C++病毒-----------混乱鼠标
  19. turtle.write方法使用说明
  20. mdio phy(bcm5482)访问

热门文章

  1. 玩转树莓派,看这一篇文章就够了
  2. Java学习记录五(多线程、网络编程、Lambda表达式和接口组成更新)
  3. 利用Python实现录音播放并翻译,真正的实时进行翻译
  4. apt update时出现Duplicate sources.list entry http://mirrors.tuna.tsinghua.edu.cn/ubuntu xenial Release
  5. 我,大二实习,996,月工资800
  6. 适合女性的4个计算机相关岗位,薪资待遇好,未来前景广阔
  7. LeetCode 例题精讲 | 08 排列组合问题:回溯法的候选集合
  8. python 可视化界面学习简易教程
  9. windows下wamp安装mongodb
  10. C#实现语音合成功能