Codeforces Round #203 - D. Looking for Owls

D. Looking for Owls

Emperor Palpatine loves owls very much. The emperor has some blueprints with the new Death Star, the blueprints contain n distinct segments and m distinct circles. We will consider the segments indexed from 1 to n in some way and the circles — indexed from 1 to m in some way.

Palpatine defines an owl as a set of a pair of distinct circles (i, j) (i < j) and one segment k, such that:

  1. circles i and j are symmetrical relatively to the straight line containing segment k;
  2. circles i and j don't have any common points;
  3. circles i and j have the same radius;
  4. segment k intersects the segment that connects the centers of circles i and j.

Help Palpatine, count the number of distinct owls on the picture.

Input

The first line contains two integers — n and m (1 ≤ n ≤ 3·105, 2 ≤ m ≤ 1500).

The next n lines contain four integers each, x1, y1, x2, y2 — the coordinates of the two endpoints of the segment. It's guaranteed that each segment has positive length.

The next m lines contain three integers each, xi, yi, ri — the coordinates of the center and the radius of the i-th circle. All coordinates are integers of at most 104 in their absolute value. The radius is a positive integer of at most 104.

It is guaranteed that all segments and all circles are dictinct.

Output

Print a single number — the answer to the problem.

Please, do not use the %lld specifier to output 64-bit integers is С++. It is preferred to use the cout stream or the %I64d specifier.

Sample test(s)
Input
1 2
3 2 3 -2
0 0 2
6 0 2
Output
1
Input
3 2
0 0 0 1
0 -1 0 1
0 -1 0 0
2 0 1
-2 0 1
Output
3
Input
1 2
-1 0 1 0
-100 0 1
100 0 1
Output
0
Note

Here's an owl from the first sample. The owl is sitting and waiting for you to count it.

Codeforces Round #203 - D. Looking for Owls

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA1MAAAA0CAIAAADgw/W0AAATTUlEQVR4nO2d61Nb95nH+Qf2RZsXSexMsk3rieOMJ9st2bbOJpshWU/s3LypnTbd2W27dBvGu946M7vpNXbsRDYxke3YSWxjC8xFgATogiSEuAhJR0ISuoCQULiDuBoQAgyYiy20LwTSOUISRzeE8PczvxfmcM7ze4Q8cz7z/G5ptim9bUq/tENxOBzJTgEAAABIFA6Hw/seR/M1vPqD4lgnDeYHAAAApCgwP5gfTQLNzwEAAAAAAHY6MD8AAA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019FC1xbYv/DwAAHirC7ZpHLtRFXhoMPKMuWARah394AxHkI3knCBbp3LZwh+MFEkfzU35bw6krKK3JN4+pbc5EaZ9tSm+Z0BJdMpGOKzVWGoeV1kn/Mo62SZ1hSCFpLhfry9U9tb66Y1LM784dV16R5jf/U/HW72qO/Ln7KGPkXcboEcbYO5+NHWGM/cu5saPnx478pffN31b/+r/Krt1q7O0bi0u/CeJhN79tC2WBMNZDAABAhNgFIQpkFH8iF/Oiihxc8eyCbBaLtZldrmVCSsgbLcR5xBMEK0wVMY7mt2NaHF/9brfb5Zo1mbsLiomzF+s+ZMg/+ET529PEB2eIk+eUpy825uYr9YYul+vuatCVvdsJmB8AAICdRriB0diO0AiMTJnntx6WetMEwQouf75M1lxyXe0o5peEeX47puHVHxSYHwAAgJ2FXRB+EJcsVJE5YNjIft8LlMsQ9Uf/bXZBdrZAsF4+DFHzC987zA/mRxOYHwAAgJ1DZCs7Iqn/bR45wOSohcGw5rcmo2v3hDK/sLMHYX4wP5rA/AAAAOwQNiv2rd1EnTpHXqwRdk1IkN9RpgmS7yH/O6QyBvROnnyY1LW9O6bh1R8UmB8AAICdQeDeeiFEjnIbVdxCeVXIyEGn+W14JsIZhyHn+YWzWpgfzI8mMD8AAACA3j4s2xiYH8yPJjA/AAAAINXFD+YH86MLzA8AAABIeWB+MD+awPwAAACAlAfmB/OjCcwPAAAASHlgfjA/msD8AAAAgJQH5gfzownMDwAAAEh5YH4wP5rA/AAAAICUB+YH86NJoPk5AAAAAADATmfN/JZ2KA6HI9kpAAAAAInCgZpfsJpfsr+W7QjMDwAAAEh5YH4wP5r4zO//AcK1cZIryYRMAAAAAElFTkSuQmCC" alt="" />


题意:有n条线段和m个圆(1 ≤ n ≤ 3·105, 2 ≤ m ≤ 1500),如果存在两个圆和这两个圆的对称线段(并且线段要和圆心的连线相交),那么一起称为一个owl,求一共有多少个owl。

