利用树状数组求交点有多少

2014-02-08 13:36:37 · 作者: · 浏览: 120

  题意就是让你求交点有多少个。

  我们可以先按a从小到大排序,a相等就按b从小到大排序,这样题意就变成了让我们求b数组的逆序数了。

  代码如下:

  #include

  #include

  #include

  #include

  #include

  #define M 1005

  #define LL long long

  using namespace std;

  int n,m,k;

  int c[M];

  struct node

  {

  int x,y;

  bool operator < ( const node &h ) const

  {

  return x0)

  {

  sum+=c[x];

  x-=Lowbit(x);

  }

  return sum;

  }

  int main()

  {

  int t;

  scanf("%d",&t);

  for(int count=1;count<=t;count++)

  {

  memset(c,0,sizeof(c));

  scanf("%d%d%d",&n,&m,&k);

  for(int i=1;i<=k;i++)

  {

  scanf("%d%d",&p[i].x,&p[i].y);

  }

  sort(p+1,p+1+k);

  LL ans=0;

  for(int i=1;i<=k;i++)

  {

  Update(p[i].y,1);

  ans+=(i-Sum(p[i].y));

  }

  printf("Test case %d: %lld\n",count,ans);

  }

  return 0;

  }