#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100000+100;
int m;
struct cow
{
int s;
int e;
int index;
};
cow cw[maxn];
int ans[maxn];
int arr[maxn];
bool cmp(cow x,cow y)
{
if (x.e==y.e)
return x.s < y.s;
return x.e > y.e;
}
int lowbit(int x)
{
return x&(-x);
}
int sum(int x)
{
int ret = 0;
while(x>0)
{
ret+=arr[x];
x-=lowbit(x);
}
return ret;
}
void add(int i,int v)
{
while(i < m)
{
arr[i] += v;
i += lowbit(i);
}
}
int main(int argc, char const *argv[])
{
while(scanf("%d", &m)!=EOF)
{
if (m == 0)
{
break;
}
memset(ans,0,sizeof(ans));
memset(cw,0,sizeof(cw));
memset(arr,0,sizeof(arr));
for (int i = 0; i < m; ++i)
{
cw[i].index = i;
scanf("%d%d",&cw[i].s,&cw[i].e);
cw[i].s++;
cw[i].e++;
}
sort(cw,cw+m,cmp);
// for (int i = 0; i < m; ++i)
// {
// printf("%d %d\n",cw[i].s,cw[i].e);
// }
for (int i = 0; i < m; ++i)
{
if (i && cw[i].s==cw[i-1].s && cw[i].e==cw[i-1].e)//线段区间重合
ans[cw[i].index]=ans[cw[i-1].index];
else
ans[cw[i].index] = sum(cw[i].s);
add(cw[i].s,1);
}
for (int i=0; i<m; i++)
{
printf("%d%c",ans[i],i==(m-1)?'\n':' ');
}
}
return 0;
}
数状数组,思路很好,记下。