思路:最暴力的做法就是枚举每一对圆,求它们的对称线,然后枚举每一条线段,判断线段是否在对称线上,复杂度o(m2n),但是数据规模太大无法承受,这题不是一道纯粹的几何题。

后来我想,如果在暴力的基础上改进一下,先预处理出每对圆的对称线,在枚举每一条线段的时候,如果能在o(1)或o(logn)内判断出这条对称线是否存在,应该能过。

于是我想到了将一条直线hash成一个long long值,这种hash还是第一次写,我将这条直线对应的向量通过求gcd将x,y都缩到最小,并且保证x非负,然后在这条直线上取一点使得x非负并且尽量小,将这向量和这点的坐标值随便乘一些很大的数再加起来就算hash了,这样就可以保证同一条直线不管已知哪两点,算出的hash值都唯一。

现在问题来了,怎么判断线段和圆心连线相交呢?如果再找出这两个圆心的话,那跟暴力无异。于是我又想到了将一个对称线的hash值和对称线和圆心的交点的x坐标弄成一个pair插入到一个vector中,全部完了后对vector排序,hash值小的在前,相等的话x坐标小的在前面,然后枚举每一条线段,将两端点和对应的向量也都hash一下然后弄成pair,对应用lower_bound和upper_bound二分查找,在这个区间内的pair肯定都是符合的,ans+=区间长度就可以了。

于是时间复杂度降到了o(m2+nlogm2)。


 #include <iostream>
#include <stdio.h>
#include <map>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define TR(x) (x+10000)*2
typedef long long ll; struct Point
{
int x, y;
Point(int x=, int y=):x(x),y(y) { }
};
typedef Point Vector; Vector operator - (const Point& A, const Point& B)
{
return Vector(A.x-B.x, A.y-B.y);
} struct Circle
{
int x,y,r;
}; struct Line
{
Point begin;
Point end;
}; int gcd(int a,int b)
{
return b== ? a : gcd(b, a%b);
} Vector simpleVector(Vector v)
{
if(v.x==)
return Vector(,);
if(v.y==)
return Vector(,);
int g=gcd(abs(v.x), abs(v.y));
if(v.x>)
return Vector(v.x/g, v.y/g);
return Vector(-v.x/g, -v.y/g);
} Point simpleCenter(Point p, Vector v) // v.x>=0 && p.x>=0 && p.y>=0
{
if(v.x==)
return Point(p.x,);
if(v.y==)
return Point(,p.y);
Point r;
r.x = p.x % v.x;
int k = (p.x-r.x)/v.x;
r.y=p.y-k*v.y;
return r;
} ll hash(Point p, Vector v)
{
ll ans= (ll)p.x*80001LL
+(ll)p.y*80001LL*
+(ll)v.x*80001LL**
+(ll)v.y*80001LL***;
return ans;
} typedef pair<ll, int> Pair;
vector<Pair> a; Circle cs[];
Line ls[];
int main()
{
// 坐标+10000,再放大2倍,坐标范围[0,40000],且都是偶数
freopen("in.txt","r",stdin);
int nLine, nCircle;
cin>> nLine>> nCircle;
for(int i=; i<nLine; i++)
{
int x1,y1,x2,y2;
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
ls[i]=Line {Point(TR(x1),TR(y1)),Point(TR(x2),TR(y2))};
} for(int i=; i<nCircle; i++)
{
int x,y,r;
scanf("%d %d %d", &x, &y, &r);
cs[i]=Circle {TR(x),TR(y),r*};
} for(int i=; i<nCircle; i++)
for(int j=i+; j<nCircle; j++)
if(cs[i].r == cs[j].r)
{
Vector c1c2=Vector(cs[j].x-cs[i].x, cs[j].y-cs[i].y);
// 判断两个圆是否相交
if(c1c2.x*c1c2.x+c1c2.y*c1c2.y <= *cs[i].r*cs[i].r)
continue;
Vector v=Vector(-cs[j].y+cs[i].y, cs[j].x-cs[i].x); // 垂直向量
v=simpleVector(v); // v.x>=0 Point center=Point((cs[j].x+cs[i].x)/, (cs[j].y+cs[i].y)/);
center = simpleCenter(center, v); a.push_back(Pair(hash(center, v), (cs[j].x+cs[i].x)/));
} sort(a.begin(), a.end()); int ans=;
for(int i=; i<nLine; i++)
{
Vector v=simpleVector(ls[i].begin-ls[i].end);
Point p=simpleCenter(ls[i].begin,v);
ll h = hash(p,v); // 在vector内二分查找在区间[L,R]内的的个数
Pair L = Pair(h, min(ls[i].begin.x, ls[i].end.x));
Pair R = Pair(h, max(ls[i].begin.x, ls[i].end.x));
vector<Pair>::iterator i1 = lower_bound(a.begin(), a.end(), L);
vector<Pair>::iterator i2 = upper_bound(a.begin(), a.end(), R);
ans+=i2-i1;
} cout<< ans; return ;
}
上一篇:Angular企业级开发-AngularJS1.x学习路径


下一篇:CentOS6.5 安装 jdk1.7