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

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.

题意是给出了一堆牛的能力值,这些能力值是用一定区间表示的。一头牛比另一头牛强壮就是这头牛的能力值区间真包含了另一头牛的能力值区间(能力值区间相等不能算作比其强壮)。问对于每一头牛来说,在这个群体中有多少头牛比自己强壮。

之前做过树状数组的几道题,长时间不做,好多地方又都不熟了,或者说之前可能没有完全理解,这次也算是好好理解。这个题和之前POJ上问两条路相交的题目差不多,思想都是控制变量,用排序的方式将一个变量控制住,用剩下来的一个变量可以用树状数组来控制。

首先因为要求的是所有牛中比自己强壮的有多少个。所以我希望每一个数据插入树状数组时,都能够得到比自己强壮的牛的数量。那就要求在数组中比自己靠前位置的,这时候发现树状数组能记录比自己靠前位置的,所以用树状数组记录的是s,而不是e。因为要比自己强壮嘛。

s用树状数组搞定了,那所以这时候自然会想到,此时我希望插入树状数组时,前面的e都比我当前的e大,这样我插入的时候就完全不用担心e值是否被包含进去了,所以这时用sort来解决就好啦~

这个题目,觉得排序的想法比树状数组都还要重要,或者说做树状数组的题,好多都是用树状数组控制一个变量,用其他方法(排序神马的)解决剩下的变量。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int n;
int ans[100005];
int cnt[100005]; struct no
{
int s,e;
int pos;
}node[100005]; bool cmp(no n1,no n2)
{
if(n1.e == n2.e)
{
return n1.s<n2.s;
}
else
{
return n1.e>n2.e;
}
} int lowbit(int x)
{
return x&(-x);
} void add(int x)
{
while(x<=n)
{
ans[x]++;
x=x+lowbit(x);
}
} int sum(int x)
{
int res=0;
while(x>0)
{
res+=ans[x];
x=x-lowbit(x);
}
return res;
} int main()
{
int i;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
for(i=1;i<=n;i++)
{
scanf("%d%d",&node[i].s,&node[i].e);
node[i].pos=i;
}
memset(ans,0,sizeof(ans));
memset(cnt,0,sizeof(cnt));
sort(node+1,node+n+1,cmp); for(i=1;i<=n;i++)
{
if(node[i].e == node[i-1].e && node[i].s == node[i-1].s)
{
cnt[node[i].pos] = cnt[node[i-1].pos];
}
else
{
cnt[node[i].pos] = sum(node[i].s+1);
}
add(node[i].s+1);
} for(i=1;i<=n;i++)
{
if(i==1)
{
printf("%d",cnt[1]);
}
else
{
printf(" %d",cnt[i]);
}
}
printf("\n");
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

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 2182 Lost Cows (树状数组 && 二分查找)

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

  4. poj2481 Cows 树状数组

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

  5. POJ2481:Cows(树状数组)

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

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

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

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

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

  8. poj 2155 Matrix (树状数组)

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

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

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

随机推荐

  1. Oracle查看正在执行的存储过程

    正在执行的存储过程 select owner,name from v$db_object_cache where type like '%PROCE%' and locks >0 and pin ...

  2. Hadoop的伪分布式安装和部署的流程

    1.准备工作 下载一些用到的命令 yum install -y vim yum install -y lrzsz yum install net-tools 目录约定 /opt #工作目录 /opt/ ...

  3. ch8 高度相等的列--CSS方法

    如下图所示效果,可以使用表格实现,本文采用在CSS中实现. 标记如下: <div class="wrapper"> <div class="box&qu ...

  4. SpringBoot初试牛刀

    新建 Spring Boot 项目常用的两种方式 你可以通过 https://start.spring.io/ 这个网站来生成一个 Spring Boot 的项目. 你可以选择自己喜欢的依赖进行加载到 ...

  5. 嵊州普及Day5T2

    题意:将(w,h)的纸条折成(W,H),最少需几步. 思路:横竖互不干扰,然后最多可折int型一半,拿个函数判断两次比较即可,然后折不了的条件是需要的矩形大于给的矩形. 见代码: #include&l ...

  6. HDU 5045 状压DP 上海网赛

    比赛的时候想的是把n个n个的题目进行状压 但这样不能讲究顺序,当时精神面貌也不好,真是挫死了 其实此题的另一个角度就是一个n个数的排列,如果我对n个人进行状压,外面套一个按题目循序渐进的大循环,那么, ...

  7. NOIP2017tg【逛公园】 题解

    先说点别的 emmm--,这是本蒟蒻的第一篇题解,大佬们勿喷QwQ(要不是看到写题解可以加贡献,我才--) 可以看到标签,是2017年提高的题目,好像是Day1T3,感觉提高考这样的题目挺好的,至少考 ...

  8. keras人工神经网络构建入门

    //2019.07.29-301.Keras 是提供一些高度可用神经网络框架的 Python API ,能帮助你快速的构建和训练自己的深度学习模型,它的后端是 TensorFlow 或者 Theano ...

  9. SQLSERVER|CDC 日志变更捕获机制

    先说一下什么是cdc ,cdc 变更数据捕获(Change Data Capture ,简称 CDC)记录 SQL Server 表的插入.更新和删除活动.SQLServer的操作会写日志,这也是CD ...

  10. CSS样式表——样式2

    样式 5)边界边框 margin:0px;                                            //外边距为0 margin:10px 0px 0px 10px;   ...