Cows
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 16163   Accepted: 5380

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.

Source

POJ Contest,Author:Mathematica@ZSU
用树状数组或线段树求解。对奶牛右端点进行降序排序,保证在对奶牛的左端点进行sum操作时,在1~左端点的区间内累计的奶牛都比它强壮。
树状数组解法
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,c[100100],ans[100100];
struct Cow{
int s,e,ind;
bool operator<(const Cow h)const{
if(e==h.e)return s<h.s;
return e>h.e;
}
}cow[100100];
inline int lowbit(int x)
{return x&(-x);}
inline void add(int x)
{
for(int i=x;i<=n;i+=lowbit(i))c[i]++;
}
inline int sum(int x)
{
int res=0;
for(int i=x;i>0;i-=lowbit(i))res+=c[i];
return res;
}
int main()
{
while(scanf("%d",&n)&&n){
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++){
scanf("%d%d",&cow[i].s,&cow[i].e);
cow[i].s++;
cow[i].e++;
cow[i].ind=i;
}
sort(cow+1,cow+1+n);
for(int i=1;i<=n;i++){
if(cow[i].s==cow[i-1].s&&cow[i].e==cow[i-1].e)ans[cow[i].ind]=ans[cow[i-1].ind];
else ans[cow[i].ind]=sum(cow[i].s);
add(cow[i].s);
}
printf("%d",ans[1]);
for(int i=2;i<=n;i++)printf(" %d",ans[i]);
puts("");
}
return 0;
}

poj 2481的更多相关文章

  1. 树状数组 POJ 2481 Cows

    题目传送门 #include <cstdio> #include <cstring> #include <algorithm> using namespace st ...

  2. POJ 2481 Cows (数组数组求逆序对)

    题目链接:http://poj.org/problem?id=2481 给你n个区间,让你求每个区间被真包含的区间个数有多少,注意是真包含,所以要是两个区间的x y都相同就算0.(类似poj3067, ...

  3. 【POJ 2481】 Cows

    [题目链接] http://poj.org/problem?id=2481 [算法] 树状数组 注意特判两头牛的s,e值相同 [代码] #include <algorithm> #incl ...

  4. POJ 2481 Cows (线段树)

    Cows 题目:http://poj.org/problem?id=2481 题意:有N头牛,每仅仅牛有一个值[S,E],假设对于牛i和牛j来说,它们的值满足以下的条件则证明牛i比牛j强壮:Si &l ...

  5. POJ 2481 Cows

    Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16546   Accepted: 5531 Description ...

  6. 2018.07.08 POJ 2481 Cows(线段树)

    Cows Time Limit: 3000MS Memory Limit: 65536K Description Farmer John's cows have discovered that the ...

  7. POJ 2481:Cows 树状数组

    Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 14906   Accepted: 4941 Description ...

  8. poj 2481 - Cows(树状数组)

    看的人家的思路,没有理解清楚,,, 结果一直改一直交,,wa了4次才交上,,, 注意: 为了使用树状数组,我们要按照e从大到小排序.但s要从小到大.(我开始的时候错在这里了) 代码如下: #inclu ...

  9. POJ 2481 Cows(树状数组)

                                                                      Cows Time Limit: 3000MS   Memory L ...

随机推荐

  1. react native 的js 文件从哪里获取

    /** * Loading JavaScript code - uncomment the one you want. * * OPTION 1 * Load from development ser ...

  2. 【Android】监听Notification被清除

    前言 一般非常驻的Notification是可以被用户清除的,如果能监听被清除的事件就可以做一些事情,比如推送重新计数的问题. 声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www ...

  3. JAVA NIO中的Channels和Buffers

    前言 Channels和Buffers是JAVA NIO里面比较重要的两个概念,NIO正是基于Channels和Buffers进行数据操作,且数据总是从Channels读取到Buffers,或者从Bu ...

  4. 烂泥:KVM、kickstart与nginx集成

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 前几篇文章介绍了FTP.NFS与KVM.kickstart集成的案例,从这篇文章开始,我们来介绍HTTP方式与KVM.kickstart集成. HTTP ...

  5. Linux磁盘管理之实现多文件系统及VFS06

    待续 Linux如何支持多文件系统 不同磁盘需要不同类型的磁盘驱动程序,驱动向上提供接口,不同驱动提供的接口格式不同,在上层是块设备层,用来屏蔽下边驱动接口的差别,向上统一提供,把所有硬盘当成块设备, ...

  6. Linux 开机自启动脚本详解

    以kibana为例     以下为skibana名称的脚本内容 #!/bin/bash #chkconfig: 2345 80 90 #description:kibana kibana=" ...

  7. ELK Nxlog->Kafka->ElasticSearch

    Windows 系统下,log4日志通过kafka发送到elasticsearch; windows 下nxlog没有找到直接发送数据到kafka的插件,所以采用logstash中转下     Nxl ...

  8. JavaScript获取元素CSS属性

    function getDefaultStyle(obj,attribute){ return obj.currentStyle?obj.currentStyle[attribute]:documen ...

  9. 作为程序员之 Vim(一)

    开始使用 Vim(一) vim被称为是编辑器之神,如果可以学好vim的话,就可以在键盘上 “健指如飞” 了,可以完全摆脱鼠标来进行文本的定位编辑. 当然,vim还可以进行各种配置,装上各种插件,做成 ...

  10. myeclipse优化

    myeclipse优化 http://blog.163.com/cayyenne@126/blog/static/12186261420100611546658/