本题的关键在于要考虑到两个区间完全相同的情况,其他的不用考虑太多。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
const int maxn = 100000 + 300;
int c[maxn];
int ans[maxn];
struct cow{
int s;
int e;
int in;
bool operator < (const cow& z) const{
if(z.s == s) return z.e < e;
else return z.s > s;
}
}co[maxn], pri;
int lowbit(int x){
return x & (-x);
}
int sum(int x){
int t = 0;
while(x > 0){
t += c[x];
x -= lowbit(x);
}
return t;
}
int update(int x){
while(x < maxn){
c[x]++;
x += lowbit(x);
}
return 0;
}
int main(){
int tot;
while(scanf("%d", &tot) && tot){
memset(c, 0, sizeof(c));
memset(co, 0, sizeof(co));
memset(ans, 0, sizeof(ans));
for(int i = 0; i < tot; ++i){
scanf("%d%d", &co[i].s, &co[i].e);
co[i].s++; co[i].e++;
co[i].in = i;
}
sort(co, co+tot);
pri.s = co[0].s; pri.e = co[0].e; pri.in = co[0].in;
co[0].e++;
ans[co[0].in] = 0 - sum(co[0].e);
update(co[0].e);
for(int i = 1; i < tot; ++i){
int s = co[i].e;
s++;
if(pri.e == co[i].e && pri.s == co[i].s){
ans[co[i].in] = ans[co[i-1].in];
}else{
ans[co[i].in] = i - sum(s-1);
}
pri.e = co[i].e; pri.s = co[i].s; pri.in = co[i].in;
update(s);
}
int i;
for(i = 0; i < tot - 1; ++i){
printf("%d ", ans[i]);
}
printf("%d\n", ans[i]);
}
return 0;
}