Problem p02304 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
long long bit[1000000], _size, n, cnt, __X1[200000], __Y1[200000], __X2[200000],
    __Y2[200000];
vector<long long> XX, YY, _XX, _YY;
vector<tuple<long long, long long, long long>> vec;
void add(long long p, long long x) {
  while (p <= _size) {
    bit[p] += x;
    p += p & -p;
  }
}
long long sum(long long p) {
  int _sum = 0;
  while (p >= 1) {
    _sum += bit[p];
    p -= p & -p;
  }
  return _sum;
}
long long __sum(long long l, long long r) { return sum(r) - sum(l - 1); }
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> __X1[i] >> __Y1[i] >> __X2[i] >> __Y2[i];
  for (int i = 0; i < n; i++) {
    XX.push_back(__X1[i]);
    XX.push_back(__X2[i]);
    YY.push_back(__Y1[i]);
    YY.push_back(__Y2[i]);
  }
  sort(XX.begin(), XX.end());
  sort(YY.begin(), YY.end());
  for (int i = 0; i < XX.size(); i++) {
    if (i == 0 || XX[i - 1] != XX[i])
      _XX.push_back(XX[i]);
    if (i == 0 || YY[i - 1] != YY[i])
      _YY.push_back(YY[i]);
  }
  for (int i = 0; i < n; i++) {
    int pos1 = lower_bound(_XX.begin(), _XX.end(), __X1[i]) - _XX.begin(),
        pos2 = lower_bound(_YY.begin(), _YY.end(), __Y1[i]) - _YY.begin();
    int pos3 = lower_bound(_XX.begin(), _XX.end(), __X2[i]) - _XX.begin(),
        pos4 = lower_bound(_YY.begin(), _YY.end(), __Y2[i]) - _YY.begin();
    pos1++;
    pos2++;
    pos3++;
    pos4++;
    __X1[i] = pos1;
    __Y1[i] = pos2;
    __X2[i] = pos3;
    __Y2[i] = pos4;
    if (__X1[i] > __X2[i])
      swap(__X1[i], __X2[i]);
    if (__Y1[i] > __Y2[i])
      swap(__Y1[i], __Y2[i]);
    if (__X1[i] == __X2[i]) {
      vec.push_back(make_tuple(__Y1[i], -1LL << 60, __X1[i]));
      vec.push_back(make_tuple(__Y2[i], 1LL << 60, __X1[i]));
    } else {
      vec.push_back(make_tuple(__Y1[i], __X1[i], __X2[i]));
    }
  }
  sort(vec.begin(), vec.end());
  _size = XX.size() + 10;
  for (int i = 0; i < vec.size(); i++) {
    if (abs(get<1>(vec[i])) >= 1LL << 50)
      add(get<2>(vec[i]), get<1>(vec[i]) / (-1LL << 60));
    else {
      cnt += __sum(get<1>(vec[i]), get<2>(vec[i]));
    }
  }
  cout << cnt << endl;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
struct Point {
  long double px, py;
};
struct Segment {
  Point p1, p2;
};
long double dot(const Point &a, const Point &b) {
  return a.px * b.px + a.py * b.py;
}
long double crs(const Point &a, const Point &b) {
  return a.px * b.py - a.py * b.px;
}
long double norm(const Point &a) { return a.px * a.px + a.py * a.py; }
int ccw(Point p0, Point p1, Point p2) {
  Point a, b;
  a.px = p1.px - p0.px;
  a.py = p1.py - p0.py;
  b.px = p2.px - p0.px;
  b.py = p2.py - p0.py;
  if (crs(a, b) > 1e-10)
    return 1;
  if (crs(a, b) < -1e-10)
    return -1;
  if (dot(a, b) < -1e-10)
    return 2;
  if (norm(a) < norm(b))
    return -2;
  return 0;
}
bool its(Point p1, Point p2, Point p3, Point p4) {
  return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 &&
          ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0);
}
bool its(Segment S1, Segment S2) { return its(S1.p1, S1.p2, S2.p1, S2.p2); }
int N;
Segment X[200000];
int main() {
  long long sum = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> X[i].p1.px >> X[i].p1.py >> X[i].p2.px >> X[i].p2.py;
  }
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      if (its(X[i], X[j]) == true) {
        sum++;
      }
    }
  }
  cout << sum << endl;
  return 0;
}