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

这题的做法和star那道题差点儿相同,先按x坐标进行升序排列,然后x同样的取对y进行降序排列,然后每次循环推断当前线段和上一条线段是不是x。y都一样,假设一样就直接等于上一条算出的值。不等于就计算。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxn 100006
struct node{
int x,y,id,num;
}a[maxn];
int b[maxn];
bool cmp(node a,node b){
if(a.x==b.x)return a.y>b.y;
return a.x<b.x;
}
bool cmp1(node a,node b){
return a.id<b.id;
} int lowbit(int x){
return x&(-x);
}
void update(int pos,int num){
while(pos<=maxn){
b[pos]+=num;pos+=lowbit(pos);
}
}
int getsum(int pos)
{
int num=0;
while(pos>0){
num+=b[pos];pos-=lowbit(pos);
}
return num;
} int main()
{
int n,m,i,j,t;
while(scanf("%d",&n)!=EOF && n!=0)
{
memset(a,0,sizeof(a));
for(i=1;i<=n;i++){
scanf("%d%d",&a[i].x,&a[i].y);
a[i].x++;a[i].y++;
a[i].id=i;
}
memset(b,0,sizeof(b));
sort(a+1,a+1+n,cmp);
for(i=1;i<=n;i++){
if(a[i].x==a[i-1].x && a[i].y==a[i-1].y){
a[i].num=a[i-1].num;
}
else{
a[i].num=getsum(maxn)-getsum(a[i].y-1);
}
update(a[i].y,1);
}
sort(a+1,a+1+n,cmp1);
for(i=1;i<=n;i++){
if(i==n)printf("%d\n",a[i].num);
else printf("%d ",a[i].num);
}
}
return 0;
}

poj2481 Cows的更多相关文章

  1. POJ-2481 Cows (线段树单点更新)

    题目大意:给n个区间,对于任意一个区间,求出能完全包含它并且长度比它长的区间的个数. 题目分析:将一个区间视为二位坐标系中的一个点,左端点视作横坐标,右端点视作纵坐标.则题目变成了求每个点的左上方.正 ...

  2. poj2481 Cows 树状数组

    题目链接:http://poj.org/problem?id=2481 解题思路: 这道题对每组数据进行查询,是树状数组的应用.对于二维的树状数组, 首先想到排序.现在对输入的数据按右值从大到小排序, ...

  3. 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)

    转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...

  4. POJ2481:Cows(树状数组)

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

  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. POJ 2186 Popular Cows(Targin缩点)

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

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

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

  8. LeetCode 299 Bulls and Cows

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

  9. [Leetcode] Bulls and Cows

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

随机推荐

  1. vue之组件的使用(转载)

    在工程目录/src下的component文件夹下创建一个 firstcomponent.vue并写仿照 App.vue 的格式和前面学到的知识写一个组件. <template> <d ...

  2. 为什么我的 app:actionViewClass="android.widget.SearchView"和app:showAsAction="ifRoom|collapseActionView"才有

    http://blog.csdn.net/cdnight/article/details/48029911 <item android:id="@+id/action_search&q ...

  3. InnoDB INFORMATION_SCHEMA Tables about Compression

    InnoDB INFORMATION_SCHEMA Tables about Compression 了解关于压缩的InnoDB INFORMATION_SCHEMA表,可以深入了解压缩的整体运行情况 ...

  4. elementary 5.0 安装 chrome

    sudo apt install google-chrome-stable wget -q -O - https://dl.google.com/linux/linux_signing_key.pub ...

  5. Linux组和提权

    目 录 第1章 组命名管理**    1 1.1 group组信息和密码信息    1 1.1.1 /etc/group 组账户信息    1 1.1.2 /etc/gshadow 组密码信息     ...

  6. python书籍推荐:量化投资:以Python为工具

    所属网站分类: 资源下载 > python电子书 作者:mimi 链接:http://www.pythonheidong.com/blog/article/451/ 来源:python黑洞网 内 ...

  7. Python装饰器粗解学习

    此次学习资料详细来自:http://blog.csdn.net/u013471155 本次是粗学,仍有诸多疑问,暂且记录一二,如有不足和建议,希望可以达者指点. 三个关键点理解:   1.关于函数“变 ...

  8. 剑指Offer(书):打印从1到最大的n位数

    题目:输入数字N,按顺序打印出从1到最大的N位十进制数,比如输入3,则打印出1.2.3一直到999 分析:N的范围不定.所以有可能超出范围,因此用数组存放以及输出.说实话,对复杂递归还是一头雾水 pu ...

  9. 【02】使用Firebug查看和编辑HTML和CSS

    使用Firebug查看和编辑HTML和CSS 描述 在本章节的教程中,我们将讨论如何使用Firebug查看和编辑HTML和CSS. 使用Firebug查看和编辑HTML 在你要查看的元素上右击鼠标然后 ...

  10. NIUDAY 11.23 北京站抢票啦 | 看 AI 落地行业 享 AI 时代红利

    2018 年是见证「奇迹」的一年.AI 从多年的热门话题中开始走下神坛,逐渐深入到了各个行业,加速经济结构优化及行业智慧化升级,AI 已不再是难以企及的神话而是可触摸的美好未来. 政策支持加上资本推动 ...