POJ 2481 Cows(树状数组)
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 17626 | Accepted: 5940 |
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
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(树状数组)的更多相关文章
- poj 2481 - Cows(树状数组)
看的人家的思路,没有理解清楚,,, 结果一直改一直交,,wa了4次才交上,,, 注意: 为了使用树状数组,我们要按照e从大到小排序.但s要从小到大.(我开始的时候错在这里了) 代码如下: #inclu ...
- Cows POJ - 2481 (树状数组 + 单点更新 + 区间查询)
Cows 思路:我们可以按照每个范围的S从小到大排序,相同的S按E从大到小排序,这样的好处是当前范围的S一定大于等于之前范围的S(即当前的范围可能被之前范围的包围),那么我们只需要统计之前的范围E比当 ...
- POJ 2481:Cows 树状数组
Cows Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 14906 Accepted: 4941 Description ...
- POJ 2182 Lost Cows (树状数组 && 二分查找)
题意:给出数n, 代表有多少头牛, 这些牛的编号为1~n, 再给出含有n-1个数的序列, 每个序列的数 ai 代表前面还有多少头比 ai 编号要小的牛, 叫你根据上述信息还原出原始的牛的编号序列 分析 ...
- poj2481 Cows 树状数组
题目链接:http://poj.org/problem?id=2481 解题思路: 这道题对每组数据进行查询,是树状数组的应用.对于二维的树状数组, 首先想到排序.现在对输入的数据按右值从大到小排序, ...
- POJ2481:Cows(树状数组)
Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ...
- poj 2229 Ultra-QuickSort(树状数组求逆序数)
题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- poj 2155 Matrix (树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16797 Accepted: 6312 Descripti ...
- poj2182Lost Cows——树状数组快速查找
题目:http://poj.org/problem?id=2182 从后往前确定,自己位置之前没有被确定的且比自己编号小的个数+1即为自己的编号: 利用树状数组快速查找,可另外开一个b数组,角标为编号 ...
随机推荐
- os.environ()
---------2016-5-9 18:56:39-- source:OS.ENVIRON()详解
- unittest可能面临的问题以及解决方法
问题1:用例的执行顺序 当使用unittest.main()时,用例的执行是按照ascall值的顺序来执行的,所以如果使用main()方法来执行用例的话,那么就需要通过命名来限制执行顺序,比如想要先执 ...
- SPSS数据分析—多重线性回归
只有一个自变量和因变量的线性回归称为简单线性回归,但是实际上,这样单纯的关系在现实世界中几乎不存在,万事万物都是互相联系的,一个问题的产生必定多种因素共同作用的结果. 对于有多个自变量和一个因变量的线 ...
- mysql 配置 utf8 依然乱码
mysql 乱码问题排除方案: 1.检查数据库及数据表是不是utf8字符集 2.查看一下jdbc.properties配置的数据库url 是否配置了characterEncoding=UTF-8或者在 ...
- csu 1804 有向无环图
题目地址 分析:从复杂度来看,一定不可能是枚举和来计算.1e5的规模来看,应该是复杂度比较合适. 我是这么想的,对于三个点,假设1->2有a种走法,2->3有b种走法.那么1->3应 ...
- Java—接口与抽象类
一.接口 Java编程语言中禁止多继承属性,但可以通过接口来帮助类扩展方法.接口中可以定义大量的常量和方法,但其中的方法只是一种声明,没有具体的实现,使用接口的类自己实现这些方法. 接口与类的不同在于 ...
- 读javascript高级程序设计11-事件
一.事件流 事件流指从页面中接收事件的顺序. 1.事件冒泡(常用) IE中采用的事件流是事件冒泡,先从具体的接收元素,然后逐步向上传播到不具体的元素. 2.事件捕获(少用) Netscapte采用事件 ...
- linux 基本命令操作
1.ls 命令 ls -a 列出所有文件,包括隐藏文件 ls -l 列出文件详细信息 ls -r 列出所有文件包括文件夹 查询具体文件可以在命令后面加 |grep 要匹配的字符串,方便我们查找, ...
- SAP常用命令及BASIS操作
Pfcg 角色,权限参数文件配置Su53 查看权限对象 st01 跟踪St22 看dump,以分析错误 eg.找到ABAP程序出错的地方,找出fou ...
- 优化ubuntu桌面
---恢复内容开始--- 此博主写的很全 http://blog.csdn.net/terence1212/article/details/52270210 使用安装Unity Tweak Tool ...