日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C - Line-line Intersection Gym - 102220C(线段相交)

發(fā)布時間:2024/7/5 编程问答 62 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C - Line-line Intersection Gym - 102220C(线段相交) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

There are n lines l1,l2,,ln

on the 2D-plane.

Staring at these lines, Calabash is wondering how many pairs of (i,j)

that 1i<jn and li,lj

share at least one common point. Note that two overlapping lines also share common points.

Please write a program to solve Calabash's problem.

Input

The first line of the input contains an integer T(1T1000)

, denoting the number of test cases.

In each test case, there is one integer n(1n100000)

in the first line, denoting the number of lines.

For the next n

lines, each line contains four integers xai,yai,xbi,ybi(|xai|,|yai|,|xbi|,|ybi|109). It means li passes both (xai,yai) and (xbi,ybi). (xai,yai) will never be coincided with (xbi,ybi)

.

It is guaranteed that n106

.

Output

For each test case, print a single line containing an integer, denoting the answer.

Example Input 3 2 0 0 1 1 0 1 1 0 2 0 0 0 1 1 0 1 1 2 0 0 1 1 0 0 1 1 Output 1 0 1 題解:兩條直線不平行必相交,若平行:若重合答案加1,否則不算。 我們用兩個map來刻畫直線的特性,mp1刻畫a*x+b*y的直線系有多少個,mp2刻畫a*x+b*y=c這一條直線有多少個。 假設(shè)當前直線與之前的線段都相交,那么我們需要減去與這條直線平行而不重合的直線。即ans+=i-1+mp2[]-mp1[]. #include<iostream> #include<cstring> #include<string> #include<queue> #include<stack> #include<algorithm> #include<stdio.h> #include<map> #include<set> using namespace std; typedef long long ll; typedef pair<ll,ll>P; typedef pair<pair<ll,ll>,ll>Pi; const int maxn=100010; map<pair<ll,ll>,ll>mp1;//兩個參數(shù)a,b,代表形如a*x+b*y=c(c任意)的直線有多少個 map<pair<pair<ll,ll>,ll>,ll>mp2;//三個參數(shù)a,b,c,代表形如a*x+b*y=c的直線有多少個,即相同直線有多少個 ll cnt,ans,n; int main() {ios::sync_with_stdio(0);int T;cin>>T;while(T--){ans=cnt=0;cin>>n;mp1.clear(),mp2.clear();for(int i=1;i<=n;i++){ll x1,x2,y1,y2;cin>>x1>>y1>>x2>>y2;ll a=x1-x2,b=y1-y2,c=x1*y2-x2*y1;ll g=__gcd(a,b);a/=g;b/=g;c/=g;mp1[P(a,b)]++;mp2[Pi(P(a,b),c)]++;ans+=i-1+mp2[Pi(P(a,b),c)]-mp1[P(a,b)];//假設(shè)與前i-1條邊都相交,需要減去與他平行而不重合的線段 }cout<<ans<<endl;}return 0; }

?

轉(zhuǎn)載于:https://www.cnblogs.com/cherish-lin/p/11066282.html

總結(jié)

以上是生活随笔為你收集整理的C - Line-line Intersection Gym - 102220C(线段相交)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。