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. [SharePoint] SharePoint 错误集 1

    1. Delete a site collection · Run command : Remove-SPSite –Identity http://ent132.sharepoint.hp.com/ ...

  2. Hosting socket.io WebSocket apps in IIS using iisnode

    In this post I explain how to configure a socket.io node.js application to use of WebSockets when ho ...

  3. 判断scrollview是否滚动到了底部

    调用函数判断如下:if(getScrollY() + getHeight() >=  computeVerticalScrollRange()){        Log.d(TAG," ...

  4. 详细对比IB开发与纯手码开发的优劣。

    1.IB是什么? Interface Builder 是一种通过图形化界面搭建UI的方式,并把窗口.菜单栏以及窗口上的各种控件的对象都“冻结”在了一个 NIB文档里:程序运行时,这些对象将会“苏醒”. ...

  5. Android-Using DDMS

    原文:http://developer.android.com/tools/debugging/ddms.html#how-ddms-works Android提供了一个debug工具叫做:DDMS, ...

  6. 利用Dreamweaver配置PHP服务器的站点

    配置的步骤: 1.打开Dreamweaver的站点------->新建站点-------->点击保存 2.点击服务器------>保存 3.配置完成之后就可以看到在Dreamweav ...

  7. 操作系统开发系列—12.f.在内核中添加中断处理 ●

    因为CPU只有一个,同一时刻要么是客户进程在运行,要么是操作系统在运行,如果实现进程,需要一种控制权转换机制,这种机制便是中断. 要做的工作有两项:设置8259A和建立IDT. /*========= ...

  8. 操作系统开发系列—10.内核HelloWorld ●

    a.我们先来体验一下在Linux下用汇编编程的感觉,见代码 [section .data] ; 数据在此 strHello db "Hello, world!", 0Ah STRL ...

  9. android Gui系统之SurfaceFlinger(5)---Vsync(2)

    9.Vsync第二部分 在上一篇中我们讲到,视图的刷新需要很多步骤, void SurfaceFlinger::handleMessageRefresh() { ATRACE_CALL(); preC ...

  10. 中控考勤仪IFace302多线程操作时无法订阅事件

    场景: 在各办事点安装中控考勤仪Iface302,各办事点的工作人员上下班报到时使用指纹或面纹进行自动登记,验证成功后将与服务吕进行通讯记录相关的考勤信息. 条件限制: 由于Iface302设备不支持 ...