Cows
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 15301   Accepted: 5095

Description

Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good.

Farmer John has N cows (we number the cows from 1 to N). Each of Farmer John's N cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval [S,E].

But some cows are strong and some are weak. Given two cows: cowi and cowj, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cowi is stronger than cowj.

For each cow, how many cows are stronger than her? Farmer John needs your help!

Input

The input contains multiple test cases.
For each test case, the first line is an integer N (1 <= N <= 105), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 105) specifying the start end location respectively of a range preferred by some cow. Locations are given as distance from the start of the ridge.

The end of the input contains a single 0.

Output

For each test case, output one line containing n space-separated integers, the i-th of which specifying the number of cows that are stronger than cowi.

Sample Input

  1. 3
  2. 1 2
  3. 0 3
  4. 3 4
  5. 0

Sample Output

  1. 1 0 0

Hint

Huge input and output,scanf and printf is recommended.
题意:两个区间:[Si, Ei] and [Sj, Ej].(0 <= S < E <= 105). 若 Si <= Sj and Ej <= Ei and Ei – Si > Ej – Sj, 则第i个区间覆盖第j个区间。给定N个区间(1 <= N <= 10^5),分别求出对于第i个区间,共有多少个区间能将它覆盖。

思路:初看好像挺复杂的。其实可以把区间[S, E]看成点(S, E),这样题目就转化为hdu 1541 Stars。只是这里是求该点左上方的点的个数。

虽然如此,我还是WA了不少,有一些细节没注意到。给点排序时是先按y由大到小排序,再按x由小到大排序。而不能先按x排序。比如n=3, [1,5], [1,4], [3,5]的例子。另外还要注意对相同点的处理。

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. const int MAX = ;
  7.  
  8. struct Node{
  9. int x, y, id, ans;
  10. }seq[MAX];
  11. int sum[MAX], n;
  12.  
  13. int cmp1(Node a,Node b){
  14. if(a.y==b.y) return a.x<b.x;
  15. return a.y>b.y;
  16. }
  17. int cmp2(Node a,Node b){
  18. return a.id<b.id;
  19. }
  20.  
  21. int lowbit(int x){
  22. return x & (-x);
  23. }
  24. void add(int pos, int val){
  25. while(pos < MAX){
  26. sum[pos]+=val;
  27. pos+=lowbit(pos);
  28. }
  29. }
  30. int getsum(int pos){
  31. int res = ;
  32. while(pos>){
  33. res+=sum[pos];
  34. pos-=lowbit(pos);
  35. }
  36. return res;
  37. }
  38.  
  39. int main()
  40. {
  41. freopen("in.txt","r",stdin);
  42. int i,j;
  43. while(scanf("%d", &n) && n){
  44. for(i=;i<=n;i++){
  45. scanf("%d%d", &seq[i].x, &seq[i].y);
  46. seq[i].x++, seq[i].y++;
  47. seq[i].id = i;
  48. }
  49. sort(seq+,seq+n+,cmp1);
  50. memset(sum, , sizeof(sum));
  51. seq[].ans = ;
  52. add(seq[].x, );
  53. int fa = ;
  54. for(i=;i<=n;i++){
  55. if(seq[i].x == seq[fa].x && seq[i].y == seq[fa].y){
  56. seq[i].ans = seq[fa].ans;
  57. }else{
  58. fa = i;
  59. seq[i].ans = getsum(seq[i].x);
  60. }
  61.  
  62. add(seq[i].x, );
  63. }
  64. sort(seq+,seq+n+,cmp2);
  65. printf("%d", seq[].ans);
  66. for(i=;i<=n;i++) printf(" %d", seq[i].ans);
  67. printf("\n");
  68. }
  69. return ;
  70. }

Cows(poj 2481 树状数组)的更多相关文章

  1. Cows POJ - 2481 树状数组

    Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can ...

  2. POJ 3321 树状数组(+dfs+重新建树)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27092   Accepted: 8033 Descr ...

  3. POJ 2352Stars 树状数组

    Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42898   Accepted: 18664 Descripti ...

  4. poj 2299 树状数组求逆序数+离散化

    http://poj.org/problem?id=2299 最初做离散化的时候没太确定可是写完发现对的---由于后缀数组学的时候,,这样的思维习惯了吧 1.初始化as[i]=i:对as数组依照num ...

  5. MooFest POJ - 1990 (树状数组)

    Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a social gather ...

  6. poj 3928 树状数组

    题目中只n个人,每个人有一个ID和一个技能值,一场比赛需要两个选手和一个裁判,只有当裁判的ID和技能值都在两个选手之间的时候才能进行一场比赛,现在问一共能组织多少场比赛. 由于排完序之后,先插入的一定 ...

  7. POJ 2299 树状数组+离散化求逆序对

    给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...

  8. poj 2299 树状数组求逆序对数+离散化

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 54883   Accepted: 20184 ...

  9. poj 2182 树状数组

    这题对于O(n^2)的算法有很多,我这随便贴一个烂的,跑了375ms. #include<iostream> #include<algorithm> using namespa ...

随机推荐

  1. 分数(有理数)的四则运算PAT1088

    2015-02-05 PAT- B1088. Rational Arithmetic (20) http://www.patest.cn/contests/pat-a-practise/1088 #i ...

  2. Escape character is '^]'. Connection closed by foreign host.

    今天在用易汇金的接口回调时候,老是回调不到我的机器上面.我的ip通过公网映射,按说是可以访问到我的ip,思考是什么问题. 1.防火墙关闭,不行 2.防火墙开启,但是把自己的端口号改为可以访问(参考:h ...

  3. zoj 3772 Calculate the Function

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 这道题需要构造矩阵:F(X)=F(X-1)+F(X-2)*A(X)转化为 ...

  4. 通过Excel来集中管理资源文件

     在支持双语或多语种项目中,常常需要编辑多个文件来添加资源项,感觉比较繁琐,所以想做一个可以集中管理资源文件的工具.借助Excel,使用Excel来记录,并且通过Excel可以进行分页分模块来规划 ...

  5. Java应用开发的一条经验

    一旦为应用建立良好的基础设施, 后续的开发就会变得容易而快速.  这些基础设施包括: 1.   线程池的建立.配置: 在 JDK 并发库的基础上建立更适合于应用的并发使用接口: 2.   跨多数据源的 ...

  6. Dom4j解析xml格式的字符串【java】

    一般我们会使用dom4j.SAX.w3c来解析xml文件,网上也大多提供此类解决方案. 但在实际项目中,也有会解析xml格式的字符串报文的. 比如,有如下字符串: String = "< ...

  7. 链表list容器中通过splice合并链表与merge的不同,及需要注意的问题

    #include "stdafx.h" #include <iostream> #include <list> #include <algorithm ...

  8. 百度地图 Android SDK - 检索功能使用的简单演示样例

    百度地图 SDK 不仅为广大开发人员提供了炫酷的地图展示效果.丰富的覆盖物图层,更为广大开发人员提供了多种 LBS 检索的能力. 通过这些接口,开发人员能够轻松的訪问百度的 LBS 数据,丰富自己的移 ...

  9. [R语言画图]气泡图symbols

    绘制气泡图主要使用函数symbols(x,y,circle=r).当中x.y是坐标轴,r是每一个点的半径. x<-rnorm(10) y<-rnorm(10) r<-abs(rnor ...

  10. SQL-SERVER2008登录错误233

    问题 : SQL Server2008在本地使用Windows身份验证登录或是使用SQL Server身份认证登录时报错:已成功与服务器建立连接,但是在登录过程中发生错取.(provider:共享内存 ...