Codeforces 1036E Covered Points (线段覆盖的整点数)【计算几何】
<题目链接>
<转载于 >>> >
题目大意:
在二维平面上给出n条不共线的线段(线段端点是整数),问这些线段总共覆盖到了多少个整数点。
解题分析:
用GCD可求的某条给定线段上有多少个整数点,理由如下:
GCD(n,m)为n与m的最大公约数,通过辗转相除法求得。令g=GCD(n,m); n=x*g, m=y*g.所以将横坐标分为g个x份,将纵坐标分为g个y份。所以,本题线段覆盖的整数点个数为 g+1 (因为包含端点,如果不包含端点就为 g-1 )。
但是这样求的的覆盖点数是包含重复点数的,所以我们可以对每条线段再遍历一次,求的它与它之后线段的不重复交点个数,然后用总的交点个数减去这些重复计算的交点,就是答案了。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define eps 1e-6
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Fore(i,a,b) for(int i=a;i>=b;i--)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mkp make_pair
#define pb push_back
#define sz size()
#define met(a,b) memset(a,b,sizeof(a))
#define iossy ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) //提高cin、cout的效率
#define fr freopen
#define pi acos(-1.0)
#define Vector Point
typedef pair<int,int> pii;
const long long linf=1LL<<;
const int iinf=<<;
const double dinf=1e17;
const int Mod=1e9+;
typedef long long ll;
typedef long double ld;
const int maxn=;
int n;
struct Point{
ll x,y;
int id;
Point(ll x=,ll y=):x(x),y(y) {}
Point operator - (const Point &a)const { return Point(x-a.x,y-a.y);}
bool operator == (const Point &a)const { return x==a.x && y==a.y; }
}; ll gcd(ll a,ll b){
while(b>){
ll t=b;b=a%b;a=t;
}
return a;
} ll Cross(Vector a,Vector b){ //向量叉乘
return a.x*b.y-a.y*b.x;
}
ll Dot(Vector a,Vector b) { //向量相乘
return a.x*b.x+a.y*b.y;
}
bool onsg(Point p,Point a1,Point a2){ //判断点p是否在a1,a2组成的线段上
return Cross(a1-p,a2-p)== && Dot(a1-p,a2-p)<;
//共线且反向
}
void ck(ll &c){
if(c>) c=;
else if(c<) c=-;
}
int Ins(Point a1,Point a2,Point b1,Point b2){ //判断两个线段是否有交点
if(a1==b1 || a1==b2 || a2==b1 || a2==b2) return ; //如果有端点相等,那么线段必然有交点
if(onsg(a1,b1,b2) || onsg(a2,b1,b2) || onsg(b1,a1,a2) || onsg(b2,a1,a2)) return ; //如果有端点在另一条线段上,那么这两条线段必然有交点 ll c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1); //用c1*c2<0来判断b1,b2是否在a1~a2线段的两边 ll c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1); //用c3*c4<0来判断a1,a2是否在b1~b2线段的两边
ck(c1);ck(c2);ck(c3);ck(c4);
return c1*c2< && c3*c4<;
} set<pair<ll,ll> >c;
void chk(Point p,Vector v,Point q,Vector w){ //找到两条线段的交点坐标
Vector u=p-q;
ll v1=Cross(w,u),v2=Cross(v,w);
if(abs(v1*v.x)%v2!= || abs(v1*v.y)%v2!=) return ;
ll xx,yy;
xx=p.x+v.x*v1/v2;yy=p.y+v.y*v1/v2;
c.insert(mkp(xx,yy));
} struct segm{
Point p1,p2;
}; segm ss[maxn];
Point p1,p2;
void solve(){
iossy;
cin>>n;
int ans=;
For(i,,n){
cin>>p1.x>>p1.y>>p2.x>>p2.y;
ss[i].p1=p1;ss[i].p2=p2;
ans+=gcd(abs(ss[i].p2.x-ss[i].p1.x),abs(ss[i].p2.y-ss[i].p1.y))+; //此处也可以直接用 __gcd()函数
}//利用gcd找出所有线段所能覆盖的整数点的总数 For(i,,n){
c.clear(); //每次清空set
For(j,i+,n){
int ct=Ins(ss[i].p1,ss[i].p2,ss[j].p1,ss[j].p2);
if(ct) chk(ss[i].p1,ss[i].p2-ss[i].p1,ss[j].p1,ss[j].p2-ss[j].p1);
//如果线段i与线段j有交点的话,将线段i与线段i+1~n的所有不重复的整数交点个数找出来
}
ans-=c.sz;
}
cout<<ans<<endl;
} int main(){
solve();
return ;
}
2018-09-09
Codeforces 1036E Covered Points (线段覆盖的整点数)【计算几何】的更多相关文章
- Codeforces 1036E. Covered Points
又一次写起了几何.... 特殊处理在于有可能出现多条线段交于一点的情况,每次考虑时,对每条线段与其他所有线段的交点存在一个set里,对每一个set,每次删除set.size()即可 重点在于判断两条线 ...
- CodeForces 1000C Covered Points Count(区间线段覆盖问题,差分)
https://codeforces.com/problemset/problem/1000/C 题意: 有n个线段,覆盖[li,ri],最后依次输出覆盖层数为1~n的点的个数. 思路: 区间线段覆盖 ...
- codeforces 1000C - Covered Points Count 【差分】
题目:戳这里 题意:给出n个线段,问被1~n个线段覆盖的点分别有多少. 解题思路: 这题很容易想到排序后维护每个端点被覆盖的线段数,关键是端点值不好处理.比较好的做法是用差分的思想,把闭区间的线段改为 ...
- C - Covered Points Count CodeForces - 1000C (差分,离散化,统计)
C - Covered Points Count CodeForces - 1000C You are given nn segments on a coordinate line; each end ...
- EDU 50 E. Covered Points 利用克莱姆法则计算线段交点
E. Covered Points 利用克莱姆法则计算线段交点.n^2枚举,最后把个数开方,从ans中减去. ans加上每个线段的定点数, 定点数用gcs(△x , △y)+1计算. #include ...
- Educational Codeforces Round 46 C - Covered Points Count
C - Covered Points Count emmm 好像是先离散化一下 注意 R需要+1 这样可以确定端点 emmm 扫描线?瞎搞一下? #include<bits/stdc++.h&g ...
- Covered Points Count CF1000C 思维 前缀和 贪心
Covered Points Count time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
- CODEVS3037 线段覆盖 5[序列DP 二分]
3037 线段覆盖 5 时间限制: 3 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 数轴上有n条线段,线段的 ...
- CODEVS1643 线段覆盖3[贪心]
1643 线段覆盖 3 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 在一个数轴上有n条线段,现要选 ...
随机推荐
- Confluence 6 管理协同编辑 - 修改你的 Synchrony 配置
你不能通过 Confluence UI 修改 Synchrony 的配置.配置的修改是通过系统属性进行修改的.在绝大部分情况下,你不需要对默认的配置进行修改. 修改 Synchrony 运行的端口. ...
- vi 编辑器常用快捷键
vi 编辑器 vim 编辑器算是vi的进阶版本 所有的unix like 系统都会内建vi编辑器 vi三种模式分别为: 1.一般模式(默认模式或指令模式) 上下左右方向键 移动光标 pageUp pa ...
- N阶楼梯上楼问题
N阶楼梯上楼问题 时间限制: 1 Sec 内存限制: 32 MB 题目描述 样例输出 13 #include <stdio.h> int main() { int i, n; long ...
- 多线程相关-ThreadPoolExecutor
应用层面: ThreadPoolExecutor: 创建多线程池执行器:new ThreadPoolExecutor(),创建方法最终都是走的以下这个构造方法: /** * Creates a new ...
- rsync启动并生成PID
/usr/bin/rsync --daemon --config=/usr/local/rsync/etc/rsyncd.conf
- 备份还原数据数据库(固定IP版)
1.新建data文件夹,用于存放备份数据 2.新建db文件夹,用于存放初建数据库为脚本 3.首次使用双击export.bat进行备份数据库: 4.以后每次使用双击setup.bat进行还原数据库: 备 ...
- Appium Demo
import unittestimport timefrom appium import webdriverfrom public import configimport os #类继承unittes ...
- CentOS安装jdk的三种方法
方法一:手动解压JDK的压缩包,然后设置环境变量 方法二:用yum安装JDK,(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的 ...
- spring cloud Config--server
概述 使用Config Server,您可以在所有环境中管理应用程序的外部属性.客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spr ...
- 集腋成裘-02-css基础-01
CSS 层叠样式表(Cascading Style Sheets)(级联样式表) 1 选择器 1.1 写法 选择器是一个选择标签的过程. 选择器{属性:值;...} 1.2 基础选择器 标签选择器 类 ...