POJ 2481 Cows
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 16546 | Accepted: 5531 |
Description
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
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
Sample Input
3
1 2
0 3
3 4
0
Sample Output
1 0 0
Hint
Source
#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的更多相关文章
- 树状数组 POJ 2481 Cows
题目传送门 #include <cstdio> #include <cstring> #include <algorithm> using namespace st ...
- POJ 2481 Cows (线段树)
Cows 题目:http://poj.org/problem?id=2481 题意:有N头牛,每仅仅牛有一个值[S,E],假设对于牛i和牛j来说,它们的值满足以下的条件则证明牛i比牛j强壮:Si &l ...
- POJ 2481 Cows (数组数组求逆序对)
题目链接:http://poj.org/problem?id=2481 给你n个区间,让你求每个区间被真包含的区间个数有多少,注意是真包含,所以要是两个区间的x y都相同就算0.(类似poj3067, ...
- 2018.07.08 POJ 2481 Cows(线段树)
Cows Time Limit: 3000MS Memory Limit: 65536K Description Farmer John's cows have discovered that the ...
- POJ 2481 Cows(树状数组)
Cows Time Limit: 3000MS Memory L ...
- poj 2481 Cows(树状数组)题解
Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ...
- poj 2481 - Cows(树状数组)
看的人家的思路,没有理解清楚,,, 结果一直改一直交,,wa了4次才交上,,, 注意: 为了使用树状数组,我们要按照e从大到小排序.但s要从小到大.(我开始的时候错在这里了) 代码如下: #inclu ...
- poj 2481 Cows(数状数组 或 线段树)
题意:对于两个区间,[si,ei] 和 [sj,ej],若 si <= sj and ei >= ej and ei - si > ej - sj 则说明区间 [si,ei] 比 [ ...
- POJ 2481 Cows 【树状数组】
<题目链接> 题目大意: 就是给出N个区间,问这个区间是多少个区间的真子集. 解题分析: 本题与stars类似,只要巧妙的将线段的起点和终点分别看成 二维坐标系中的x,y坐标,就会发现,其 ...
随机推荐
- C#委托及事件
转载:http://www.cnblogs.com/warensoft/archive/2010/03/19/1689806.html C#委托及事件 在C#中,委托(delegate)是一种引用类型 ...
- android Notification定义与应用
首先要明白一个概念: Intent 与 PendingIntent 的区别: Intent:是意图,即告诉系统我要干什么,然后做Intent应该做的事,而intent是消息的内容 PendingInt ...
- hdu 1288 Hat's Tea
这个要慢慢理解…… ;}
- 01 - 编译链接第一个wxWidgets3.0例子
1. preprocessor #define __WXMSW__#define WXUSINGDLL 2. vc10中设置Include dir, lib dir, dll path VC++平台编 ...
- MySQL 卸载 --Mac
pkill mysql sudo rm /usr/local/mysql sudo rm -rf /usr/local/mysql* sudo rm -rf /Library/StartupItems ...
- cojs 西瓜 解题报告
首先我们要知道pick公式 设二维平面内任意多边形面积为S 设多边形内部整点数为a 设多边形边界的整点数为b 则满足S=a+b/2-1 变形得a=S-b/2+1 由期望的线性性质我们把问题转化为 1. ...
- eclipse中的工程中有叉叉
在eclipse中的工程中有叉叉,并且不是编译的错. 那么,让eclipse自己告诉你原因吧.菜单Window->Show View->Problems 然后就去解决相应的Problems ...
- platform_driver_register(),platform_device_register()区别
设备与驱动的两种绑定方式:在设备注册时进行绑定及在驱动注册时进行绑定. 以一个USB设备为例,有两种情形: (1)先插上USB设备并挂到总线中,然后在安装USB驱动程序过程中从总线上遍历各个设备,看驱 ...
- spring是怎样面向接口编程的?
1.只要分层,就涉及到接口来连接.各层之间只通过接口来向外暴露功能. 2.spring中service层调用dao层时候,service层声明接口变量,具体接口实现可以ioc注入
- Windows基于Apache的svn服务器配置
参照 http://bbs.iusesvn.com/thread-158-1-1.html文章,经过svn的洗刷,终于把它配置成功,现在把我所配置的方法,记录下来,以供其他有需要的朋友参考,需要改进的 ...