Cows
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 15386 | Accepted: 5128 |
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!
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.
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.
题意:给定N个子区间,问包含第i个区间的区间有多少个。(只允许区间一个端点重合)
num[i].s记录左端点,num[i].t记录右端点
思路:考虑固定一个端点。假设固定s,那么按s升序排列后,我们就保证了num[i].s <= num[i+1].s,这时发现只有当num[i].t > num[i+1].t时第i区间才包含第i+1区间,那么我们再按t降序排下就好了。
这样i+1和i区间的可能情况 (因为区间重合的情况可以直接判定,就不列出了)
一、num[i].s == num[i+1].s, num[i+1].t < num[i].t
二、num[i].s < num[i+1].s, num[i+1].t <= num[i].t
三、num[i].s < num[i+1].s, num[i+1].t > num[i].t
显然,只有>=num[i+1].t的,才是合法区间。这样统计>=右端点的区间数再映射右端点就可以了。
此题还有一种问法,问第i个区间的(真)子区间有多少个?
解决方案:s降序再t升序,统计<=右端点的区间数,再映射右端点就好了。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (100000+10)
#define MAXM (200000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
#define first fi
#define second se
using namespace std;
struct Node{
int s, t, id;
};
Node num[MAXN];
bool cmp(Node a, Node b){
if(a.s != b.s)
return a.s < b.s;
else
return a.t > b.t;
}
int N = 100001;
int C1[MAXN], C2[MAXN];
int lowbit(int x){
return x & (-x);
}
void Update(int *C, int x)
{
while(x <= N)
{
C[x] += 1;
x += lowbit(x);
}
}
int sum(int *C, int x)
{
int s = 0;
while(x > 0)
{
s += C[x];
x -= lowbit(x);
}
return s;
}
int ans[MAXN];
int main()
{
int n;
while(Ri(n), n)
{
CLR(C1, 0); CLR(C2, 0);
for(int i = 0; i < n; i++)
{
Ri(num[i].s), Ri(num[i].t);
//num[i].s++; num[i].t++;
num[i].id = i;
}
sort(num, num+n, cmp);
for(int i = 0; i < n; i++)
{
if(i && num[i].s == num[i-1].s && num[i].t == num[i-1].t)
ans[num[i].id] = ans[num[i-1].id];
else
ans[num[i].id] = sum(C1, N) - sum(C1, num[i].t-1);
Update(C1, num[i].t);
}
for(int i = 0; i < n; i++)
{
if(i) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
}
return 0;
}