题目链接
题目大意:左海岸n个城市右海岸m个城市,有k条道路要建造,问一共有几个交点。
思路:首先从同一个点出发的道路永远都不可能相遇,到同一个点的所有道路也不可能相遇。只有x1>x2&&y1<y2或者反过来才会相遇,所以按y从大到小排序然后依次插入结点在线查询就好了。
#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;
}