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数组,角标为编号 ...
随机推荐
- createjs 的 bitmapdata类
今天测试一个功能,在效率上出现了问题.2D舞台绘制了大量的元素,联想到AS3的 bitmapdata.darw() 功能,遗憾是createjs官方类 中没有bitmapdata类. 好在已经有大神替 ...
- 关于jQuery源码分析
http://www.w3ctech.com/topic/256 jQuery源码剖析(一)——概览&工具方法
- Beautiful 疑问小记
一.获取id和class的text() html = urlopen(real_url) bsObj = BeautifulSoup(html) h1 = bsObj.h1.get_text() co ...
- Pycharm使用问题# Interpreter设置
Pycharm可以迅速更换interpreter版本. 在菜单栏选择File-Settings打开Settings设置对话框,选择展开Interpreter选项: 使用列表右侧的+和—即可增加和删除I ...
- nginx+tomcat 配置负载均衡
nginx 从Nginx官网下载页面(http://nginx.org/en/download.html)下载Nginx最新版本(我用的是nginx-1.8.1版本) 安装就直接把压缩包解压到一个路径 ...
- [SoapUI] Post请求Body里面限制特殊字符(&、%),Groovy脚本里特殊字符需要添加“\”转义($)。
SoapUI的Post请求,在body里面不能包含(&.%),如果含有这些特殊字符,请求会报错:在添加的Groovy脚本里有些特殊字符需要使用“\”转义($),不然也会报错.
- Software Engineering: 1. Introduction
Resource: Ian, Sommerville, Software Engineering 1. Professional software development 1.1 Software e ...
- android产品业务逻辑对app稳定影响太大
产品经理们, 看看你们的交互文档, 有n个逻辑分支, 在我们的实现中至少存在2*n个逻辑分支 这样极度造成了app的不稳定性,表现就是 非必须的bug很多.还有就是维护性极差 当然你们会说,你们可以写 ...
- “不支持一个STA线程上针对多个句柄的WaitAll。”的解决方案
一.异常提示 不支持一个 STA 线程上针对多个句柄的 WaitAll. 出错界面如下图: 二.解决方法 先直接上解决方案吧.其实解决方法很简单如下面的代码直接把main函数的[STAThread]属 ...
- IT自学论坛
http://yun.baidu.com/share/home?uk=4113898546&view=share#category/type=0