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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
  int W, N;
  const int MAX_N = 100001;
  while (cin >> N >> W) {
    int b = 1, e = W;
    int cnt = W;
    vector<bool> used(MAX_N, false);
    REP(i, W) used[i + 1] = true;
    vector<int> ans;
    while (e <= N) {
      ans.push_back(cnt);
      for (int i = 1; i * i <= b; i++)
        if (b % i == 0) {
          if (used[i] && b + 2 * i > (e + 1)) {
            used[i] = false;
            cnt--;
          }
          if (used[b / i] && b + 2 * (b / i) > e + 1) {
            used[b / i] = false;
            cnt--;
          }
        }
      b++;
      e++;
      assert(!used[e]);
      used[e] = true;
      cnt++;
      for (int i = 1; i * i <= e; i++)
        if (e % i == 0) {
          if (!used[i] && e - i >= b) {
            used[i] = true;
            cnt++;
          }
          if (!used[e / i] && e - (e / i) >= b) {
            used[e / i] = true;
            cnt++;
          }
        }
    }
    REP(i, ans.size()) {
      cout << ans[i];
      if (i == ans.size() - 1)
        cout << endl;
      else
        cout << " ";
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
  int W, N;
  const int MAX_N = 100001;
  while (cin >> N >> W) {
    int b = 1, e = W;
    int cnt = W;
    vector<bool> used(MAX_N, false);
    REP(i, W) used[i + 1] = true;
    vector<int> ans;
    while (e <= N) {
      ans.push_back(cnt);
      for (int i = 1; i <= b; i++)
        if (b % i == 0 && used[i]) {
          if (b + 2 * i > (e + 1)) {
            used[i] = false;
            cnt--;
          }
        }
      b++;
      e++;
      assert(!used[e]);
      used[e] = true;
      cnt++;
      for (int i = 1; i < e; i++)
        if (e % i == 0 && !used[i] && e - i >= b) {
          used[i] = true;
          cnt++;
        }
    }
    REP(i, ans.size()) {
      cout << ans[i];
      if (i == ans.size() - 1)
        cout << endl;
      else
        cout << " ";
    }
  }
  return 0;
}