#284# div.2 C.Crazy Town

來源:互聯網
上載者:User

標籤:

C. Crazy Town 

Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let‘s call each such region a block. We define an intersection as the point where at least two different roads intersect.

Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).

Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.

Input

The first line contains two space-separated integers x1, y1 ( - 106 ≤ x1, y1 ≤ 106) — the coordinates of your home.

The second line contains two integers separated by a space x2, y2 ( - 106 ≤ x2, y2 ≤ 106) — the coordinates of the university you are studying at.

The third line contains an integer n (1 ≤ n ≤ 300) — the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≤ ai, bi, ci ≤ 106; |ai| + |bi| > 0) — the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).

Output

Output the answer to the problem.

Sample test(s)Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note

Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):

 

POINT:

  1.判斷直線與線段是否相交即判斷線段兩端點是否在直線兩側;

  注意此題不能直接判斷,因為乘積可能long long越界;

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<string> 5 #include<algorithm> 6 #include<map> 7 using namespace std; 8 const int maxn = 3010; 9 10 struct Node11 {12     long long x, y;13 }p1, p2;14 int n;15 int main()16 {17     scanf("%I64d%I64d%I64d%I64d", &p1.x, &p1.y, &p2.x, &p2.y);18     scanf("%d", &n);19     long long cnt = 0;20     for(int i = 0; i < n; i++)21     {22         long long a, b, c;23         scanf("%I64d%I64d%I64d", &a, &b, &c);24         long long t1 = (a*p1.x + b*p1.y + c);25         long long t2 = (a*p2.x + b*p2.y + c);26         //  此處不能直接判斷: long long 越界 !27         if( (t1>0 && t2<0) || (t1<0 && t2>0) )  cnt++;28     }29     printf("%I64d\n", cnt);30     return 0;31 }

 

#284# div.2 C.Crazy Town

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.