Cows
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 17626   Accepted: 5940

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
【分析】给你n个区间,问你对于每个区间,有多少个区间是完全覆盖它的。完全覆盖的意思是若Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj,那么j就被i完全覆盖。
n<=1e5,所以暴力肯定超时,而树状数组正好可以用于快速的统计个数。首先得排个序,然后模板统计。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 2e9
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = 1e5+;
const int M = ;
int n,m;
ll tree[N],ans[N];
struct man{
int s,e,no;
bool operator< (const man &it)const{
if(e==it.e)return s<it.s;
return e>it.e;
}
}a[N];
void add(int k,int num){
while(k<=1e5+){
tree[k]+=num;
//printf("####%lld\n",tree[k]);
k+=k&(-k);
}
}
ll Sum(int k){
ll sum=;
while(k>){
sum+=tree[k];
k-=k&(-k);
}
return sum;
}
int main() {
while(~scanf("%d",&n)&&n){
met(tree,);met(ans,);
for(int i=;i<=n;i++){
scanf("%d%d",&a[i].s,&a[i].e);
a[i].s++;a[i].e++;a[i].no=i;
}
sort(a+,a++n);
for(int i=;i<=n;i++){
ll Ans;
if(a[i].s==a[i-].s&&a[i].e==a[i-].e)Ans=ans[a[i-].no];
else Ans=Sum(a[i].s);
//printf("%d\n",Ans);
ans[a[i].no]=Ans;
add(a[i].s,);
}
printf("%lld",ans[]);
for(int i=;i<=n;i++){
printf(" %lld",ans[i]);
}
printf("\n");
}
return ;
}

POJ 2481 Cows(树状数组)的更多相关文章

  1. poj 2481 - Cows(树状数组)

    看的人家的思路,没有理解清楚,,, 结果一直改一直交,,wa了4次才交上,,, 注意: 为了使用树状数组,我们要按照e从大到小排序.但s要从小到大.(我开始的时候错在这里了) 代码如下: #inclu ...

  2. Cows POJ - 2481 (树状数组 + 单点更新 + 区间查询)

    Cows 思路:我们可以按照每个范围的S从小到大排序,相同的S按E从大到小排序,这样的好处是当前范围的S一定大于等于之前范围的S(即当前的范围可能被之前范围的包围),那么我们只需要统计之前的范围E比当 ...

  3. POJ 2481:Cows 树状数组

    Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 14906   Accepted: 4941 Description ...

  4. POJ 2182 Lost Cows (树状数组 && 二分查找)

    题意:给出数n, 代表有多少头牛, 这些牛的编号为1~n, 再给出含有n-1个数的序列, 每个序列的数 ai 代表前面还有多少头比 ai 编号要小的牛, 叫你根据上述信息还原出原始的牛的编号序列 分析 ...

  5. poj2481 Cows 树状数组

    题目链接:http://poj.org/problem?id=2481 解题思路: 这道题对每组数据进行查询,是树状数组的应用.对于二维的树状数组, 首先想到排序.现在对输入的数据按右值从大到小排序, ...

  6. POJ2481:Cows(树状数组)

    Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ...

  7. poj 2229 Ultra-QuickSort(树状数组求逆序数)

    题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...

  8. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  9. poj 2155 Matrix (树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16797   Accepted: 6312 Descripti ...

  10. poj2182Lost Cows——树状数组快速查找

    题目:http://poj.org/problem?id=2182 从后往前确定,自己位置之前没有被确定的且比自己编号小的个数+1即为自己的编号: 利用树状数组快速查找,可另外开一个b数组,角标为编号 ...

随机推荐

  1. 安装openJDK 8

    1.JDK 8 示例 (1.1)Debian, Ubuntu等使用下述安装命令: $ -jre ps : openjdk-8-jre 仅包含JRE,如果需要开发java程序,需要下载openjdk-8 ...

  2. Flex 学习笔记 ComboBox内容框宽度

    如何设置ComboBox下拉选项框的宽度呢 左边下拉框发现字符太长了   属性里也找不到相关宽度可以设置,解决如下 <!--添加open事件 打开下拉选项框时设置--> <s:Com ...

  3. iOS - Apache Tomcat WebServer 服务器配置

    前言 提前下载好相关软件,且安装目录最好安装在全英文路径下.如果路径有中文名,那么可能会出现一些莫名其妙的问题. 提前准备好的软件: apache-tomcat-6.0.45.tar.gz eclip ...

  4. Mac 在命令行中获得Root权限

    Mac 在命令行中获得Root权限 作者 firedragonpzy 13 九月, 2012 2条评论 本文为firedragonpzy原创,转载务必在明显处注明:转载自[Softeware MyZo ...

  5. git drupal eclipse

    eclispe如何打补丁https://www.drupal.org/patch/apply打patch,初级详细教程https://www.drupal.org/node/620014

  6. Java完成最简单的WebService创建及使用(REST方式,Jersey框架)

    前言: 一直以来都对WebService感兴趣,但因为难以理解WebService到底是什么,所以了解甚少.周二的时候有个跟我关系比较好的同事想要自己写个WebService的小Demo,希望能够做成 ...

  7. WampServer Version 2.5 bug修改

    做PHP开发都需要安装PHP的运行环境,为了方便,网上可以下载到好多的集成环境,最近使用WampServer Version 2.5发现有一些bug,分享一下修改的方法.高手请路过. 1.echo d ...

  8. 博客打开慢?请禁用WordPress默认的谷歌字体!

    最近几天,谷歌中国挂了之后,发现我的博客打开极慢,原以为是空间问题,可一查,发现同台服务器的用户打开并不慢,排除了空间问题后,这边查询元素发现博客打开时加载了一个链接地址“fonts.googleap ...

  9. Linux下文件和目录的相关操作

    文件和目录的操作命令,按以下思路进行整理,感觉更便于记忆和使用 1.创建一个二进制文件 touch f1 2.向文件中写入数据 echo "hello" >> f1 e ...

  10. jsp与php混用的漏洞

    接手一个项目是jsp写的,用起来感觉开发是在太麻烦了.于是新功能就用php写的,jsp.php两者之间相互跳转, 突然一天发现在tomcat的web站下打开php竟然显示了php源码,在php站下看j ...