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. 转载:HBASE配置说明

    HBase 默认配置  ,原文:http://eclecl1314-163-com.iteye.com/blog/1474286 该文档是用hbase默认配置文件生成的,文件源是 hbase-defa ...

  2. iOS 开发之路(登陆页键盘遮挡输入框问题)一

    在学习开发登陆页的时候,遇到的问题分享如下: 首先是swift 3.0 中,NotificationCenter 设置 selector 如下: @IBOutlet weak var bottomCo ...

  3. 软工_个人项目反(shai)思(zhao)

    这次作业成绩还算不错,但是也收获了很多的经验与教训,在这里总结一下. 需要继续改进的地方 作业提交方面仍旧有些问题.如助教所说,在GitHub中并没有保存已经编译好的exe文件,导致助教在检查作业的时 ...

  4. Nexus Repository Manager 3.0 发布

    著名仓库管理工具Nexus,在2016年4月6日发布3.0版本(包括OSS版),相较2.*版本有很大的改变: 1. 从底层重构,从而提高性能,增强扩展能力,并改善用户体验 2. 升级界面,增加更多的浏 ...

  5. 用java的jdk 生成android 的jni接口文档

    1 检查系统是否安装了jdk,并将javac的路径配置到PATH中  cmd窗口,输入 java -version  查看输出项 2 创建需要so的接口类package com.ndk.test; p ...

  6. Linux 虚拟机网络适配器从E1000改为VMXNET3

    我们知道VMware的网络适配器类型有多种,例如E1000.VMXNET.VMXNET 2 (Enhanced).VMXNET3等,就性能而言,一般VMXNET3要优于E1000,下面介绍如果将Lin ...

  7. Javascript之旅——第六站:看看writable特性

    说起js中的那些特性标记,总觉得有些怪怪的,那为什么要说到这个attribute,起源于对一个问题的疑问,我们都知道window对象其实就是 浏览器窗口的一个实例,既然是一个实例,那这个实例就应该有“ ...

  8. 基于RMI服务传输大文件的完整解决方案

    基于RMI服务传输大文件,分为上传和下载两种操作,需要注意的技术点主要有三方面,第一,RMI服务中传输的数据必须是可序列化的.第二,在传输大文件的过程中应该有进度提醒机制,对于大文件传输来说,这点很重 ...

  9. 金士顿U盘,群联PS2251-60主控,量产还原教程

    还原成一个可移动磁盘教程,只是在"分区设置"中将 "模式=21" 改为 "模式=3" 即可. 1. 打开:"MPALL_F1_90 ...

  10. js和jquery页面初始化加载函数的方法及先后顺序

    运行下面代码.弹出A.B.C.D.E的顺序:A=B=C>D=E. jquery:等待页面加载完数据,以及页面部分元素(不包括图片.视频), js:是页面全部加载完成才执行初始化加载. <! ...