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
/*先按e值从大到小,相同就x从小到大
排好后由于前面的E值已经比当前点大,只要找出S值比当前小的就满足
注意坐标+1,处理相同的,s,e
建模是关键*/
#include"iostream"
#include"cstdio"
#include"cstring"
#include"algorithm"
#define MAX 100005
using namespace std;
struct node{
int e,s;
int id;
}cow[MAX];
int N;
int c[MAX];
int a[MAX];
int cmp(const node &a,const node &b)
{
if(a.e==b.e)
return b.s > a.s;
else
return a.e > b.e;
}
int lowbit(int x)
{
return x&(-x);
}
int getsum(int x)
{
int sum=;
while(x>)
{
sum+=c[x];
x-=lowbit(x);
}
return sum;
}
void updata(int x,int d)
{
while(x<=MAX)
{
c[x]+=d;
x+=lowbit(x);
}
}
int main()
{
int i;
while(scanf("%d",&N)!=EOF)
{
if(!N)
break;
memset(c,,sizeof(c));
memset(a,,sizeof(a));
for(int i=;i<N;i++)
{
scanf("%d %d",&cow[i].s,&cow[i].e);
cow[i].s++;
cow[i].e++;
cow[i].id=i;
}
sort(cow,cow+N,cmp);
a[cow[].id]=;
updata(cow[].s,);
for( i=;i<N;i++)
{
if(cow[i].s==cow[i-].s&&cow[i].e==cow[i-].e)
a[cow[i].id]=a[cow[i-].id];
else
a[cow[i].id]=getsum(cow[i].s);
updata(cow[i].s,);
}
printf("%d",a[]);
for(int i=;i<N;i++)
printf(" %d",a[i]);
printf("\n");
}
return ;
}

Cows的更多相关文章

  1. [LeetCode] Bulls and Cows 公母牛游戏

    You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...

  2. POJ 2186 Popular Cows(Targin缩点)

    传送门 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31808   Accepted: 1292 ...

  3. POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

    传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Acce ...

  4. LeetCode 299 Bulls and Cows

    Problem: You are playing the following Bulls and Cows game with your friend: You write down a number ...

  5. [Leetcode] Bulls and Cows

    You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...

  6. 【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列

    第一次写单调队列太垃圾... 左右各扫一遍即可. #include <iostream> #include <cstdio> #include <cstring> ...

  7. POJ2186 Popular Cows [强连通分量|缩点]

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31241   Accepted: 12691 De ...

  8. Poj2186Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31533   Accepted: 12817 De ...

  9. [poj2182] Lost Cows (线段树)

    线段树 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacula ...

  10. 【POJ3621】Sightseeing Cows

    Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8331   Accepted: 2791 ...

随机推荐

  1. JVM系列一:JVM内存组成及分配

    java内存组成介绍:堆(Heap)和非堆(Non-heap)内存 按照官方的说法:“Java 虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配.堆是在 Java 虚拟机启动时 ...

  2. Jquery添加移除样式

    获取与设置样式 获取class和设置class都可以使用attr()方法来完成.例如使用attr()方法来获取p元素的class,JQuery代码如下: var p_class = $("p ...

  3. cvc-elt.1: Cannot find the declaration of element 'beans'

    @(编程) 现象描述 导入的一个eclipse项目报错,各种方法都无法解决,报错信息如下: cvc-elt.1: Cannot find the declaration of element 'bea ...

  4. *** WARNING L1: UNRESOLVED EXTERNAL SYMBOL

    kei编译时提示: *** WARNING L1: UNRESOLVED EXTERNAL SYMBOL *** WARNING L1:reference made to unresolved ext ...

  5. Python类的探讨

    我们下面的探讨基于Python3,我实际测试使用的是Python3.2,Python3与Python2在类函数的类型上做了改变 1,类定义语法  Python类定义以关键字class开头,一个类定义例 ...

  6. C#扫描仪编程、条形码识别编程资料

    扫描仪编程资料:http://www.cnblogs.com/wubh/archive/2011/11/07/2239178.html 图片条形码识别资料:http://www.codeproject ...

  7. poj3468A Simple Problem with Integers(线段树,在段更新时要注意)

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  8. 转载 使用WiX Toolset创建.NET程序发布Bootstrapper(安装策略管理)(一&二)——初识WiX

    转载fromVan Pan 的专栏   http://blog.csdn.net/rryqsh/article/details/8274832 http://blog.csdn.net/rryqsh/ ...

  9. UI:字典的两种取值的区别

    字典的两种取值的区别 (objectForKey: 和 valueForKey )参考 一般来说 key 可以是任意字符串组合,如果 key 不是以 @ 符号开头,这时候 valueForKey: 等 ...

  10. 常见Java基础知识(较长)

    1.抽象:  抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节.抽象包括两个方面,一是过程抽象, ...