2018.07.08 POJ 2481 Cows(线段树)
Cows
Time Limit: 3000MS Memory Limit: 65536K
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
事实上,我感觉这道题出出来是考察我们的英文水平的。让我们用OI" role="presentation" style="position: relative;">OIOI的语法来翻译一下题目,有n" role="presentation" style="position: relative;">nn个区间,求出每个区间被包含的次数(重合不算)。显然先按照左端点排序,然后所有区间的左端点就是单调不减的了。这样的话答案就只与右端点有关了。这个直接区间查询单点插入就行了。
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#define lc (p<<1)
#define rc (p<<1|1)
#define mid (T[p].l+T[p].r>>1)
#define N 100005
using namespace std;
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
return ans;
}
int n,ans[N];
struct Node{int s,t,id;}q[N];
struct st{int l,r,sum;}T[N<<2];
inline void pushup(int p){T[p].sum=T[lc].sum+T[rc].sum;}
inline void build(int p,int l,int r){
T[p].l=l,T[p].r=r,T[p].sum=0;
if(l==r)return;
build(lc,l,mid);
build(rc,mid+1,r);
}
inline void update(int p,int k){
if(T[p].l==T[p].r){
++T[p].sum;
return;
}
if(k<=mid)update(lc,k);
else update(rc,k);
pushup(p);
}
inline int query(int p,int ql,int qr){
if(ql>T[p].r||T[p].l>qr)return 0;
if(ql<=T[p].l&&T[p].r<=qr)return T[p].sum;
if(qr<=mid)return query(lc,ql,qr);
if(ql>mid)return query(rc,ql,qr);
return query(lc,ql,mid)+query(rc,mid+1,qr);
}
inline bool cmp(Node a,Node b){return a.s==b.s?a.t>b.t:a.s<b.s;}
int main(){
while(scanf("%d",&n)&&n){
for(int i=1;i<=n;++i)q[i].s=read()+1,q[i].t=read()+1,q[i].id=i;
build(1,1,100000);
sort(q+1,q+n+1,cmp);
for(int i=1;i<=n;++i){
if(q[i].s==q[i-1].s&&q[i].t==q[i-1].t){
ans[q[i].id]=ans[q[i-1].id];
}
else ans[q[i].id]=query(1,q[i].t,100000);
update(1,q[i].t);
}
for(int i=1;i<=n;++i)printf("%d ",ans[i]);
puts("");
}
return 0;
}
2018.07.08 POJ 2481 Cows(线段树)的更多相关文章
- POJ 2481 Cows (线段树)
Cows 题目:http://poj.org/problem?id=2481 题意:有N头牛,每仅仅牛有一个值[S,E],假设对于牛i和牛j来说,它们的值满足以下的条件则证明牛i比牛j强壮:Si &l ...
- 2018.07.08 hdu5316 Magician(线段树)
Magician Problem Description Fantasy magicians usually gain their ability through one of three usual ...
- 2018.07.31cogs2964. 数列操作η(线段树)
传送门 线段树基本操作. 给出一个排列b,有一个初始值都为0的数组a,维护区间加1,区间统计区间∑(ai/bi)" role="presentation" style=& ...
- 2018.07.25 hdu5306Gorgeous Sequence(线段树)
传送门 线段树基本操作. 要求维护区间取min" role="presentation" style="position: relative;"> ...
- 2018.07.23[PA2015]Siano(线段树)
[PA2015]Siano 描述 Description 农夫Byteasar买了一片n亩的土地,他要在这上面种草. 他在每一亩土地上都种植了一种独一无二的草,其中,第i亩土地的草每天会长高a[i]厘 ...
- POJ 2481 Cows(树状数组)
Cows Time Limit: 3000MS Memory L ...
- POJ 2481 Cows 【树状数组】
<题目链接> 题目大意: 就是给出N个区间,问这个区间是多少个区间的真子集. 解题分析: 本题与stars类似,只要巧妙的将线段的起点和终点分别看成 二维坐标系中的x,y坐标,就会发现,其 ...
- poj 2481 Cows(树状数组)题解
Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ...
- 2018.07.03 POJ 3348 Cows(凸包)
Cows Time Limit: 2000MS Memory Limit: 65536K Description Your friend to the south is interested in b ...
随机推荐
- hibernate-注解及配置
联合主键: 一.将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并重写equals和hascode,再将该类注解为@Embeddable,最后在主类中(该类不 ...
- clamav杀毒软件的安装
Linux Unix.Trojan.Elknot (Linux.BackDoor.Gates.5)木马清理 此恶意软件结合了传统后门程序和DDoS攻击木马的功能 前两天性能测试服务器被种马,cpu一直 ...
- Oracle免客户端InstantClient安装使用
正常情况下,用PL/SQL等软件连接Oracle,需要安装Oracle客户端软件,一般安装oracle客户端差不多需要2G左右的硬盘空间,但如果我们仅仅是连接数据库进行查询和执行一些相应的语句而不进行 ...
- Haskell语言学习笔记(58)Bifoldable
Bifoldable class Bifoldable p where bifold :: Monoid m => p m m -> m bifold = bifoldMap id id ...
- REST Client
1. 为什么要使用REST Client 在实际企业开发过程中经常会有这样的需求: 1.我当前开发的这个系统是需要调用其他系统的接口,也就是我们需要频繁的测试接口,尝试不同的入参参数去查看返回结果, ...
- 安装 neo4j 在 .../bin 目录下使用 ./neo4j 没反应 和 从csv 导入数据到neo4j
可以使用 /bin/sh ./neo4j start 如果提示:./neo4j: 28: set: Illegal option -o pipefail 那么 ubuntu”set Illegal o ...
- PPT地图 - 动态显示省份扩散效果
找一张中国地图矢量图,这个要找半天,自己画难度有点大.图中每个省份都是单独一张图.从我的百度云盘下载 提取码: 61ha 选中一个省份图块并复制,填充复制图块的颜色,拖拽该图块覆盖住该省份原来位置. ...
- pyplot图像组件
pyplot图像组件 ax子对象的组件内容 Title 图表标题 plt.title() Axis 坐标范围,x轴,y轴 plt.axis() label 坐标轴标注 plt.xlabel() plt ...
- DBA 招聘
数据库管理员(资深) 眼控科技 10-19万 72小时反馈 上海 6小时前 大专及以上 2年以上经验 普通话 25-35岁 绩效奖金 带薪年假 午餐补助 定期体检 年底双薪 五险一金 职位描述: 工作 ...
- 新手C#SQL Server使用记录2018.08.10
主键(PrimaryKey):主键就是每个数据行(记录)的唯一标识,不会有重复值的列(字段)才能当做主键.一个表可以没有主键,但是这样会很难处理表,因此一般情况表都要设置主键. 主键有两张选用策略,分 ...