神题。同学指教。1秒AC。。。http://blog.csdn.net/jtjy568805874/article/details/50724656

#include<cstdio>
#include<cstring>
#include<ctime>
#include<algorithm>
using namespace std; const int maxn = + ;
struct point
{
double x;
double y;
int id;
}p[ * maxn];
struct Line
{
int a;
int b;
}line[maxn];
int T, n, tot;
int flag[maxn]; bool cmp(const point&a, const point&b)
{
if (a.x == b.x) return a.y < b.y;
return a.x < b.x;
} const double eps = 1e-;
#define zero(x)(((x)>0?(x):(-x))<eps) double xmult(point p1, point p2, point p0)
{
return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y);
} int dots_inline(point p1, point p2, point p3)
{
return zero(xmult(p1, p2, p3));
} int same_side(point p1, point p2, point l1, point l2)
{
return xmult(l1, p1, l2)*xmult(l1, p2, l2)>eps;
} int dot_online_in(point p, point l1, point l2)
{
return zero(xmult(p, l1, l2)) && (l1.x - p.x)*(l2.x - p.x)<eps && (l1.y - p.y)*(l2.y - p.y)<eps;
} int intersect_in(point u1, point u2, point v1, point v2)
{
if (!dots_inline(u1, u2, v1) || !dots_inline(u1, u2, v2)) return !same_side(u1, u2, v1, v2) && !same_side(v1, v2, u1, u2);
return dot_online_in(u1, v1, v2) || dot_online_in(u2, v1, v2) || dot_online_in(v1, u1, u2) || dot_online_in(v2, u1, u2);
} int main()
{
scanf("%d", &T);
while (T--)
{
long long ans = ;
scanf("%d", &n); tot = ; memset(flag, , sizeof flag);
for (int i = ; i <= n; i++)
{
scanf("%lf%lf", &p[tot].x, &p[tot].y); p[tot].id = i; tot++;
scanf("%lf%lf", &p[tot].x, &p[tot].y); p[tot].id = i; tot++;
}
sort(p, p + tot, cmp);
for (int i = ; i < tot; i++)
{
if (!flag[p[i].id])
{
flag[p[i].id] = ;
line[p[i].id].a = i;
}
else
{
line[p[i].id].b = i;
p[i].id = -p[i].id;
}
} for (int i = ; i < tot; i++)
{
if (p[i].id>)
{
int j;
for (j = i + ; p[j].id != -p[i].id; j++)
{
if (p[j].id > )
{
if (intersect_in(p[line[p[i].id].a], p[line[p[i].id].b], p[line[p[j].id].a], p[line[p[j].id].b])) ans++;
}
} for (;; j++)
{
if (j+<tot&&p[j].x == p[j + ].x&&p[j].y == p[j + ].y)
{
if (p[j+].id>)
if (intersect_in(p[line[p[i].id].a], p[line[p[i].id].b], p[line[p[j+].id].a], p[line[p[j+].id].b])) ans++;
}
else break;
}
} }
printf("%lld\n", ans);
}
return ;
}

