树状数组的应用 也是来求逆序数的
AC代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
const int N=1000105;
typedef long long ll;
#define MM(x,y) memset(x,y,sizeof(x))
using namespace std;
int n,m,k;
struct Node
{
int x;
int y;
}node[N];
bool cmp(struct Node a,struct Node b)
{
if(a.x==b.x)
return a.y<b.y;
else
return a.x<b.x;
}
ll low[N];
void add(int k,int num)
{
while(k<=m)
{
low[k]+=num;
k+=k&-k;
}
}
ll sum(int k)
{
ll res=0;
while(k>0)
{
res+=low[k];
k-=k&-k;
}
return res;
}
int main()
{
int i,j,tcase;
scanf("%d",&tcase);
for(int p=1;p<=tcase;p++)
{
MM(low,0);
scanf("%d%d%d",&n,&m,&k);
for(i=0;i<k;i++)
{
scanf("%d%d",&node[i].x,&node[i].y);
}
sort(node,node+k,cmp);
ll res=0;
for(i=0;i<k;i++)
{
add(node[i].y,1);
res+=(sum(m)-sum(node[i].y));
}
printf("Test case %d: %lld\n",p,res);
}
return 0;
}