Cows
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 14777 | Accepted: 4890 |
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 iis 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 iis 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
Hint
Huge input and output,scanf and printf is recommended.
问你对每头牛,有多少头牛比他强壮,i牛比j牛强壮条件是Si <= Sj && Ej <= Ei && Ei - Si > Ej - Sj,也就是i牛可以完全覆盖j牛。
排序时先按e从大到小再按s从小到大,若遇到重复的数据直接存到数组中。
AC代码(线段树):
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "cstdlib"
using namespace std;
const int MAXN = 1e5 + 5;
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
int n, sum[MAXN << 2], ans[MAXN];
struct node
{
/* data */
int s, e, flag;
}a[MAXN];
bool cmp(node a, node b)
{
if(a.e == b.e) return a.s < b.s;
return a.e > b.e;
}
void pushup(int rt)
{
sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void update(int rt, int l, int r, int x)
{
if(l == r) {
++sum[rt];
return;
}
int mid = (l + r) >> 1;
if(x <= mid) update(lson, x);
else update(rson, x);
pushup(rt);
}
int query(int rt, int l, int r, int L, int R)
{
if(L <= l && R >= r) return sum[rt];
int mid = (l + r) >> 1, res = 0;
if(L <= mid) res += query(lson, L, R);
if(R > mid) res += query(rson, L, R);
return res;
}
int main(int argc, char const *argv[])
{
while(scanf("%d", &n) != EOF && n) {
memset(sum, 0, sizeof(sum));
for(int i = 0; i < n; ++i) {
scanf("%d%d", &a[i].s, &a[i].e);
++a[i].s;
a[i].flag = i;
}
sort(a, a + n, cmp);
for(int i = 0; i < n; ++i) {
int x = a[i].s;
if(i && a[i].s == a[i - 1].s && a[i].e == a[i - 1].e) ans[a[i].flag] = ans[a[i - 1].flag];
else ans[a[i].flag] = query(1, 1, 1e5 + 1, 1, x);
update(1, 1, 1e5 + 1, x);
}
for(int i = 0; i < n - 1; ++i)
printf("%d ", ans[i]);
printf("%d\n", ans[n - 1]);
}
return 0;
}
AC代码(树状数组):
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int MAXN = 1e5 + 5;
int n, c[MAXN], ans[MAXN];
struct node
{
/* data */
int s, e, flag;
}a[MAXN];
bool cmp(node a, node b)
{
if(a.e == b.e) return a.s < b.s;
return a.e > b.e;
}
int lowbit(int x)
{
return x & (-x);
}
void update(int x, int v)
{
for(int i = x; i <= n; i += lowbit(i))
c[i] += v;
}
int get_sum(int x)
{
int sum = 0;
for(int i = x; i > 0; i -= lowbit(i))
sum += c[i];
return sum;
}
int main(int argc, char const *argv[])
{
while(scanf("%d", &n) != EOF && n) {
memset(c, 0, sizeof(c));
memset(ans, 0, sizeof(ans));
for(int i = 0; i < n; ++i) {
scanf("%d%d", &a[i].s, &a[i].e);
a[i].flag = i;
}
sort(a, a + n, cmp);
c[0] =1;
for(int i = 0; i < n; ++i) {
if(i && a[i].s == a[i - 1].s && a[i].e == a[i - 1].e) ans[a[i].flag] = ans[a[i - 1].flag];
else ans[a[i].flag] = get_sum(a[i].s + 1);
update(a[i].s + 1, 1);
}
for(int i = 0; i < n - 1; ++i)
printf("%d ", ans[i]);
printf("%d\n", ans[n - 1]);
}
return 0;
}