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

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  for (int n; cin >> n, n;) {
    int x[123], y[123];
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i];
      x[i] += 2000;
      y[i] += 2000;
    }
    x[n] = x[0];
    y[n] = y[0];
    int ans = 0;
    for (int i = 0; i <= 4000; i++) {
      bool in[4234] = {};
      vector<double> cx[2];
      for (int j = 0; j < n; j++) {
        if (y[j] != y[j + 1] && min(y[j], y[j + 1]) <= i &&
            i + 1 <= max(y[j], y[j + 1])) {
          double x1 =
              (x[j + 1] - x[j]) * (i - y[j]) * 1. / (y[j + 1] - y[j]) + x[j];
          double x2 =
              (x[j + 1] - x[j]) * (i + 1 - y[j]) * 1. / (y[j + 1] - y[j]) +
              x[j];
          cx[0].push_back(x1);
          cx[1].push_back(x2);
          if (x1 > x2) {
            swap(x1, x2);
          }
          for (int k = x1; k < x2; k++) {
            in[k] = true;
          }
        }
      }
      for (auto e : cx) {
        sort(begin(e), end(e));
        for (int i = 0; i < e.size(); i += 2) {
          for (int j = e[i]; j < e[i + 1]; j++) {
            in[j] = true;
          }
        }
      }
      ans += count(begin(in), end(in), true);
    }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

const int H = 4096;
const int W = 4096;
// bitset<W> on[H];
bitset<W> hl[H];
bitset<W> vl[H];
bool on[H][W];
// bool hl[H][W];
// bool vl[H][W];
// //set<tuple<short,short> > hl,vl;
// bool g[H][W];

int main() {
  for (int m; cin >> m, m;) {
    int x[123], y[123];
    for (int i = 0; i < m; i++) {
      cin >> x[i] >> y[i];
      x[i] += 2001;
      y[i] += 2001;
    }
    x[m] = x[0];
    y[m] = y[0];
    fill(*begin(on), *end(on), false);
    // fill(*begin(g),*end(g),false);
    // fill(*begin(vl),*end(vl),false);
    // fill(*begin(hl),*end(hl),false);
    for (int i = 0; i < H; i++) {
      //      on[i].reset();
      vl[i].reset();
      hl[i].reset();
    }
    for (int i = 0; i < m; i++) {
      if (x[i] == x[i + 1]) {
        for (int j = min(y[i], y[i + 1]); j < max(y[i], y[i + 1]); j++) {
          //	  cout<<j<<' '<<x[i]-1<<' '<<x[i]<<endl;
          //	  vl.insert(make_tuple(j,x[i]-1));
          vl[j][x[i] - 1] = true;
        }
      } else if (y[i] == y[i + 1]) {
        for (int j = min(x[i], x[i + 1]); j < max(x[i], x[i + 1]); j++) {
          //	  cout<<j<<' '<<y[i]-1<<' '<<y[i]<<endl;
          //	  hl.insert(make_tuple(j,y[i]-1));
          hl[j][y[i] - 1] = true;
        }
      } else {
        int x1 = x[i], y1 = y[i], x2 = x[i + 1], y2 = y[i + 1];
        if (x1 > x2) {
          swap(x1, x2);
          swap(y1, y2);
        }
        //	cerr<<x1<<' '<<y1<<' '<<x2<<' '<<y2<<endl;
        for (int j = 0; j < x2 - x1; j++) {
          double dsy = y1 + (y2 - y1) * 1. / (x2 - x1) * j;
          double dty = y1 + (y2 - y1) * 1. / (x2 - x1) * (j + 1);
          int sy, ty;
          if (abs(dsy - int(dsy + .5)) < 1e-9) {
            sy = dsy + .5;
          } else {
            if (y1 < y2) {
              sy = dsy;
            } else {
              sy = dsy + 1;
            }
          }
          if (abs(dty - int(dty + .5)) < 1e-9) {
            ty = dty + .5;
          } else {
            if (y1 < y2) {
              ty = dty + 1;
            } else {
              ty = dty;
            }
          }
          for (int k = sy; k != ty; k += (sy < ty) - (sy > ty)) {
            //	    cout<<k<<' '<<x1+j<<endl;
            on[k - (sy > ty)][x1 + j] = true;
          }
        }
      }
    }
    vector<pair<short, short>> v;
    v.reserve(H * W * 2 / 3);
    v.emplace_back(0, 0);
    on[0][0] = true;
    int ans = H * W - 1;
    while (!v.empty()) {
      auto c = v.back();
      v.pop_back();
      int cy = c.first;
      int cx = c.second;
      for (int i = 0; i < 4; i++) {
        static int dy[] = {-1, 0, 1, 0};
        static int dx[] = {0, 1, 0, -1};
        int ny = cy + dy[i];
        int nx = cx + dx[i];
        bool f = false;
        if (0 <= ny && ny < H && 0 <= nx && nx < W && !on[ny][nx]) {
          if (i % 2) {
            //	    if(!vl.count(make_tuple(cy,min(cx,nx)))){
            if (!vl[cy][min(cx, nx)]) {
              f = true;
            }
          } else {
            //	    if(!hl.count(make_tuple(cx,min(cy,ny)))){
            if (!hl[cx][min(cy, ny)]) {
              f = true;
            }
          }
        }
        if (f) {
          on[ny][nx] = true;
          v.emplace_back(ny, nx);
          ans--;
        }
      }
    }
    cout << ans << endl;
  }
}