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. EJS 高效的 JavaScript 模板引擎

    什么是 EJS? "E" 代表 "effective",即[高效].EJS 是一套简单的模板语言,帮你利用普通的 JavaScript 代码生成 HTML 页面 ...

  2. java、mysql、oracle、pgsql数据类型对应关系

    看不清 请 Ctrl+鼠标滚轮     放大页面

  3. 安装redis集群

    1.搭建集群需要使用到官方提供的ruby脚本. 需要安装ruby的环境. 安装ruby yum install ruby yum install rubygems 2.将ruby包redis-3.0. ...

  4. Day8 - C - Largest Rectangle in a Histogram HDU - 1506

    A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...

  5. 「NOIP2013」华容道

    传送门 Luogu 解题思路 预支一点东西: 这题其实有着更为思维的图模型,还十分考验码力,不简单啊 这居然是联赛题 讲正解: 显然我们对于一种合法方案,空格子肯定是一直围绕着特定棋子反复横跳的. 所 ...

  6. 071、Java面向对象之使用private封装属性

    01.代码如下: package TIANPAN; class Book { // 定义一个新的类 private String title; // 书的名字 private double price ...

  7. Adobe Illustrator CC 2017安装方法

    1.下载软件地址 2017破解版32位64位中文版下载[百度网盘资源地址]:https://pan.baidu.com/s/13BsU8CfsLB6OXr7SkRFzCg 注意:使用之前请关闭杀毒软件 ...

  8. input type="submit" 和"button"有什么区别

    HTML中<input type="submit" /> 和 <input type="button" /> 主要从元素定义类型.点击触 ...

  9. Day7 - D - The Euler function HDU - 2824

    The Euler function phi is an important kind of function in number theory, (n) represents the amount ...

  10. axios 如何取消已发送的请求?

    前言 最近在项目中遇到一个问题,在连续发送同一请求时,如果第二次请求比第一次请求快,那么实际显示的是第一次请求的数据,这就会造成数据和我选择的内容不一致的问题.解决的方案:在后续发送请求时,判断之前的 ...