简单线段树。
#include <stdio.h> #define MAXN 100005
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 int cols[MAXN<<], leaf; void PushDown(int rt) {
if ( cols[rt] ) {
cols[rt<<|] += cols[rt];
cols[rt<<] += cols[rt];
cols[rt] = ;
}
} void build(int l, int r, int rt) {
int mid;
cols[rt] = ;
if (l == r)
return ;
mid = (l+r)>>;
build(lson);
build(rson);
} void update(int ll, int rr, int l, int r, int rt) {
int mid; if (ll<=l && rr>=r) {
cols[rt]++;
return ;
}
PushDown(rt);
mid = (l+r)>>;
if (ll <= mid)
update(ll, rr, lson);
if (rr > mid)
update(ll, rr, rson);
} void query(int l, int r, int rt) {
int mid;
if (l == r) {
if (leaf)
printf(" %d", cols[rt]);
else
printf("%d", cols[rt]);
++leaf;
return ;
}
PushDown(rt);
mid = (l+r)>>;
query(lson);
query(rson);
} int main() {
int n;
int i, x, y; while (scanf("%d",&n)!=EOF && n) {
build(, n, );
for (i=; i<n; ++i) {
scanf("%d %d", &x, &y);
update(x,y,,n,);
}
leaf = ;
query(, n, );
printf("\n");
} return ;
}