题目来源:http://poj.org/problem?id=2481
问题描述
Cows
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 23004 | Accepted: 7753 |
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.
Source
POJ Contest,Author:Mathematica@ZSU
------------------------------------------------------------
题意
n头奶牛,每头奶牛对应一个区间,如果奶牛A的区间真包含于奶牛B的区间,则称奶牛B比奶牛A“强壮”。对于每头奶牛,求比它强壮的奶牛的个数。
------------------------------------------------------------
思路
之前同样是用了树状数组做的这道题,详见?。本次用线段树解。
线段树部分与上一题?基本相同。区别在于,因为本题没有说明输入的区间的大小顺序,所以需要读入所有区间,然后按区间尾端降序(第一顺序)并按区间首端升序(第二顺序),这样就只要考虑区间首端的大小比较即可:对于奶牛A,比它强壮的奶牛个数等于它之前区间首端小于等于它的个数。
特别值得注意的是,如果两头奶牛区间完全相同,则没有哪头比对方更强壮,所以要往前遍历看有没有区间完全相同的情况。
还有就是std::sort第三个参数中比较函数的写法:切记返回值一定是bool. 另外也可以写成类的形式并重载()运算符。如果是std::priority_queue中的第三项自定义比较项,则只能写成类的形式。
------------------------------------------------------------
代码
#include<cstdio>
#include<algorithm>
#include<cstring>
const int NMAX = (int) 1e5 + 5, XRANGE = (int) 1e5;
struct Cow {
int s, e; // 起始点和终止点
int ind; // 奶牛原来的编号
int stronger; // 比该奶牛更强壮的奶牛数量
}cow[NMAX];
bool cmpSE (const Cow & a, const Cow & b)// 按照终止点的降序(第一顺序)和起始点的升序(第二顺序)排序
{
if (a.e > b.e)
{
return 1;
}
else if (a.e == b.e)
{
if (a.s < b.s)
{
return 1;
}
}
return 0;
}
bool cmpIND(const Cow & a, const Cow & b) // 按ind升序排列
{
return a.ind < b.ind;
}
int tree[NMAX<<2]; // 基于虚拟数组cnt[0:NMAX]之上,cnt[i]表示x = i的奶牛的个数
void update(int root, int lp, int rp, int x) // 线段树单点修改函数
{
if (x < lp || x > rp)
{
return;
}
if (lp == rp)
{
tree[root] ++;
return;
}
int mid = (lp + rp)/2;
if (x <= mid)
{
update(2*root+1, lp, mid, x);
}
else
{
update(2*root+2, mid+1, rp, x);
}
tree[root]++;
}
int query(int root, int lp, int rp, int x) // 求cnt[0:x]的线段和
{
if (x < lp)
{
return 0;
}
else if (rp <= x)
{
return tree[root];
}
else
{
int mid = (lp + rp)/2;
if (x <= mid)
{
return query(2*root+1, lp, mid, x);
}
else
{
return query(2*root+1, lp, mid, x) + query(2*root+2, mid+1, rp, x);
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("2481.txt", "r", stdin);
#endif
int n, i, tmp, j;
while (scanf("%d", &n) != EOF)
{
if (n == 0)
{
break;
}
memset(tree, 0, sizeof(tree));
memset(cow, 0, sizeof(cow));
for (i=0; i<n; i++)
{
scanf("%d%d", &(cow[i].s), &(cow[i].e));
cow[i].ind = i;
}
std::sort(cow, cow + n, cmpSE);
for (i=0; i<n; i++)
{
update(0, 0, XRANGE, cow[i].s);
// 由于数组cow关于cow[i].e降序,所以只要统计比cow[i].s小的cow[j].s的个数即可
tmp = query(0, 0, XRANGE, cow[i].s) - 1; // 去掉cow[i]本身
j = i-1;
while (j >= 0 && cow[i].s == cow[j].s && cow[i].e == cow[j].e)
{
j--;
tmp--; // 与cow[i]区间相同的奶牛也不能算
}
cow[i].stronger = tmp;
}
std::sort(cow, cow+n, cmpIND);
for (i=0; i<n; i++)
{
printf("%d ", cow[i].stronger);
}
printf("\n");
}
return 0;
}