pku 3067 Japan(变相逆序数)
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define MAXN 1010
#define MAXK 500010
struct HNode
{
int x,y;
}highway[MAXK];
inline bool mycmp(const HNode &h1,const HNode &h2)
{return h1.x<h2.x;}
int c[MAXN];
inline int lowbit(int x)
{
return x&(-x);
}
void update(int x,int delta)
{
while(x<MAXN)
{
c[x]+=delta;
x+=lowbit(x);
}
}
int getSum(int x)
{
int s=0;
while(x>0)
{
s+=c[x];
x-=lowbit(x);
}
return s;
}
int main()
{
int T,cas,N,M,K,i;
__int64 ans;
scanf("%d",&T);
highway[0].x=-1;
for(cas=1;cas<=T;cas++)
{
scanf("%d %d %d",&N,&M,&K);
for(i=1;i<=K;i++) scanf("%d %d",&highway[i].x,&highway[i].y);
sort(highway+1,highway+K+1,mycmp);
memset(c,0,sizeof(c));
ans=0;
for(i=1;i<=K;i++)
{
update(highway[i].y,1);
ans+=(i-getSum(highway[i].y));
if(highway[i].x == highway[i-1].x)
{
int j=i,t=0;
while(j>=1 && highway[j].x==highway[j-1].x)
{
j--;
if(highway[i].y < highway[j].y) t++;
}
ans -= t;
}
}
printf("Test case %d: %I64d\n",cas,ans);
}
return 0;
}
浙公网安备 33010602011771号