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

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
 
 
 
解析:树状数组(单点更新,区间查询)。把奶牛吃三叶草的范围进行排序(按照E从大到小排序,E相同则按S从小到大排序),本题的解法就与 POJ 2352 Stars 大致相同,但要注意本题与 POJ 2352 Stars 区别在于:不同奶牛吃三叶草的范围是可以重复的,而在 POJ 2352 Stars 点的坐标不会重复。
 
 
 
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lowbit(x) (x)&(-x)
using namespace std; struct Cow{
int s, e;
int id;
bool operator < (const Cow& b)const
{
if(e != b.e) return e>b.e;
return s<b.s;
}
}cow[100005]; int n;
int c[100005];
int ans[100005]; void add(int x, int val)
{
for(int i = x; i <= 100001; i += lowbit(i))
c[i] += val;
} int sum(int x)
{
int ret = 0;
for(int i = x; i > 0; i -= lowbit(i))
ret += c[i];
return ret;
} void solve()
{
memset(c, 0, sizeof(c));
memset(ans, 0, sizeof(ans));
sort(cow+1, cow+n+1);
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].id] = ans[cow[i-1].id];
else
ans[cow[i].id] = sum(cow[i].s);
add(cow[i].s, 1);
}
for(int i = 1; i < n; ++i)
printf("%d ", ans[i]);
printf("%d\n", ans[n]);
} int main()
{
while(scanf("%d", &n), n){
int s, e;
for(int i = 1; i <= n; ++i){
scanf("%d%d", &s, &e);
cow[i].s = s+1;
cow[i].e = e+1;
cow[i].id = i;
}
solve();
}
return 0;
}

POJ 2481 Cows的更多相关文章

  1. 树状数组 POJ 2481 Cows

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

  2. POJ 2481 Cows (线段树)

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

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

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

  4. 2018.07.08 POJ 2481 Cows(线段树)

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

  5. POJ 2481 Cows(树状数组)

                                                                      Cows Time Limit: 3000MS   Memory L ...

  6. poj 2481 Cows(树状数组)题解

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

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

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

  8. poj 2481 Cows(数状数组 或 线段树)

    题意:对于两个区间,[si,ei] 和 [sj,ej],若 si <= sj and ei >= ej and ei - si > ej - sj 则说明区间 [si,ei] 比 [ ...

  9. POJ 2481 Cows 【树状数组】

    <题目链接> 题目大意: 就是给出N个区间,问这个区间是多少个区间的真子集. 解题分析: 本题与stars类似,只要巧妙的将线段的起点和终点分别看成 二维坐标系中的x,y坐标,就会发现,其 ...

随机推荐

  1. 如何监控业务的响应速度?Cloud Insight SDK 实践分享

    一直在说 Cloud Insight 是数据聚合平台,可以用 SDK 和 API 实现业务监控,如今不拿出点实践人们恐怕是不能信服.那今天本文就先简单介绍一下 SDK 可以应用在哪些方面,再举个真实用 ...

  2. Lua 代码编写技巧

    1.克隆表 u = {unpack(table)} 一般克隆长度较小的表 2.判断表是否为空 if next(t) == nil then..  判断该表是否为空,包括t={}的情况 3.插入表 使用 ...

  3. POJ 1417 True Liars(种类并查集+dp背包问题)

    题目大意: 一共有p1+p2个人,分成两组,一组p1,一组p2.给出N个条件,格式如下: x y yes表示x和y分到同一组,即同是好人或者同是坏人. x y no表示x和y分到不同组,一个为好人,一 ...

  4. Java 获取amr音频格式的音频长度

    import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; public class GetAm ...

  5. http://www.oschina.net/translate/elasticsearch-getting-started?cmp

    http://www.oschina.net/translate/elasticsearch-getting-started?cmp

  6. 安装xampp后,出现“Apache 2 Test Page powered by CentOS“

    因为是在本地测试,所以没有去考虑为什么会这样,考虑太多的原因.只要能运行就行. 所以网络搜索了一番. 最后,解决办法是: 1, 找到apachectl. 那么就在命令行敲find / -name ap ...

  7. linux下安装Apache(https) 服务器证书安装配置指南

    一.  安装准备 1.    安装Openssl 要使Apache支持SSL,需要首先安装Openssl支持.推荐下载安装openssl-0.9.8k.tar.gz   下载Openssl:http: ...

  8. 8、SpringMVC源码分析(3):分析ModelAndView的形成过程

    首先,我们还是从DispatcherServlet.doDispatch(HttpServletRequest request, HttpServletResponse response) throw ...

  9. 嵌入式控制(0)----linux系统网络配置

    嵌入式系统本身具有操作系统的全部属性,但收到其硬件条件制约,故需要主机通过串口/网口等方式与其通信.今日下午的工作主要是linux系统的ssh传输配置,nfs服务器配置,tftp服务器配置. ip的概 ...

  10. Linux下jvm、tomcat、mysql、log4j优化配置

    小菜一直对操作系统心存畏惧,以前也很少接触,这次创业购买了Linux云主机,由于木有人帮忙,只能自己动手优化服务器了.... 小菜的云主机配置大致为:centeos6(32位),4核心cpu,4G内存 ...