还是太弱,很明显的一道树状数组竟然很久想不出来。这道题如果想到如何排序了,就好做了。
按题目题意,我们要找强壮的,那么就先把最强壮的找出来,存起来,然后找第二强壮的,此时已经知道比他强壮的牛有多少头了,依次类推,最后所有牛的结果都能求出来,如果从最弱的开始找,显然是无法找出来的。
然后注意一下区间重合时一定要特判。每头牛都要插入树状数组中。
/*
ID: sdj22251
PROG: subset
LANG: C++
*/
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#define MAX 1000000009
#define LOCA
using namespace std;
int n, b[100001], ans[100001];
struct cow
{
int s, e, id;
}p[100005];
bool cmp(cow x, cow y)
{
if(x.s == y.s) return x.e > y.e;
else return x.s < y.s;
}
int lowbit(int x)
{
return x & -x;
}
void modify(int x)
{
for(int i = x; i > 0; i -= lowbit(i))
b[i]++;
}
int sum(int x)
{
int s = 0;
for(int i = x; i < 100001; i += lowbit(i))
s += b[i];
return s;
}
int main()
{
#ifdef LOCAL
freopen("subset.in","r",stdin);
freopen("subset.out","w",stdout);
#endif
int i;
while(scanf("%d", &n) != EOF)
{
if(n == 0) break;
memset(b, 0, sizeof(b));
for(i = 0; i < n; i++)
{
scanf("%d%d", &p[i].s, &p[i].e);
p[i].s++;
p[i].e++;
p[i].id = i;
}
sort(p, p + n, cmp);
for(i = 0; i < n; i++)
{
if(i > 0 && p[i].s == p[i - 1].s && p[i].e == p[i - 1].e)
ans[p[i].id] = ans[p[i - 1].id];
else
{
ans[p[i].id] = sum(p[i].e);
}
modify(p[i].e);
}
for(i = 0; i < n - 1; i++)
printf("%d ", ans[i]);
printf("%d\n", ans[n - 1]);
}
return 0;
}