Topic link
Topic idea: There are n cities on the left coast and m cities on the right coast. There are k roads to be built, and there are a total of several intersections.
Idea: First, roads starting from the same point can never meet, and all roads to the same point can never meet. Only x1>x2&&y1<y2 or vice versa will meet, so sort by y from largest to smallest and then insert the nodes in order to query online.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int N=1e7+10;
int c[1010],n,m,k;
struct node
{
int x,y;
bool operator<(const node & a)const
{
return y>a.y;
}
}p[N];
inline int lowbit(int x){
return x&(-x);}
void add(int pos,int val)
{
while(pos<=n)
{
c[pos]+=val;
pos+=lowbit(pos);
}
}
ll query(int pos)
{
ll res=0;
while(pos>0)
{
res+=c[pos];
pos-=lowbit(pos);
}
return res;
}
int main()
{
int t,tt=0;
scanf("%d",&t);
while(t--)
{
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);
p[i].x++;
}
sort(p+1,p+k+1);
ll ans=0;
for(int i=1;i<=k;)
{
ans+=query(p[i].x-1);
int now=i;
while(p[now].y==p[now+1].y&&now<=k) ans+=query(p[now+1].x-1),now++;
add(p[i].x,1);
while(p[i].y==p[i+1].y&&i<=k) add(p[i+1].x,1),i++;
i++;
}
printf("Test case %d: %lld\n",++tt,ans);
}
return 0;
}