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<<62;
const int iinf=1<<30;
const double dinf=1e17;
const int Mod=1e9+9;
typedef long long ll;
typedef long double ld;
const int maxn=1000005;
int n;
struct Point{
    ll x,y;
    int id;
    Point(ll x=0,ll y=0):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 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)==0 && Dot(a1-p,a2-p)<0;    
    //共线且反向
}
void ck(ll &c){
    if(c>0) c=1;
    else if(c<0) c=-1;
}
int Ins(Point a1,Point a2,Point b1,Point b2){     //判断两个线段是否有交点
    if(a1==b1 || a1==b2 || a2==b1 || a2==b2) return 1;   //如果有端点相等,那么线段必然有交点
    if(onsg(a1,b1,b2) || onsg(a2,b1,b2) || onsg(b1,a1,a2) || onsg(b2,a1,a2)) return 1;    //如果有端点在另一条线段上,那么这两条线段必然有交点   
    
    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<0 && c3*c4<0;
}

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!=0 || abs(v1*v.y)%v2!=0) 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=0;
    For(i,1,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))+1;
    }//利用gcd找出所有线段所能覆盖的整数点的总数

    For(i,1,n){
        c.clear();    //每次清空set
        For(j,i+1,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(){
    int t=1;
    solve();
    return 0;
}

2018-09-09

猜你喜欢

转载自www.cnblogs.com/00isok/p/9613583.html
今日推荐