D - 演离合相遇悲喜为谁
二维平面上N个点之间共有C(n,2)条连线。求这C(n,2)条线中斜率小于0的线的数量。
二维平面上的一个点,根据对应的X Y坐标可以表示为(X,Y)。例如:(2,3) (3,4) (1,5) (4,6),其中(1,5)同(2,3)(3,4)的连线斜率 < 0,因此斜率小于0的连线数量为2。
Input
第1行:1个数N,N为点的数量(0 <= N <= 50000)
第2 - N + 1行:N个点的坐标,坐标为整数。(0 <= Xii, Yii <= 10^9)Output
输出斜率小于0的连线的数量。(2,3) (2,4)以及(2,3) (3,3)这2种情况不统计在内。
Sample Input
4 2 3 3 4 1 5 4 6Sample Output
2
和A - 并肩行过山与水 POJ - 2352 Stars 树状数组很像,只是这题需要自己排序。PS:注意计算时y不能相等
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <malloc.h>
#define Twhile() int T;scanf("%d",&T);while(T--)
#define clc(a,b,n) for(int i=0;i<=n;i++)a[i]=b
#define clc2(a,b,n,m) for(int i=0;i<=n;i++)for(int j=0;j<=m;j++)a[i][j]=b
#define fora(i,a,b) for(int i=a;i<b;i++)
#define fors(i,a,b) for(int i=a;i>b;i--)
#define fora2(i,a,b) for(int i=a;i<=b;i++)
#define fors2(i,a,b) for(int i=a;i>=b;i--)
#define PI acos(-1.0)
#define eps 1e-6
#define INF 0x3f3f3f3f
#define BASE 131
#define lowbit(x) x&(-x)
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
using namespace std;
const int maxn=50000+11;
LL tre[maxn];
stack<int>sta;
struct node
{
int x,y;
}a[maxn];
int xx[maxn];
bool cmp(node a,node b)
{
if(a.y!=b.y)return a.y<b.y;
return a.x>b.x;
}
void update(int i,int val,int n)//n是树的大小
{
for(;i<=n;i+=lowbit(i))
tre[i]+=val;
}
LL sum(int i)
{
LL ret=0;
for(;i>0;i-=lowbit(i))
ret+=tre[i];
return ret;
}
int main()
{
int N,T;
while(~scanf("%d",&N))
{
fora2(i,1,N){scanf("%d%d",&a[i].x,&a[i].y);xx[i]=a[i].x;}
sort(a+1,a+1+N,cmp);
sort(xx+1,xx+1+N);
memset(tre,0,sizeof(tre));
LL ans=0;
fora2(i,1,N)
{
while(!sta.empty())sta.pop();
int t=a[i].y;
int ti=i;
while(a[i].y==t&&i<=N)
{
int k=lower_bound(xx+1,xx+1+N,a[i].x)-xx;
LL tem=sum(k);
tem=ti-tem-1;//小于a[i].y的情况下,大于a[i].x的x有多少
ans+=tem;
sta.push(k);
i++;
}
while(!sta.empty())
{
int k=sta.top();
sta.pop();
update(k,1,maxn-1);
}
i--;
//printf("%d %d %d %d %d\n",a[i].x,a[i].y,k,tem,ans);
}
printf("%lld\n",ans);
}
return 0;
}