题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556
3 1 1 2 2 3 3 3 1 1 1 2 1 3 0
1 1 1 3 2 1
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cstdio>
#include <cmath>
const int N=1e5+100;
using namespace std;
int c[N],n;
int lowbit(int x)
{
  return x&(-x);
}
void update(int x,int d)
{
  while(x>0)
  {
   c[x]+=d;
   x-=lowbit(x);
  }
}
int getsum(int x)
{
  int ans=0;
  while(x<=n)
  {
   ans+=c[x];
   x+=lowbit(x);
  }
  return ans;
}
int main()
{
    int a,b;
    while(scanf("%d",&n)!=EOF)
    {
      if(n==0)break;
      memset(c,0,sizeof(c));
      for(int i=0;i<n;i++)
      {
        scanf("%d%d",&a,&b);
        update(b,1);
        update(a-1,-1);
      }
      for(int i=1;i<=n;i++)
      {
        if(i==1)printf("%d",getsum(i));
        else printf(" %d",getsum(i));
      }
      printf("\n");
    }
    return 0;
}
原文地址:http://blog.csdn.net/liusuangeng/article/details/39342737