HUST 1376 Random intersection的更多相关文章

  1. [Linked List]Intersection of Two Linked Lists

    Total Accepted: 53721 Total Submissions: 180705 Difficulty: Easy Write a program to find the node at ...

  2. codeforces 478B Random Teams

    codeforces   478B  Random Teams  解题报告 题目链接:cm.hust.edu.cn/vjudge/contest/view.action?cid=88890#probl ...

  3. 【2019.6.2】python:json操作、函数、集合、random()等

    一.json操作: json就是一个字符串,从文件中读取json,必须是json格式.j'son串中必须是双引号,不能有单引号,单引号不能转换 1.1使用: import json #使用json先引 ...

  4. Chrome V8引擎系列随笔 (1):Math.Random()函数概览

    先让大家来看一幅图,这幅图是V8引擎4.7版本和4.9版本Math.Random()函数的值的分布图,我可以这么理解 .从下图中,也许你会认为这是个二维码?其实这幅图告诉我们一个道理,第二张图的点的分 ...

  5. Math.random()

    Math.random() 日期时间函数(需要用变量调用):var b = new Date(); //获取当前时间b.getTime() //获取时间戳b.getFullYear() //获取年份b ...

  6. .Net使用system.Security.Cryptography.RNGCryptoServiceProvider类与System.Random类生成随机数

    .Net中我们通常使用Random类生成随机数,在一些场景下,我却发现Random生成的随机数并不可靠,在下面的例子中我们通过循环随机生成10个随机数: ; i < ; i++) { Rando ...

  7. 随机数(random)

    需求 Random rd=new Random(); 需要十以内的随机数  (0---10) System.out.println((int)((rd.nextDouble()*100)/10)); ...

  8. python写红包的原理流程包含random,lambda其中的使用和见简单介绍

    Python写红包的原理流程 首先来说说要用到的知识点,第一个要说的是扩展包random,random模块一般用来生成一个随机数 今天要用到ramdom中unifrom的方法用于生成一个指定范围的随机 ...

  9. [LeetCode] Random Pick Index 随机拾取序列

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

随机推荐

  1. Centos 6.4下使用VSFTPD无法正常连接与无法上传文件的问题解决

    发表于 2014 年 4 月 13 日 作者 SCKA 最近利用Linux搭建服务器 搭建FTP的时候决定使用VSFTP搭建,结果却出现了无法正常连接与无法上传文件等诸多问题 经过许久的努力,终于让V ...

  2. 。net MVC 序列化 反序列化

           序列化 (Serialization)将对象的状态信息转换为可以存储或传输的形式的过程.在序列化期间,对象将其当前状态写入到临时或持久性存储区.以后,可以通过从存储区中读取或反序列化对象 ...

  3. Number Sequence HDU 1711 KMP 模板

    题目大意:两个数组匹配,求子串首次出现的位置. 题目思路:数组长度,比较大,朴素算法的时间复杂度为 m*n超时.KMP的时间复杂度为m+n可行. #include<iostream> #i ...

  4. linux的学习系列 6---打印文件和发送邮件

    文件打印 如果你希望打印文本文件,最好预先处理一下,包括调整边距.设置行高.设置标题等,这样打印出来的文件更加美观,易于阅读.当然,不处理也可以打印,但是可能会比较丑陋. 大部分的Linux自带了 n ...

  5. IDL 计算TVDI

    介绍请看:http://blog.sina.com.cn/s/blog_764b1e9d0100wdrr.html 源码: IDL 源码PRO TVDI,NDVI,LST,NBINS,RES RES ...

  6. linux shell: 取得某个目录下的文件名列表

    取得某个目录下文件名的列表(没有子目录) ls -l dir/ | awk '{print "dir\\" $9}' #其中$9是ls -l 的第9个字段-文件名

  7. StreamReader 读取文本文件乱码问题

    解决读取文本文件乱码问题.我采取的是读取前先判断文本文件格式. StreamReader sr = new StreamReader(fullfileName, GetFileEncodeType(f ...

  8. iOS 一招搞定去掉字符串开始的0,尤其是针对时间格式化

    // 2.利用整型自动去掉开头的0, 不要循环和判断prefix以及截取字符串- (NSString *)pp_formatDateWithArrYMDToMD;{    NSInteger mont ...

  9. 转 shell脚本学习指南

    shell脚本学习指南 以下八点不敢说就能成为你shell脚本学习指南de全部,至少可以让你编写出可靠的shell脚本. 1. 指定bashshell 脚本的第一行,#!之后应该是什么?如果拿这个问题 ...

  10. 转:创建WebTest插件

    •Web测试插件为隔离Web测试中各个主声明语句外部的代码提供了一种手段.自定义的Web测试插件为在运行Web测试时调用某些代码提供了途径.在每个测试迭代中,Web测试插件都要运行一次. •通过从We ...