p496.ans1
======================
5

=================
p496.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n;
vector<array<int, 4>> a;

void read() {
    cin >> n;
    a.resize(n);
    for(auto& x: a) {
        cin >> x[0] >> x[1] >> x[2] >> x[3];
    }
}

void solve() {
    // An L-shape is two segments meeting at a shared endpoint at a right angle.
    // For each segment compute its normalized direction (dx, dy) reduced by gcd
    // and canonically oriented (dx >= 0, axis directions fixed), then for each
    // of its two endpoints record that direction. slopes_per_point maps a point
    // to a multiset of directions of segments incident there. At a point, two
    // incident segments are perpendicular when their directions are negatives
    // of a 90-degree rotation; for each direction we look up its perpendicular
    // direction (rotate by 90: (dx, dy) -> (-dy, dx), then re-canonicalize) and
    // add the product of the two counts. Summing over all points double-counts
    // each pair (once per endpoint considered), so we divide by 2.

    map<pair<int, int>, map<pair<int, int>, int>> slopes_per_point;

    for(auto i = 0; i < n; i++) {
        int dx = a[i][2] - a[i][0];
        int dy = a[i][3] - a[i][1];
        int g = gcd(dx, dy);
        dx /= g;
        dy /= g;

        if(dx < 0) {
            dx = -dx;
            dy = -dy;
        }

        if(dx == 0) {
            dy = 1;
        }

        if(dy == 0) {
            dx = 1;
        }

        slopes_per_point[{a[i][0], a[i][1]}][{dx, dy}]++;
        slopes_per_point[{a[i][2], a[i][3]}][{dx, dy}]++;
    }

    int ans = 0;
    for(auto& [_, slopes]: slopes_per_point) {
        int cur = 0;
        for(auto& [slope, cnt]: slopes) {
            auto [dx, dy] = slope;
            swap(dx, dy);
            dx *= -1;
            if(dx < 0) {
                dx = -dx;
                dy = -dy;
            }

            if(slopes.count({dx, dy})) {
                cur += cnt * slopes[{dx, dy}];
            }
        }

        ans += cur;
    }

    cout << ans / 2 << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
p496.in1
======================
7
0 4 0 7
4 4 1 6
1 6 -3 0
4 4 0 4
0 0 0 4
0 0 0 2
0 4 4 4

=================
statement.txt
======================
496. L-Shapes
Time limit per test: 0.75 second(s)
Memory limit: 262144 kilobytes
input: standard
output: standard



Let's say that two line segments of non-zero length form an  if they are at a  angle to each other and one endpoint of one segment coincides with one endpoint of the other.

There are N line segments on a plane. The segments are numbered from 1 to N. Count the number of distinct pairs of segments that form L-shapes. Two pairs are considered distinct if they contain segments with different numbers.

Input
The first line of the input line contains the integer N (1 ≤ N ≤ 5000). Each of the following N lines describes one segment and contains four integers x1, y1, x2, y2 (), where (x1, y1) and (x2, y2) are endpoints of the segment. It may be assumed that for each segment x1 ≠q x2 or y1 ≠q y2.

Output
The output file should contain a single integer, the total number of distinct pairs of line segments forming L-shapes.

Example(s)
sample input
sample output
7
0 4 0 7
4 4 1 6
1 6 -3 0
4 4 0 4
0 0 0 4
0 0 0 2
0 4 4 4
5



Note. In the example the L-shapes are formed by the following pairs of segments: (1, 4), (1, 7), (2, 3), (4, 5), (5, 7). Note that the segments 4 and 7 coincide, but the pairs (1, 4) and (1, 7), for example, are still considered distinct.

=================
