Angle Beats

\[Time Limit: 4000 ms \quad Memory Limit: 1048576 kB
\]

题意

给出 \(n\) 个初始点以及 \(q\) 次询问,每次询问给出一个询问点 \(Q\),求包括 \(Q\) 点的直角三角形有多少个。保证 \(n+q\) 个点都不重复。

思路

  1. 对于每次询问,当 \(Q\) 为直角点时,以 \(Q\) 为原点,对 \(n\) 个点做象限极角排序,然后用双指针 \(L\)、 \(R\) 维护直角三角形的个数。 \(L\) 指针用来枚举其中的一条直角边, \(R\) 指针用来寻找在另一条直角边上的点有多少个,每次找 \(QL\) 这条边逆时针方向的另一条边\(QR\)。所以当 \(L\) 往逆时针转动时,\(R\) 也会往逆时针转动,那么就可以用双指针直接维护出来了,特别注意一下多个点在同一条直线上的情况就可以了。
  2. 若 \(Q\) 不是直角点时,可以离线处理,把 \(n+q\) 个点全部存出来,然后枚举以 \(n\) 个初始点为直角点时,对哪些的 \(Q\) 点有贡献,维护方法同上。

最后的复杂度为 \(O\left(qnC_1 + n(n+q)C_2\right)\),\(C_1、C_2\) 取决于在枚举直角点为原点后,到原点在同一条直线上的点数量。

我试过把 \(n+q\) 个节点全部提取出来,然后暴力枚举每个点为直角点的情况,但是这样复杂度会 \(T\)。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e4+10; struct Point {
ll x, y;
int id;
} p[maxn], be[maxn];
int n, m;
int ans[maxn]; int cmp1(Point a, Point b) {
ll d = a.x*b.y - b.x*a.y;
if(d == 0) {
return a.x<b.x;
} else {
return d>0;
}
}
int Qua(Point a) {
if(a.x>0 && a.y>=0) return 1;
if(a.x<=0 && a.y>0) return 2;
if(a.x<0 && a.y<=0) return 3;
if(a.x>=0 && a.y<0) return 4;
} int cmp(Point a, Point b) {
if(Qua(a) == Qua(b)) return cmp1(a, b);
else return Qua(a)<Qua(b);
} ll check(Point a, Point b) {
return a.x*b.x + a.y*b.y;
} ll chaji(Point a, Point b) {
return a.x*b.y - b.x*a.y;
} ll work(Point pp) {
for(int i=1; i<=n; i++) {
p[i] = be[i];
p[i].x -= pp.x;
p[i].y -= pp.y;
}
p[0] = pp;
sort(p+1, p+1+n, cmp);
for(int j=1; j<=n; j++) {
p[j+n] = p[j];
}
ll ans = 0;
int R = 2;
for(int L=1; L<=n; L++) {
while(R<=2*n) {
if(chaji(p[L], p[R]) < 0) break;
if(check(p[L], p[R]) <= 0) break;
R++;
}
int tR = R;
while(tR<=2*n) {
if(chaji(p[L], p[tR]) <= 0) break;
if(check(p[L], p[tR]) != 0) break;
ans++;
tR++;
}
}
return ans;
} int main(){
// freopen("in", "r", stdin);
while(~scanf("%d%d", &n, &m)) {
int all = 0;
for(int i=1; i<=n; i++) {
all++;
int x, y;
scanf("%d%d", &x, &y);
p[all].x = x, p[all].y = y, p[all].id = 0;
be[all] = p[all];
}
for(int i=1; i<=m; i++) {
all++;
int x, y;
scanf("%d%d", &x, &y);
p[all].x = x, p[all].y = y, p[all].id = i;
be[all] = p[all];
ans[i] = work(p[all]);
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=all; j++) {
p[j] = be[j];
}
p[0] = be[i];
int flag = 0;
for(int j=1; j<=all; j++) {
if(p[j].x == p[0].x && p[j].y == p[0].y) flag = 1;
if(flag) p[j] = p[j+1];
p[j].x -= p[0].x;
p[j].y -= p[0].y;
} int nn = all-1;
sort(p+1, p+1+nn, cmp);
for(int j=1; j<=nn; j++) {
p[j+nn] = p[j];
}
int R = 2;
for(int L=1; L<=nn; L++) {
int id = 0;
if(p[0].id) id = p[0].id;
if(p[L].id) id = p[L].id;
while(R<=2*nn) {
if(chaji(p[L], p[R]) < 0) break;
if(check(p[L], p[R]) <= 0) break;
R++;
}
int tR = R;
while(tR<=2*nn) {
if(chaji(p[L], p[tR]) <= 0) break;
if(check(p[L], p[tR]) != 0) break;
if(id == 0) {
if(p[tR].id) ans[p[tR].id]++;
} else {
if(p[tR].id == 0) ans[id]++;
}
tR++;
}
}
}
for(int i=1; i<=m; i++) {
printf("%d\n", ans[i]);
}
}
return 0;
}

