poj 2481
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 16163 | Accepted: 5380 |
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<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的更多相关文章
- 树状数组 POJ 2481 Cows
题目传送门 #include <cstdio> #include <cstring> #include <algorithm> using namespace st ...
- POJ 2481 Cows (数组数组求逆序对)
题目链接:http://poj.org/problem?id=2481 给你n个区间,让你求每个区间被真包含的区间个数有多少,注意是真包含,所以要是两个区间的x y都相同就算0.(类似poj3067, ...
- 【POJ 2481】 Cows
[题目链接] http://poj.org/problem?id=2481 [算法] 树状数组 注意特判两头牛的s,e值相同 [代码] #include <algorithm> #incl ...
- POJ 2481 Cows (线段树)
Cows 题目:http://poj.org/problem?id=2481 题意:有N头牛,每仅仅牛有一个值[S,E],假设对于牛i和牛j来说,它们的值满足以下的条件则证明牛i比牛j强壮:Si &l ...
- POJ 2481 Cows
Cows Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16546 Accepted: 5531 Description ...
- 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 Limit: 65536K Total Submissions: 14906 Accepted: 4941 Description ...
- poj 2481 - Cows(树状数组)
看的人家的思路,没有理解清楚,,, 结果一直改一直交,,wa了4次才交上,,, 注意: 为了使用树状数组,我们要按照e从大到小排序.但s要从小到大.(我开始的时候错在这里了) 代码如下: #inclu ...
- POJ 2481 Cows(树状数组)
Cows Time Limit: 3000MS Memory L ...
随机推荐
- 编写更加稳定/可读的javascript代码
每个人都有自己的编程风格,也无可避免的要去感受别人的编程风格--修改别人的代码."修改别人的代码"对于我们来说的一件很痛苦的事情.因为有些代码并不是那么容易阅读.可维护的,让另一个 ...
- Autodesk 2013开发者日(DevDays)又要来了 -- 北京(2013年11月7日)和上海(2013年11月11日)
各位, 一年一度的Autodesk 开发者日(Devdays)开始注册了,抓紧时间前排占座! 注册地址: www.autodesk.com.cn/adndevday2013 今年开发者日的主题:革命性 ...
- Android Content Provider基础
Android Content Provider基础 Content Providers Content providers管理对一个结构化的数据集合的访问.它们封装了数据,并且提供了保护数据安全性的 ...
- 转 #ifndef/#define/#endif使用详解
想必很多人都看过“头文件中的 #ifndef/#define/#endif 防止该头文件被重复引用”.但是是否能理解“被重复引用”是什么意思?是不能在不同的两个文件中使用include来包含这个头文件 ...
- Base64与Bitmap转换
Base64与Bitmap互转 /** * 将base64转为bitmap * * @param string * @return */ public Bitmap stringtoBitmap(St ...
- Mac下修改Hosts文件工具——Gas Mask
这段时间在做公司APP的项目,看到公司开发IOS的同事和我这边联调程序时,经常需要手动修改hosts文件,比较麻烦. 在公司忙,没有来及找,给同事推荐了我当时知道的一个切换hosts文件的工具:sma ...
- js document.createElement()的用法 (转)
document.createElement()的用法 分析代码时,发现自己的盲点--document.createElement(),冲浪一番,总结了点经验. document.createElem ...
- Java并发之CountDownLatch 多功能同步工具类
package com.thread.test.thread; import java.util.Random; import java.util.concurrent.*; /** * CountD ...
- Mysql数据库上修改日期-->造数据
这次要给客户安装测试ineedle设备,但是安装后不会立刻有数据显示,不能够全面的展示给用户web界面的一些信息.此时需要有一个公网服务器能够展示一下ineedle统计数据,但是公司58设备上没有流量 ...
- 六、Android学习第五天——Handler的使用(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 六.Android学习第五天——Handler的使用 注意:有很多功能是不 ...