http://acm.hdu.edu.cn/showproblem.php?pid=1556
View Code
1 #include<cstdio>
2 using namespace std;
3
4 const int N=100100;
5 struct node
6 {
7 int l,r,c;
8 int m() {return (l+r)/2;}
9 }st[N*4];
10 int ans[N];
11 void build(int l,int r,int rt)
12 {
13 st[rt].l=l; st[rt].r=r; st[rt].c=0;
14 if (l==r) return;
15 int m=st[rt].m();
16 build(l,m,rt*2);
17 build(m+1,r,rt*2+1);
18 }
19 void pushdown(int rt)
20 {
21 int &c=st[rt].c;
22 if(c)
23 {
24 st[rt*2].c+=c;
25 st[rt*2+1].c+=c;
26 c=0;
27 }
28 }
29 void update(int a,int b,int rt)
30 {
31 int l=st[rt].l, r=st[rt].r;
32 if (a<=l && r<=b)
33 {
34 st[rt].c++;
35 return;
36 }
37 pushdown(rt);
38 int m=st[rt].m();
39 if (a<=m) update(a,b,rt*2);
40 if (b>m) update(a,b,rt*2+1);
41 }
42 void query(int rt)
43 {
44 int l=st[rt].l, r=st[rt].r;
45 if (l==r)
46 {
47 ans[l]=st[rt].c;
48 return;
49 }
50 pushdown(rt);
51 query(rt*2);
52 query(rt*2+1);
53 }
54 int main()
55 {
56 int n;
57 while (scanf("%d",&n),n)
58 {
59 build(1,n,1);
60 for (int i=1;i<=n;i++)
61 {
62 int a,b;
63 scanf("%d%d",&a,&b);
64 update(a,b,1);
65 }
66 query(1);
67 for (int i=1;i<n;i++) printf("%d ",ans[i]);
68 printf("%d\n",ans[n]);
69 }
70 return 0;
71 }