Angle Beats Gym - 102361A(计算几何)的更多相关文章

  1. Angle Beats Gym - 102361A

    题目链接:https://vjudge.net/problem/Gym-102361A 题意:给定N个点,q次询问每次询问给一个点,问在N个点中取2个和给定点最多可以组成几个直角三角形. 思路:htt ...

  2. Gym102361A Angle Beats(直角三角形 计算几何)题解

    题意: \(n\)个点,\(q\)个询问,每次问包含询问点的直角三角形有几个 思路: 代码: #include<bits/stdc++.h> using namespace std; co ...

  3. Codeforces Gym 102361A Angle Beats CCPC2019秦皇岛A题 题解

    题目链接:https://codeforces.com/gym/102361/problem/A 题意:给定二维平面上的\(n\)个点,\(q\)次询问,每次加入一个点,询问平面上有几个包含该点的直角 ...

  4. hdu6731 Angle Beats(ccpc秦皇岛A,计算几何)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6731 题意: 给出$n$个点,有$q$次询问 每次询问给出一个点$b$,求这$n+1$个点,组成直角 ...

  5. Gym 101055A 计算几何,暴力

    http://codeforces.com/gym/101055/problem/A 题目:给定一些三维空间的点,要你找一个平面,能覆盖尽量多的点,只要求输出点数即可.n<=50 因为数据量小, ...

  6. Rasheda And The Zeriba Gym - 100283A  计算几何

    http://codeforces.com/gym/100283/problem/A 考虑到多边形是不稳定的,是可以变来变去的. 那么总是可以把每个点放到圆上. 所以只需要判断圆心角是不是小于等于36 ...

  7. 【HDOJ6731】Angle Beats(极角排序)

    题意:二维平面上给定n个整点,q个询问 每个询问给定另外的一个整点,问其能与n个整点中任意取2个组成的直角三角形的个数 保证所有点位置不同 n<=2e3,q<=2e3,abs(x[i],y ...

  8. Gym - 101845D 计算几何

    Ignacio is a famous mathematician, some time ago he was married with Dolly, a famous scientific, at ...

  9. CCPC 2019 秦皇岛 Angle Beats

    题目 给出P个点,然后给出Q个询问,问从P中选出两个点和给的点能组成直角三角形的方法个数.-O2,时间限制5秒. \[2\leqslant P\leqslant 2000,\qquad 1\leqsl ...

随机推荐

  1. 第17课 lambda表达式

    一. lambda表达式 (一)语法定义:[capture](paramters) mutable ->returnType{statement} 1.[capture]:捕获列表 (1)lam ...

  2. 初识Go语言--(2)Hello World

    新建hello.go文件,内容如下 package main import "fmt" func main() { //输出Hello World fmt.Println(&quo ...

  3. mysql插入数据频繁出现坏表

    测试环境mysql出现了一个怪表:select查询表卡死,alter修改表卡死,甚至我不想要这个表了,delete.truncate.drop表都卡死卡主了...... 解决办法: >show ...

  4. THUPC2019/CTS2019/APIO2019自闭记

    自闭了,自闭选手不配拥有游记.

  5. Redis(二)特性和学习路线

    Redis是高效的内存数据库或者说缓存.对多种数据结构都支持,每种数据结构都有相应的应用场景. 特性 Redis支持非常多的特性,从用途.性能.高可用.数据安全方面都提供了相应的支持和保障. Redi ...

  6. Jaeger接入Python应用:jaeger-client-python【非完全教程】

    目录 Jaeger接入Python应用 1. 安装 jaeger-client 2. (示例)创建 Tracer 对象,并通过 Tracer 对象创建 Span 来追踪业务流程. 3. 初始化和配置 ...

  7. docker安装mysql8

    docker run --restart=always -d -v /opt/data/conf.d/:/etc/mysql/conf.d/ -v /opt/data/mysql/:/var/lib/ ...

  8. WPF 精修篇 非UI进程后台更新UI进程

    原文:WPF 精修篇 非UI进程后台更新UI进程 <Grid> <Grid.RowDefinitions> <RowDefinition Height="11* ...

  9. 小程序上传图片功能 uploadFile:fail Read error:ssl=0xa738d808:I/O error during system call,Connection reset by peer

    由于纯网页上传图片小程序会闪退,就采用了小程序原生的上传功能wx.uploadfile 处理流程: 1.网页需要跳转到小程序 需要引用 <script src='https://res.wx.q ...

  10. Ext.form.Label组件动态设置html值

    解决方法: (1)用的是 Ext.getCmp(id).setText('XXXX')可以动态设置label 显示的文本值,但是文本中有个别数字需要改变颜色显示,需要加样式,这种方法会把加样式的标签( ...