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数组,角标为编号 ...
随机推荐
- 《BI项目笔记》基于雪花模型的维度设计
GBGradeCode 外键关系: 1 烟叶等级 T_GBGradeCode.I_DistinctionID=T_Distinction.I_DistinctionID 烟叶等级分为:上等烟.中等烟. ...
- Linux_导出函数
1.linux 下查看 .so 导出函数列表(http://blog.csdn.net/wangweixaut061/article/details/7164809) nm -D 7z.so objd ...
- RecyleView 简析
LayoutManager.Adapter是必须设置的(不设置将无法显示数据---不执行onCreateViewHolder()等方法) ItemDecoration\ItemAnimator是可选的 ...
- solr 6.1 服务端 tomcat 搭建及调用
一.下载 apache solr6.1.0 最新版本zip,解压缩生成一个solr6.1.0文件夹 二.安装 1.在d:/projects下新建一个solr 2.把solr6.1.0/server/s ...
- Error: listen EADDRINUSE
有可能是已经作用了18001端口 netstat -antp|grep 18001 kill 然后重启程序. events.js:72 throw er; // Unhandled 'error' e ...
- jquery的一些属性选择器
td:contains(Henry)选择td里面所有class为Henry的元素 .siblings()选择相同dom层次的所有其他元素,例如 <ul> <li>第一个< ...
- 如何在MapControl界面添加双击事件实现标绘及符号样式更改
private void axMapControl1_OnDoubleClick(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnDo ...
- 忘记了MariaDB root密码的解决办法
1.停掉mariaDB systemctl stop mariadb.service 2.KILL掉系统里的MySQL进程: ps -ef | grep mariadb #查询进程PIDkill 进程 ...
- MVC部署到iis
程序域功能->打开或关闭->iis信息服务及.net framework下的两个要勾选 1. 发布程序,以文件系统file system 的形式,发布到一个文件夹里 自定义-> ...
- PHP中多态,抽象类,接口,
小例子: 需求:公司定义一个接口让我们开发功能 usb.interface.php: <?php interface USB{ public function run(); } store.cl ...