Problem p00821 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt"

struct point {
  int x;
  int y;
  int id;
};

struct line {
  int fromx;
  int tox;
  int id;
  ld ny;
  ld fromy;
  ld toy;
  ld getdy() const { return (toy - fromy) / (tox - fromx); }
};
bool operator<(const line &l, const line &r) {
  return abs(l.ny - r.ny) < eps ? l.getdy() < r.getdy() : l.ny < r.ny;
}
const int aa = 2001;
const int bb = 0;
int main() {
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    vector<vector<point>> xs(2 * aa + 1);
    vector<point> ps;
    for (int i = 0; i < N; ++i) {
      int x, y;
      cin >> x >> y;
      x -= bb;
      x += aa;
      y += aa;
      y = 2 * aa - y;
      xs[x].push_back(point{x, y, i});
      ps.push_back(point{x, y, i});
    }

    vector<line> nlines;
    vector<vector<int>> oks(2 * aa, vector<int>(2 * aa));
    for (int nx = 0; nx < aa * 2 + 1; ++nx) {
      for (auto p : xs[nx]) {
        for (int i = 0; i < 2; ++i) {
          int tarid;
          if (i == 0)
            tarid = (p.id + 1) % N;
          else
            tarid = (p.id + N - 1) % N;
          if (nx < ps[tarid].x) {
            const line aline{p.x,     ps[tarid].x, tarid,
                             ld(p.y), ld(p.y),     ld(ps[tarid].y)};
            auto it = lower_bound(nlines.begin(), nlines.end(), aline);
            nlines.insert(it, aline);
          } else if (nx > ps[tarid].x) {
            nlines.erase(find_if(nlines.begin(), nlines.end(),
                                 [=](const line &l) { return l.id == p.id; }));
          }
        }
      }
      for (auto &l : nlines) {
        const ld dy = (l.toy - l.fromy) / (l.tox - l.fromx);
        int fy, ty;
        if (abs(dy) < eps)
          continue;
        else if (dy > 0) {
          fy = int(l.ny + eps);
          ty = int(l.ny + dy - eps);
        } else {
          fy = int(l.ny + dy + eps);
          ty = int(l.ny - eps);
        }
        l.ny = l.ny + dy;
        for (int y = fy; y <= ty; ++y) {
          oks[y][nx] = true;
        }
      }
      for (int i = 0; i < nlines.size(); i += 2) {
        int fy = int(nlines[i].ny + eps);
        int ty = int(nlines[i + 1].ny - eps);
        for (int y = fy; y <= ty; ++y) {
          oks[y][nx] = true;
        }
      }
    }

    int ans = 0;
    for (int y = 0; y < 2 * aa; ++y) {
      for (int x = 0; x < 2 * aa; ++x) {
        ans += oks[y][x];
        /*if (oks[y][x])cout << "#";
        else cout << ".";*/
      }
      // cout << endl;
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt"

struct point {
  int x;
  int y;
  int id;
};

struct line {
  int fromx;
  int tox;
  int id;
  ld ny;
  ld fromy;
  ld toy;
  ld getdy() const { return (toy - fromy) / (tox - fromx); }
};
bool operator<(const line &l, const line &r) {
  return abs(l.ny - r.ny) < eps ? l.getdy() < r.getdy() : l.ny < r.ny;
}
const int aa = 2001;
const int bb = 0;
int main() {
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    vector<vector<point>> xs(2 * aa + 1);
    vector<point> ps;
    for (int i = 0; i < N; ++i) {
      int x, y;
      cin >> x >> y;
      x -= bb;
      x += aa;
      y += aa;
      y = 2 * aa - y;
      xs[x].push_back(point{x, y, i});
      ps.push_back(point{x, y, i});
    }

    vector<line> nlines;
    vector<vector<bool>> oks(2 * aa, vector<bool>(2 * aa));
    for (int nx = 0; nx < aa * 2 + 1; ++nx) {
      for (auto p : xs[nx]) {
        for (int i = 0; i < 2; ++i) {
          int tarid;
          if (i == 0)
            tarid = (p.id + 1) % N;
          else
            tarid = (p.id + N - 1) % N;
          if (nx < ps[tarid].x) {
            const line aline{p.x,     ps[tarid].x, tarid,
                             ld(p.y), ld(p.y),     ld(ps[tarid].y)};
            auto it = lower_bound(nlines.begin(), nlines.end(), aline);
            nlines.insert(it, aline);
          } else if (nx > ps[tarid].x) {
            nlines.erase(find_if(nlines.begin(), nlines.end(),
                                 [=](const line &l) { return l.id == p.id; }));
          }
        }
      }
      for (auto &l : nlines) {
        const ld dy = (l.toy - l.fromy) / (l.tox - l.fromx);
        int fy, ty;
        if (abs(dy) < eps)
          continue;
        else if (dy > 0) {
          fy = int(l.ny + eps);
          ty = int(l.ny + dy - eps);
        } else {
          fy = int(l.ny + dy + eps);
          ty = int(l.ny - eps);
        }
        l.ny = l.ny + dy;
        for (int y = fy; y <= ty; ++y) {
          oks[y][nx] = true;
        }
      }
      for (int i = 0; i < nlines.size(); i += 2) {
        int fy = int(nlines[i].ny + eps);
        int ty = int(nlines[i + 1].ny - eps);
        for (int y = fy; y <= ty; ++y) {
          oks[y][nx] = true;
        }
      }
    }

    int ans = 0;
    for (int y = 0; y < 2 * aa; ++y) {
      for (int x = 0; x < 2 * aa; ++x) {
        ans += oks[y][x];
        /*if (oks[y][x])cout << "#";
        else cout << ".";*/
      }
      // cout << endl;
    }
    cout << ans << endl;
  }
  return 0;
}