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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

struct Point {
  double x, y;
};

struct Segment {
  Point p1, p2;
};

#define BOTTOM 0
#define LEFT 1
#define RIGHT 2
#define TOP 3

class EndPoint {
public:
  Point p;
  int seg, st;
  EndPoint() {}
  EndPoint(Point p, int seg, int st) : p(p), seg(seg), st(st) {}

  bool operator<(const EndPoint &ep) const {
    if (p.y == ep.p.y) {
      return st < ep.st;
    } else
      return p.y < ep.p.y;
  }
};

EndPoint EP[2 * 100000];

int manhattanIntersection(vector<Segment> S) {
  int n = S.size();

  for (int i = 0, k = 0; i < n; i++) {
    if (S[i].p1.y == S[i].p2.y) {
      if (S[i].p1.x > S[i].p2.x)
        swap(S[i].p1, S[i].p2);
    } else if (S[i].p1.y > S[i].p2.y)
      swap(S[i].p1, S[i].p2);

    if (S[i].p1.y == S[i].p2.y) {
      EP[k++] = EndPoint(S[i].p1, i, LEFT);
      EP[k++] = EndPoint(S[i].p2, i, RIGHT);
    } else {
      EP[k++] = EndPoint(S[i].p1, i, BOTTOM);
      EP[k++] = EndPoint(S[i].p2, i, TOP);
    }
  }

  sort(EP, EP + (2 * n));

  set<int> BT;
  BT.insert(1000000001);
  int cnt = 0;

  for (int i = 0; i < 2 * n; i++) {
    if (EP[i].st == TOP) {
      BT.erase(EP[i].p.x);
    } else if (EP[i].st == BOTTOM) {
      BT.insert(EP[i].p.x);
    } else if (EP[i].st == LEFT) {
      set<int>::iterator b = BT.lower_bound(S[EP[i].seg].p1.x);
      set<int>::iterator e = BT.upper_bound(S[EP[i].seg].p2.x);
      cnt += distance(b, e);
    }
  }

  return cnt;
}

int main() {
  int n;
  vector<Segment> s;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    Segment x;
    scanf("%lf %lf %lf %lf", &x.p1.x, &x.p1.y, &x.p2.x, &x.p2.y);
    s.push_back(x);
  }

  printf("%d\\n", manhattanIntersection(s));

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

struct Point {
  double x, y;
};

struct Segment {
  Point p1, p2;
};

#define BOTTOM 0
#define LEFT 1
#define RIGHT 2
#define TOP 3

class EndPoint {
public:
  Point p;
  int seg, st;
  EndPoint() {}
  EndPoint(Point p, int seg, int st) : p(p), seg(seg), st(st) {}

  bool operator<(const EndPoint &ep) const {
    if (p.y == ep.p.y) {
      return st < ep.st;
    } else
      return p.y < ep.p.y;
  }
};

EndPoint EP[2 * 100000];

int manhattanIntersection(vector<Segment> S) {
  int n = S.size();

  for (int i = 0, k = 0; i < n; i++) {
    if (S[i].p1.y == S[i].p2.y) {
      if (S[i].p1.y > S[i].p2.x)
        swap(S[i].p1, S[i].p2);
    } else if (S[i].p1.y > S[i].p2.y)
      swap(S[i].p1, S[i].p2);

    if (S[i].p1.y == S[i].p2.y) {
      EP[k++] = EndPoint(S[i].p1, i, LEFT);
      EP[k++] = EndPoint(S[i].p2, i, RIGHT);
    } else {
      EP[k++] = EndPoint(S[i].p1, i, BOTTOM);
      EP[k++] = EndPoint(S[i].p2, i, TOP);
    }
  }

  sort(EP, EP + (2 * n));

  set<int> BT;
  BT.insert(1000000001);
  int cnt = 0;

  for (int i = 0; i < 2 * n; i++) {
    if (EP[i].st == TOP) {
      BT.erase(EP[i].p.x);
    } else if (EP[i].st == BOTTOM) {
      BT.insert(EP[i].p.x);
    } else if (EP[i].st == LEFT) {
      set<int>::iterator b = BT.lower_bound(S[EP[i].seg].p1.x);
      set<int>::iterator e = BT.upper_bound(S[EP[i].seg].p2.x);
      cnt += distance(b, e);
    }
  }

  return cnt;
}

int main() {
  int n;
  vector<Segment> s;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    Segment x;
    scanf("%lf %lf %lf %lf", &x.p1.x, &x.p1.y, &x.p2.x, &x.p2.y);
    s.push_back(x);
  }

  printf("%d\\n", manhattanIntersection(s));

  return 0;
}