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. POJ2104 K-th number 函数式线段树

    很久没打代码了,不知道为什么,昨天考岭南文化之前突然开始思考起这个问题来,这个问题据说有很多种方法,划分树什么的,不过对于我现在这种水平还是用熟悉的线段树做比较好.这到题今年8月份的时候曾经做过,那个 ...

  2. POJ 1258 Agri-Net(最小生成树,模板题)

    用的是prim算法. 我用vector数组,每次求最小的dis时,不需要遍历所有的点,只需要遍历之前加入到vector数组中的点(即dis[v]!=INF的点).但其实时间也差不多,和遍历所有的点的方 ...

  3. 利用hadoop自带程序运行wordcount

    1.启动hadoop守护进程 bin/start-all.sh 2.在hadoop的bin目录下建立一个input文件夹 JIAS-MacBook-Pro:hadoop- jia$ mkdir inp ...

  4. 2013 ACM-ICPC长沙赛区全国邀请赛—Special equations

    ……但是没仔细看,直接跳过了 这题直接枚举就可以过了 ;}

  5. 深入浅出Java并发包—锁机制(一)

    前面我们看到了Lock和synchronized都能正常的保证数据的一致性(上文例子中执行的结果都是20000000),也看到了Lock的优势,那究竟他们是什么原理来保障的呢?今天我们就来探讨下Jav ...

  6. 解决在windows的eclipse上面运行WordCount程序出现的一系列问题详解

    一.简介 要在Windows下的 Eclipse上调试Hadoop2代码,所以我们在windows下的Eclipse配置hadoop-eclipse-plugin- 2.6.0.jar插件,并在运行H ...

  7. lintcode:Coins in a Line 硬币排成线

    题目 硬币排成线 有 n 个硬币排成一条线.两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止.拿到最后一枚硬币的人获胜. 请判定 第一个玩家 是输还是赢? 样例 n = 1, 返回  ...

  8. Arraysort

    import java.util.*;public class Arraysort{ public static void main(String[]args){ int[]a={100,34,88, ...

  9. 245. Shortest Word Distance III

    题目: This is a follow up of Shortest Word Distance. The only difference is now word1 could be the sam ...

  10. ServletContentLIstener接口演示ServletContext的启动和初始化

    ServletContextListener接口中包含两个方法,一个是contextInitialized()方法, 用来监听ServletContext的启动和初始化:一个是contextDestr ...