您的位置:首页 > 编程语言 > Go语言

POJ3067--Japan(树状数组)

2014-01-17 16:11 253 查看
题意就是让你求交点有多少个。

我们可以先按a从小到大排序,a相等就按b从小到大排序。

代码如下:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#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 x<h.x || (x==h.x && y<h.y);
}
};
struct node p[M*M];

int Lowbit(int x)
{
return x&(-x);
}

void Update(int x,int d)
{
while(x<=M)
{
c[x]+=d;
x+=Lowbit(x);
}
}

int Sum(int x)
{
int sum=0;
while(x>0)
{
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  algorithm