Cows
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 15222   Accepted: 5070

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

3
1 2
0 3
3 4
0

Sample Output

1 0 0

Hint

Huge input and output,scanf and printf is recommended.
 
题意:线段起点s,终点e,问每一个线段包含在几个线段里面,样例中线段[1,2]包含在[0,3]里面所以第一个输出是1,
思路:按照e按照从大到小排序,e相同按照s从小到大排序。因为只有e从大到小排序后,s就可以从小到大更新了,
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <string.h>
using namespace std;
const int MAX = + ;
struct node
{
int s,e;
int index;
};
node a[MAX];
int c[MAX],cnt[MAX];
int cmp(node x,node y)
{
if(x.e == y.e)
return x.s < y.s;
return x.e > y.e;
}
int lowbit(int k)
{
return k & (-k);
}
void add (int k,int num)
{
for(int i = k; i < MAX; i += lowbit(i))
c[i] += num;
}
int sum(int k)
{
int s = ;
for(int i = k; i > ; i -= lowbit(i))
s += c[i];
return s;
}
int main()
{
int n;
while(scanf("%d", &n) != EOF && n)
{
for(int i = ; i < n; i++)
{
scanf("%d%d", &a[i].s, &a[i].e);
a[i].index = i;
}
sort(a,a + n,cmp);
memset(c,,sizeof(c));
memset(cnt,,sizeof(cnt));
cnt[a[].index] = ;
add (a[].s + , );
for(int i = ; i < n; i++)
{
if(a[i].s == a[i - ].s && a[i].e == a[i - ].e)
cnt[a[i].index] = cnt[a[i - ].index];
else
cnt[a[i].index] = sum(a[i].s + );
add(a[i].s + ,);
}
printf("%d",cnt[]);
for(int i = ; i < n; i++)
printf(" %d",cnt[i]);
printf("\n");
} return ;
}

最新文章

  1. 【开源】OSharp3.0框架解说系列(6.2):操作日志与数据日志
  2. java解析XML(转载)
  3. Android 设置代理(验证用户名和密码)
  4. android 入门-android Studio git配置
  5. K近邻分类算法实现 in Python
  6. Raft、Zab
  7. JavaWeb国际化
  8. 495. Kids and Prizes
  9. TextView属性详解
  10. CF Zepto Code Rush 2014 B. Om Nom and Spiders
  11. FTP&amp;samba 服务简单部署
  12. 002Java概述
  13. MySQL连接及基本信息查看命令小结
  14. h5微信页面在手机微信端和微信web开发者工具中都能正常显示,但是在pc端微信浏览器上打不开(显示空白)
  15. 杂谈2.cpp
  16. java中的数据加密5 数字证书
  17. react 传递非state给子元素的注意事项
  18. Hive sql和Presto sql的一些对比
  19. Template parse errors: The pipe &#39;translate&#39; could not be found
  20. python range函数与numpy arange函数,xrange与range的区别

热门文章

  1. 未能加载文件或程序集“Enyim.Caching”或它的某一个依赖项。未能验证强名称签名
  2. 【转】【Asp.Net】了解使用 ASP.NET AJAX 进行局部页面更新
  3. 区块链技术(一):Truffle开发入门
  4. PyQT制作视频播放器
  5. jQuery使用ajax跨域获取数据
  6. SpringMVC数据验证
  7. java 中Handler 和Runnable 的使用 异步发送消息 转
  8. #Linux学习笔记# Linux文件的属性以及权限说明
  9. [USACO2005][POJ3169]Layout(差分约束)
  10. 重构笔记---MEF框架(下)