题目大意:
给定你一些区间,去判断每个区间是其他区间真子集的个数。
分析:
本题与Stars有点类似。首先对E从大到小排序,如果Ei = Ej 则让S从小到大排序,然后就是stars类型。就是求cow[i].S前比他小的个数。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 100010;
int c[MAXN];
struct Node
{
int S, E;
int id;
}cow[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 UFset(int pos, int data)
{
while(pos < MAXN)
{
c[pos] += data;
pos += lowbit(pos);
}
}
int Querry( int pos )
{
int sum = 0;
while(pos > 0)
{
sum += c[pos];
pos -= lowbit(pos);
}
return sum;
}
int main()
{
int n, i;
int cnt[MAXN];
while(scanf("%d", &n) && n)
{
for(i = 1; i <= n; ++i)
{
scanf("%d %d", &cow[i].S, &cow[i].E);
cow[i].id = i;
}
memset(c, 0, sizeof(c));
memset(cnt, 0, sizeof(cnt));
sort(cow+1, cow+1+n, cmp);
for(i = 1; i <= n; ++i)
{
if( (i-1) && (cow[i].E == cow[i-1].E) && (cow[i].S == cow[i-1].S))
cnt[cow[i].id] = cnt[cow[i-1].id];
else
cnt[cow[i].id] = Querry(cow[i].S+1);//由于S是从零开始的,而pos是大于0的,因此此处加1
UFset(cow[i].S+1, 1);
}
for(i = 1; i < n; ++i)
printf("%d ", cnt[i]);
printf("%d\n", cnt[n]);
}
return 0;
}