首页 > 代码库 > POJ - 2481 Cows(树状数组)
POJ - 2481 Cows(树状数组)
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: cow i and cow j, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cow i is stronger than cow j.
For each cow, how many cows are stronger than her? Farmer John needs your help!
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: cow i and cow j, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cow i is stronger than cow j.
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 <= 10 5), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 10 5) 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.
For each test case, the first line is an integer N (1 <= N <= 10 5), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 10 5) 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 cow i.
Sample Input
3 1 2 0 3 3 4 0
Sample Output
1 0 0
思路:首先按照e值从大到小排序,然后为了保证ei-si>ej-sj,所以在排序函数那边注意一下,然后就是典型的树状数组的应用,每次插入的时候查找
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 100005; struct Cow { int s, e, num; bool operator <(const Cow &tmp) const { if (e != tmp.e) return e > tmp.e; return s < tmp.s; } } cow[maxn]; int c[maxn], total[maxn], n, Max; int lowbit(int x) { return x & (-x); } void Add(int x) { while (x <= Max) { c[x] += 1; x += lowbit(x); } } int sum(int x) { int ans = 0; while (x > 0) { ans += c[x]; x -= lowbit(x); } return ans; } int main() { while (scanf("%d", &n) != EOF && n) { memset(c, 0, sizeof(c)); memset(total, 0, sizeof(total)); Max = 0; for (int i = 1; i <= n; i++) { scanf("%d%d", &cow[i].s, &cow[i].e); cow[i].s++, cow[i].e++; cow[i].num = i; Max = max(Max, cow[i].s); } sort(cow+1, cow+1+n); for (int i = 1; i <= n; i++) { Add(cow[i].s); if (i > 1 && cow[i].s == cow[i-1].s && cow[i].e == cow[i-1].e) total[cow[i].num] = total[cow[i-1].num]; else total[cow[i].num] = sum(cow[i].s) - 1; } for (int i = 1; i < n; i++) printf("%d ", total[i]); printf("%d\n", total[n]); } return 0; }
POJ - 2481 Cows(树状数组)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。