In World War 3, your countries' scientists have invented a special weapon. Assume that the enemy's city can be described by rectangular coordinates and it has n roads which are all lines. None of the road is paralled with Y-axis. Besides, each road is represented by two different points (ai,bi) (ci,di) on it. Any three roads will not intersect at one point.
This special weapon can destroy all the castles whose x coordinate belongs to (l,r). After spying, you know that all the castles are set in the crossing point of two roads and in each crossing point there is a castle. In addition, each road's end-point's x coordinate does not belong to (l,r).
The scientists want to check the weapon's effect. If its effect can not reach army's expectation, they have to spend more time and more money in expanding its range. Obviously, the number of castles it can destroy plays an important role on the effect. So you are asked to calculate how many castles can be destroyed by this special weapon.
Input
Input contains multiple cases.
Every test case, the first line is an integers n (2 <= n <= 10000). Then n lines follow. The (i+1)-th line contains four integers ai,bi,ci,di (-1E8 <= ai,bi,ci,di <= 1E8). The (n+2)-th line contains two doubles l,r (-1E8 <= l,r <= 1E8) There is a blank line between two cases.
Output
For each case, output the number of castles that can be destroyed by the weapon.
Sample Input
3 0 0 1 1 2 0 1 1 0 0 2 0 0 2.5
Sample Output
2
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5 + 108; const ll mod = 1e9 + 7; int ooo = 800; int n; struct Line { double k, b; } o[maxn]; struct point { double l, r; int pos; } s[maxn]; double L, R; bool cmp1(point S, point T) { return S.l <= T.l; } bool cmp2(point S, point T) { return S.r <= T.r; } int sum[maxn]; inline void pushup(int rt) { sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; } inline void build(int rt, int l, int r) { if (l == r) { sum[rt] = 0; return; } int mid = l + r >> 1; build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r); pushup(rt); } inline void update(int rt, int l, int r, int pos, int val) { if (l == r) { sum[rt] += val; return; } int mid = l + r >> 1; if (pos <= mid) { update(rt << 1, l, mid, pos, val); } else { update(rt << 1 | 1, mid + 1, r, pos, val); } pushup(rt); } inline int query(int rt, int l, int r, int ql, int qr) { if (ql <= l && qr >= r) { return sum[rt]; } int mid = l + r >> 1, cur = 0; if (ql <= mid) { cur += query(rt << 1, l, mid, ql, qr); } if (qr > mid) { cur += query(rt << 1 | 1, mid + 1, r, ql, qr); } return cur; } int main() { #ifndef ONLINE_JUDGE freopen("1.txt", "r", stdin); #endif while (scanf("%d", &n) != EOF) { for (register int i = 1; i <= n; ++i) { double x1, y1, x2, y2; scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); o[i].k = (y2 - y1) / (x2 - x1); o[i].b = y1 - o[i].k * x1; } scanf("%lf%lf", &L, &R); L += 1e-8; R -= 1e-8; for (register int i = 1; i <= n; ++i) { s[i].l = o[i].k * L + o[i].b; s[i].r = o[i].k * R + o[i].b; } sort(s + 1, s + 1 + n, cmp1); for (register int i = 1; i <= n; ++i) { s[i].pos = i; } sort(s + 1, s + 1 + n, cmp2); int res = 0; build(1, 1, n); for (register int i = 1; i <= n; ++i) { update(1, 1, n, s[i].pos, 1); res += i - query(1, 1, n, 1, s[i].pos); } printf("%d ", res); } return 0; }