alexjercan/bugnet huggingface dataset:
Problem p01602 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <string>

int main() {
  int n;

  std::cin >> n;

  char c;
  long long cnt, stat = 0;
  for (int i = 0; i < n; ++i) {
    std::cin >> c >> cnt;

    if (c == ')')
      stat -= cnt;
    else
      stat += cnt;

    if (stat < 0) {
      std::cout << "NO" << std::endl;

      return 0;
    }
  }

  if (stat == 0)
    std::cout << "YES" << std::endl;
  else
    std::cout << "NO" << std::endl;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <string>

std::string input() {
  int n;
  std::string res;

  std::cin >> n;

  char c;
  int cnt;
  for (int i = 0; i < n; ++i) {
    std::cin >> c >> cnt;

    res += std::string(cnt, c);
  }

  return res;
}

int main() {
  auto str = input();

  int stat = 0;
  for (auto &&c : str) {
    if (c == ')')
      --stat;
    else
      ++stat;

    if (stat < 0) {
      std::cout << "NO" << std::endl;

      return 0;
    }
  }

  std::cout << "YES" << std::endl;
}
Problem p01602 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <string>
using namespace std;

int main() {

  int n;
  int m;
  string s;
  long long int ans = 0;
  bool d = false;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> s >> m;

    if (s == "(")
      ans += m;
    if (s == ")")
      ans -= m;

    if (ans < 0)
      d = true;
  }

  if (ans == 0 && !d)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <stack>
#include <string>
using namespace std;

int main() {

  int n;
  string s;
  int m;
  stack<int> sta;
  bool ans = false;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> s >> m;

    if (s == "(" && ans == false) {
      for (int j = 0; j < m; j++)
        sta.push(1);
    }

    if (s == ")" && ans == false) {
      for (int j = 0; j < m; j++) {
        if (sta.size() == 0) {
          ans = true;
          break;
        }
        sta.pop();
      }
    }
  }

  if (sta.size() == 0 && !ans)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
Problem p01602 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {

  string s;
  int n;
  cin >> n;
  int d = 0;
  bool flag = true;
  for (int i = 0; i < n; i++) {
    char c;
    cin >> c;
    int p;
    cin >> p;
    if (flag == false)
      continue;

    if (c == '(')
      d += p;
    else
      d -= p;

    if (d < 0) {
      flag = false;
      continue;
    }
  }

  if (d != 0)
    flag = false;

  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  // cout << s << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {

  string s;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    char c;
    cin >> c;
    int cnt;
    cin >> cnt;
    for (int j = 0; j < cnt; j++) {
      s = s + c;
    }
  }

  // cout << s << endl;
  int d = 0;
  for (int i = 0; i < (int)s.size(); i++) {
    if (s[i] == '(')
      d++;
    else
      d--;
    if (d < 0) {
      cout << "NO" << endl;
      return 0;
    }
  }

  if (d == 0)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
Problem p01602 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
using namespace std;

int main(void) {
  int n;
  cin >> n;
  unsigned int st = 0;
  for (int i = 0; i < n; i++) {
    char parentheses;
    int p_num;
    cin >> parentheses >> p_num;
    if (parentheses == '(') {
      st += p_num;
    } else {
      if (st < p_num) {
        cout << "NO" << endl;
        return 0;
      } else {
        st -= p_num;
      }
    }
  }
  if (st == 0) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <stack>
using namespace std;

int main(void) {
  int n;
  cin >> n;
  stack<bool> st;
  for (int i = 0; i < n; i++) {
    char parentheses;
    int p_num;
    cin >> parentheses >> p_num;
    if (parentheses == '(') {
      for (int i = 0; i < p_num; i++) {
        st.push(true);
      }
    } else {
      for (int i = 0; i < p_num; i++) {
        if (st.empty()) {
          cout << "NO" << endl;
          return 0;
        } else {
          st.pop();
        }
      }
    }
  }
  if (st.empty()) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
  return 0;
}
Problem p01602 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 10000000;

int main() {
  int n;
  cin >> n;
  bool f = true;
  ll s = 0LL, a;
  char c;
  rep(i, n) {
    cin >> c >> a;
    if (c == ')')
      a = -a;
    s += a;
    if (s < 0)
      f = false;
  }
  cout << (f && s == 0 ? "YES" : "NO") << endl;
}

Error: MLE

Fixed submission:

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 10000000;

int main() {
  int n;
  cin >> n;
  string s;
  int a;
  char c;
  rep(i, n) {
    cin >> c >> a;
    string t(a, c);
    s += t;
  }
  n = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '(')
      n++;
    else
      n--;
    if (n < 0) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
}
Problem p01604 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

typedef pair<int, int> P;

const int INF = 1 << 29;

int N;
vector<string> line(100);
map<string, int> label;
int to[101];
int d[101];
vector<int> from[101];

int main() {
  ios::sync_with_stdio(false);
  cin >> N;
  cin.ignore();
  for (int x = 0; x < N; x++) {
    getline(cin, line[x]);
  }
  for (int x = 0; x < N; x++) {
    to[x] = x + 1;
    if (line[x].substr(0, 5) == "goto ")
      continue;
    label[line[x].substr(0, line[x].size() - 1)] = x;
  }
  for (int x = 0; x < N; x++) {
    if (line[x].substr(0, 5) != "goto ")
      continue;
    to[x] = label[line[x].substr(5, line[x].size() - 6)];
  }
  for (int x = 0; x < N; x++) {
    from[to[x]].push_back(x);
  }
  queue<P> que;
  que.push(P(N, 0));
  fill(d, d + N + 1, INF);
  int ans = INF;
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int loc = p.first, cnt = p.second;
    if (d[loc] <= cnt)
      continue;
    d[loc] = cnt;
    if (cnt >= ans)
      continue;
    if (loc == 0) {
      ans = cnt;
      continue;
    }
    for (int x = 0; x < from[loc].size(); x++) {
      que.push(P(from[loc][x], cnt));
    }
    if (to[loc - 1] != loc) {
      que.push(P(loc - 1, cnt + 1));
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

typedef pair<int, int> P;

int N;
vector<string> line(100);
map<string, int> label;
int to[101];
vector<int> from[101];

int main() {
  ios::sync_with_stdio(false);
  cin >> N;
  cin.ignore();
  for (int x = 0; x < N; x++) {
    getline(cin, line[x]);
  }
  for (int x = 0; x < N; x++) {
    to[x] = x + 1;
    if (line[x].substr(0, 5) == "goto ")
      continue;
    label[line[x].substr(0, line[x].size() - 1)] = x;
  }
  for (int x = 0; x < N; x++) {
    if (line[x].substr(0, 5) != "goto ")
      continue;
    to[x] = label[line[x].substr(5, line[x].size() - 6)];
  }
  for (int x = 0; x < N; x++) {
    from[to[x]].push_back(x);
  }
  // for(int x = 0; x <= N; x++){
  //     cout << x << ":";
  //     for(int y = 0; y < from[x].size(); y++){
  //         cout << ' ' << from[x][y];
  //     }
  //     cout << endl;
  // }
  queue<P> que;
  que.push(P(N, 0));
  int ans = 1 << 29;
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int loc = p.first, cnt = p.second;
    if (cnt >= ans)
      continue;
    if (loc == 0)
      ans = cnt;
    for (int x = 0; x < from[loc].size(); x++) {
      que.push(P(from[loc][x], cnt));
    }
    if (to[loc - 1] != loc) {
      que.push(P(loc - 1, cnt + 1));
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p01604 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int n;
int g[110];
int dp[110];

int solve(int i, int k) {
  if (i >= n)
    return 0;
  if (dp[i] != -1)
    return dp[i];
  if (k > 1000)
    return 1000000;
  int r = min(solve(g[i], k + 1), solve(i + 1, k + 1) + 1);
  dp[i] = r;
  return r;
}

int main() {
  cin >> n;
  map<string, int> m;
  bool F[110];
  string S[110];
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    F[i] = (s == "goto");
    if (F[i]) {
      cin >> s;
      S[i] = s.substr(0, s.size() - 1);
    } else {
      S[i] = s.substr(0, s.size() - 1);
      m[S[i]] = i;
    }
  }
  vector<int> v[110];
  for (int i = 0; i < n; i++) {
    if (F[i]) {
      assert(m.find(S[i]) != m.end());
      v[m[S[i]]].push_back(i);
      g[i] = m[S[i]];
    } else {
      v[i + 1].push_back(i);
      g[i] = i + 1;
    }
  }
  priority_queue<P, vector<P>, greater<P>> q;
  q.push(P(0, n));
  bool used[110] = {};
  int r = 10000;
  while (!q.empty()) {
    int a, b;
    tie(a, b) = q.top();
    q.pop();
    if (used[b])
      continue;
    used[b] = true;
    if (!b) {
      r = a;
      break;
    }
    q.push(P(a + 1, b - 1));
    for (int d : v[b]) {
      q.push(P(a, d));
    }
  }
  cout << r << endl;
  return 0;
}

Error: -6
12f5e8c5-5666-4e5a-bfbd-785fd97c029a.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p01604/C++/s651788635.cpp:55: int main(): Assertion `m.find(S[i]) == m.end()' failed.


Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef tuple<int, int, ll, ll> Q;

int n;
int g[110];
int dp[110];

int solve(int i, int k) {
  if (i == n)
    return 0;
  if (dp[i] != -1)
    return dp[i];
  if (k > 1000)
    return 1000000;
  int r = min(solve(g[i], k + 1), solve(i + 1, k + 1) + 1);
  dp[i] = r;
  return r;
}

int main() {
  cin >> n;
  map<string, int> m;
  bool F[110];
  string S[110];
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    F[i] = (s == "goto");
    if (F[i]) {
      cin >> s;
      S[i] = s.substr(0, s.size() - 1);
    } else {
      S[i] = s.substr(0, s.size() - 1);
      m[S[i]] = i;
    }
  }
  for (int i = 0; i < n; i++) {
    if (F[i]) {
      assert(m.find(S[i]) == m.end());
      g[i] = m[S[i]];
    } else {
      g[i] = i + 1;
    }
  }
  fill_n(dp, 110, -1);
  cout << solve(0, 0) << endl;
  return 0;
}
Problem p01604 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
using namespace std;
typedef pair<vector<int>, int> P;

string getVal(string message) {
  stringstream ss;
  ss << message;
  string a, b;
  ss >> a >> b;
  return b.substr(0, b.size() - 1);
}

int main() {
  int N;
  cin >> N;
  int G[N + 1][N + 1];

  for (int i = 0; i < N + 1; i++)
    for (int j = 0; j < N + 1; j++)
      G[j][i] = (1 << 29);
  map<string, P> index;
  cin.ignore();
  for (int i = 0; i < N; i++) {
    string line;
    getline(cin, line);

    if (line.substr(0, 4) == "goto")
      index[getVal(line)].first.push_back(i);
    else
      index[line.substr(0, line.size() - 1)].second = i;
  }

  for (map<string, P>::iterator it = index.begin(); it != index.end(); it++) {
    for (int i = 0; i < (*it).second.first.size(); i++) {
      G[((*it).second.first)[i]][(*it).second.second] = 0;
      G[((*it).second.first)[i]][((*it).second.first)[i] + 1] =
          min(G[(*it).second.first[i]][(*it).second.first[i] + 1], 1);
    }
    G[(*it).second.second][(*it).second.second + 1] = 0;
  }

  for (int k = 0; k < N + 1; k++)
    for (int i = 0; i < N + 1; i++)
      for (int j = 0; j < N + 1; j++)
        G[i][j] = min(G[i][j], G[i][k] + G[k][j]);

  cout << G[0][N] << endl;
  return 0;
}

Error: 0
2
LOOP:
goto LOOP;


Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
using namespace std;
typedef pair<int, int> P;

string getVal(string message) {
  stringstream ss;
  ss << message;
  string a, b;
  ss >> a >> b;
  return b.substr(0, b.size() - 1);
}

int main() {
  int N;
  cin >> N;
  cerr << N << endl;
  int G[N + 1][N + 1];

  for (int i = 0; i < N + 1; i++)
    for (int j = 0; j < N + 1; j++)
      G[j][i] = (1 << 29);
  map<string, P> index;
  cin.ignore();
  for (int i = 0; i < N; i++) {
    string line;
    getline(cin, line);
    cerr << line << endl;
    if (line.substr(0, 4) == "goto")
      index[getVal(line)].first = i;
    else
      index[line.substr(0, line.size() - 1)].second = i;
  }

  for (map<string, P>::iterator it = index.begin(); it != index.end(); it++) {
    G[(*it).second.first][(*it).second.second] = 0;
    if ((*it).second.first > (*it).second.second)
      G[(*it).second.first][(*it).second.first + 1] = 1;
    G[(*it).second.second][(*it).second.second + 1] = 0;
  }

  for (int k = 0; k < N + 1; k++)
    for (int i = 0; i < N + 1; i++)
      for (int j = 0; j < N + 1; j++)
        G[i][j] = min(G[i][j], G[i][k] + G[k][j]);

  cout << G[0][N] << endl;
  return 0;
}
Problem p01606 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

inline vector<int> factorize(int x) {
  vector<int> ret;

  for (int i = 1; i * i <= x; i++) {
    if (x % i == 0) {
      ret.push_back(i);
      if (i * i != x)
        ret.push_back(x / i);
    }
  }

  return (ret);
}

int main() {
  int N, W;
  int cnt = 0;
  bool manage[100001] = {0};

  scanf("%d %d", &N, &W);

  for (int i = 1; i <= W - 1; i++) {
    manage[i] = true;
    cnt++;
  }

  for (int i = W; i <= N; i++) {
    vector<int> v;

    if (i - W) {
      v = factorize(i - W);

      for (int j = 0; j < v.size(); j++) {
        int mini = (i - W + v[j]) / v[j], maxi = i / v[j];
        if (mini == 1 || maxi - mini >= 1)
          continue;
        else {
          cnt -= manage[v[j]];
          manage[v[j]] = false;
        }
      }
    }

    v = factorize(i);

    for (int j = 0; j < v.size(); j++) {
      int mini = (i - W + v[j]) / v[j], maxi = i / v[j];
      if (mini == 1 || maxi - mini >= 1) {
        cnt += !manage[v[j]];
        manage[v[j]] = true;
      }
    }

    printf("%d%c", cnt, " \\n"[i == N]);
  }

  return (0);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

inline vector<int> factorize(int x) {
  vector<int> ret;

  for (int i = 1; i * i <= x; i++) {
    ret.push_back(i);
    if (i * i != x)
      ret.push_back(x / i);
  }

  return (ret);
}

int main() {
  int N, W;
  int cnt = 0;
  bool manage[100001] = {0};

  scanf("%d %d", &N, &W);

  for (int i = 1; i <= W - 1; i++) {
    manage[i] = true;
    cnt++;
  }

  for (int i = W; i <= N; i++) {
    vector<int> v;

    if (i - W) {
      v = factorize(i - W);

      for (int j = 0; j < v.size(); j++) {
        int mini = (i - W + v[j]) / v[j], maxi = i / v[j];
        if (mini == 1 || maxi - mini >= 1)
          continue;
        else {
          cnt -= manage[v[j]];
          manage[v[j]] = false;
        }
      }
    }

    v = factorize(i);

    for (int j = 0; j < v.size(); j++) {
      int mini = (i - W + v[j]) / v[j], maxi = i / v[j];
      if (mini == 1 || maxi - mini >= 1) {
        cnt += !manage[v[j]];
        manage[v[j]] = true;
      }
    }

    printf("%d%c", cnt, " \\n"[i == N]);
  }

  return (0);
}
Problem p01606 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, w;
  cin >> n >> w;

  vector<int> a(2 * n, 0);
  for (int i = 1; i <= w / 2; ++i)
    ++a[i + 1];
  for (int i = w / 2 + 1; i < w; ++i) {
    for (int j = 3 * i; j <= n; j += i) {
      ++a[j - w + 1];
      --a[j - i + 1];
    }
  }

  int ret = w;
  for (int i = 1; i <= n - w + 1; ++i) {
    ret += a[i];
    cout << ret;
    if (i < n - w + 1)
      cout << ' ';
    else
      cout << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, w;
  cin >> n >> w;

  vector<int> a(n + 2, 0);
  for (int i = 1; i <= w / 2; ++i)
    ++a[i + 1];
  for (int i = w / 2 + 1; i < w; ++i) {
    for (int j = 3 * i; j <= n; j += i) {
      ++a[j - w + 1];
      --a[j - i + 1];
    }
  }

  int ret = w;
  for (int i = 1; i <= n - w + 1; ++i) {
    ret += a[i];
    cout << ret;
    if (i < n - w + 1)
      cout << ' ';
    else
      cout << endl;
  }

  return 0;
}
Problem p01606 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

inline vector<int> getDivisors(int n) {
  vector<int> v;
  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      v.push_back(i);
      if (i * i != n)
        v.push_back(n / i);
    }
  }
  return v;
}
int S[100010] = {};
int C[100010] = {};

int add(int x, int a) {
  for (; x < 100010; x += x & -x) {
    C[x] += a;
  }
}
int get(int x) {
  int ans = 0;
  for (; x > 0; x -= x & -x) {
    ans += C[x];
  }
  return ans;
}

int main() {
  int N, W;
  cin >> N >> W;
  for (int i = 1; i <= W; i++) {
    vector<int> g = getDivisors(i);
    for (int j = 0; j < g.size(); j++) {
      if (S[g[j]] == 1)
        add(g[j], 1);
      S[g[j]]++;
    }
  }

  int o = 0;
  cout << W;
  for (int i = 1; i <= N - W; i++) {
    vector<int> g = getDivisors(i);
    for (int j = 0; j < g.size(); j++) {
      if (S[g[j]] == 2)
        add(g[j], -1);
      S[g[j]]--;
    }
    g = getDivisors(i + W);
    for (int j = 0; j < g.size(); j++) {
      if (S[g[j]] == 1)
        add(g[j], 1);
      S[g[j]]++;
    }
    int o = 0;
    cout << " " << get(i) + W;
  }
  cout << endl;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

vector<int> getDivisors(int n) {
  vector<int> v;
  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      v.push_back(i);
      if (i * i != n)
        v.push_back(n / i);
    }
  }
  return v;
}
int S[100010] = {};
int C = 0;
int main() {
  int N, W;
  cin >> N >> W;
  for (int i = 1; i <= W; i++) {
    vector<int> g = getDivisors(i);
    for (int j = 0; j < g.size(); j++) {
      if (S[g[j]] == 1)
        C++;
      S[g[j]]++;
    }
  }

  int o = 0;
  cout << W;
  for (int i = 1; i <= N - W; i++) {
    vector<int> g = getDivisors(i);
    for (int j = 0; j < g.size(); j++) {
      if (S[g[j]] == 2)
        C--;
      S[g[j]]--;
    }
    g = getDivisors(i + W);
    for (int j = 0; j < g.size(); j++) {
      if (S[g[j]] == 1)
        C++;
      S[g[j]]++;
    }
    int o = 0;
    for (int k = 0; k < 100010; k++)
      if (S[k] >= 2 && k <= i)
        o++;
    cout << " " << o + W;
  }
  cout << endl;
}
Problem p01606 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = int(s); i <= int(e); i++)
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define pi 3.14159265358979

int a[100010];

int main(void) {

  int n, w;
  cin >> n >> w;

  for (int i = 1; i <= w / 2; i++)
    a[i] = w - 1 + i;

  for (int i = w / 2 + 1; i <= n - w + 1; i++)
    a[i] = w + w / 2;

  for (int j = w / 2 + 1; j <= w - 1; j++) {
    for (int k = 3 * j; k <= n; k += j) {
      for (int i = k - w + 1; i <= k - j; i++)
        a[i] += 1;
    }
  }

  for (int i = 1; i <= n - w + 1; i++) {
    if (i != n - w + 1)
      cout << a[i] << ' ';
    else
      cout << a[i] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = int(s); i <= int(e); i++)
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define pi 3.14159265358979

int main(void) {

  int n, w;
  cin >> n >> w;

  vector<int> v;

  for (int i = 1; i <= n - w + 1; i++) {

    if (i <= w / 2)
      v.push_back(w - 1 + i);

    int c = w + w / 2;
    if (i >= w / 2 + 1 && i <= w - 1) {
      for (int j = w / 2 + 1; j <= i - 1; j++) {
        if (i <= 2 * j && 3 * j <= w - 1 + i)
          c++;
      }

      v.push_back(c);
    }

    if (i >= w && i <= n - w + 1) {
      c = 0;
      for (int k = w / 2 + 1; k <= w - 1; k++) {
        if (i % k == 0) {
          if ((i / k + 1) * k <= w - 1 + i)
            c++;
        } else if ((i / k + 2) * k <= w - 1 + i)
          c++;
      }

      v.push_back(w + w / 2 + c);
    }
  }

  int s = v.size();
  for (int i = 0; i < s; i++) {
    if (i != s - 1)
      cout << v[i] << ' ';
    else
      cout << v[i] << endl;
  }

  return 0;
}