Educational Codeforces Round 50 (Rated for Div. 2) E. Covered Points

注释上都有解析了,就不写了吧,去重的问题就用set解决,并且呢第i个线段最多和其他线段产生i-1个交点,n^2logn。

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
#include <functional>
#include <stack>
using namespace std;
typedef long long ll;
#define T int t_;Read(t_);while(t_--)
#define dight(chr) (chr>='0'&&chr<='9')
#define alpha(chr) (chr>='a'&&chr<='z')
#define INF (0x3f3f3f3f)
#define maxn (300005)
#define maxm (10005)
#define mod 1000000007
#define ull unsigned long long
#define repne(x,y,i) for(i=(x);i<(y);++i)
#define repe(x,y,i) for(i=(x);i<=(y);++i)
#define repde(x,y,i) for(i=(x);i>=(y);--i)
#define repdne(x,y,i) for(i=(x);i>(y);--i)
#define ri register int
inline void Read(int &n){char chr=getchar(),sign=1;for(;!dight(chr);chr=getchar())if(chr=='-')sign=-1;
    for(n=0;dight(chr);chr=getchar())n=n*10+chr-'0';n*=sign;}
inline void Read(ll &n){char chr=getchar(),sign=1;for(;!dight(chr);chr=getchar())if
    (chr=='-')sign=-1;
    for(n=0;dight(chr);chr=getchar())n=n*10+chr-'0';n*=sign;}
ll g[1005],sx[1005],sy[1005],ex[1005],ey[1005];
set<pair<ll,ll> >se[1005];
ll gcd(ll x,ll y){
    return (y==0)?x:gcd(y,x%y);
}
int main()
{
    freopen("a.in","r",stdin);
    freopen("b.out","w",stdout);
    //对于每一条线段的每一个整数点可由二元组(sx+k*(ex-sx)/gcd(ex-sx,sy-ey),sy+k*(ey-sy)/gcd(ex-sx,sy-ey))得到
    //由此可得到线段中所有的点个数为sum((ex-sx)/gcd(ex-sx,sy-ey)+1)
    //由于存在重复点需要减去重复点的重复个数
    //枚举解方程,若有解则可以确定此点的位置,由于n条线段最多产生n*(n-1)/2个交点
    int n;
    ri i,j,k;
    Read(n);
    repe(1,n,i) Read(sx[i]),Read(sy[i]),Read(ex[i]),Read(ey[i]),g[i] = gcd(abs(ex[i]-sx[i]),abs(sy[i]-ey[i]));
    ll ans = 0;
    repe(1,n,i) ans = ans + g[i] + 1;
    //sx[i] + s*(ex[i]-sx[i])/g[i] = sx[j] + t*(ex[j]-sx[j])/g[j]
    //sy[i] + s*(ey[i]-sy[i])/g[i] = sy[j] + t*(ey[j]-sy[j])/g[j]
    repe(1,n,i){
        ll a = (ex[i] - sx[i])/g[i],b = (ey[i] - sy[i])/g[i];
        ll lm = a / gcd(abs(a),abs(b)) * b;
        repe(i+1,n,j){
            ll c = (ex[j]-sx[j])/g[j],d = (ey[j]-sy[j])/g[j];
            if(a == 0){
                if(c != 0){
                    if((sx[i] - sx[j]) % c == 0){
                        ll t = (sx[i] - sx[j]) / c,x = sx[j] + t * c,y = sy[j] + t * d;
                        if((x - sx[i])*(ex[i]-sx[i]) >= 0 && abs(x-sx[i])<=abs(ex[i]-sx[i]) && (x - sx[j])*(ex[j]-sx[j]) >= 0 && abs(x-sx[j])<=abs(ex[j]-sx[j]) && (y - sy[i])*(ey[i]-sy[i]) >= 0 && abs(y-sy[i])<=abs(ey[i]-sy[i]) && (y - sy[j])*(ey[j]-sy[j]) >= 0 && abs(y-sy[j])<=abs(ey[j]-sy[j])){
                            
                            se[i].insert(make_pair(x,y));
                          //  --ans;
                        }
                    }
                }
                continue;
            }
            if(b == 0){
                if(d != 0){
                    if((sy[i] - sy[j]) % d == 0){
                        ll t = (sy[i] - sy[j]) / d,x = sx[j] + t * c,y = sy[j] + t * d;
                        if((x - sx[i])*(ex[i]-sx[i]) >= 0 && abs(x-sx[i])<=abs(ex[i]-sx[i]) && (x - sx[j])*(ex[j]-sx[j]) >= 0 && abs(x-sx[j])<=abs(ex[j]-sx[j]) && (y - sy[i])*(ey[i]-sy[i]) >= 0 && abs(y-sy[i])<=abs(ey[i]-sy[i]) && (y - sy[j])*(ey[j]-sy[j]) >= 0 && abs(y-sy[j])<=abs(ey[j]-sy[j])){
                            
                            se[i].insert(make_pair(x,y));
                           // --ans;
                        }
                    }
                }
                continue;
            }
            if(c * lm / a == d * lm / b) continue;
            ll tc = c,td = d;
            c *= lm / a,d *= lm/b;
            if(c - d == 0) continue;
            if(((sx[i]-sx[j])*lm/a - (sy[i]-sy[j])*lm/b) % (c-d) != 0) continue;
            else{
                
                ll t = ((sx[i]-sx[j])*lm/a - (sy[i]-sy[j])*lm/b) / (c-d),c = tc,d = td,x = sx[j] + t * c,y = sy[j] + t * d;
                if((x - sx[i])*(ex[i]-sx[i]) >= 0 && abs(x-sx[i])<=abs(ex[i]-sx[i]) && (x - sx[j])*(ex[j]-sx[j]) >= 0 && abs(x-sx[j])<=abs(ex[j]-sx[j]) && (y - sy[i])*(ey[i]-sy[i]) >= 0 && abs(y-sy[i])<=abs(ey[i]-sy[i]) && (y - sy[j])*(ey[j]-sy[j]) >= 0 && abs(y-sy[j])<=abs(ey[j]-sy[j])){
                   
                    se[i].insert(make_pair(x,y));
                    //--ans;
                }
            }
        }
    }
    for(int i = 1;i <= n;++i) ans -= (int)se[i].size();
    cout << ans << endl;
    return 0;
}

 

posted @ 2018-09-12 18:37  zhuiyicc  阅读(124)  评论(0编辑  收藏  举报