prithivMLmods/Coder-Stat huggingface dataset:
Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int MAX = 1280;

int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<int>> A(H, vector<int>(W));
  auto B = A;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> A[i][j];
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> B[i][j];
      A[i][j] = abs(A[i][j] - B[i][j]);
    }
  }
  vector<vector<vector<bool>>> dp(
      H, vector<vector<bool>>(W, vector<bool>(MAX + 1)));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (i == 0 && j == 0) {
        dp[i][j][A[i][j]] = true;
        continue;
      }
      if (0 <= i - 1) {
        for (int k = 0; k <= MAX; k++)
          if (dp[i - 1][j][k]) {
            dp[i][j][(k + A[i][j])] = true;
            dp[i][j][abs(k - A[i][j])] = true;
          }
      }
      if (0 <= j - 1) {
        for (int k = 0; k <= MAX; k++)
          if (dp[i][j - 1][k]) {
            dp[i][j][(k + A[i][j])] = true;
            dp[i][j][abs(k - A[i][j])] = true;
          }
      }
    }
  }
  int res = INT_MAX;
  for (int i = 0; i <= MAX; i++)
    if (dp[H - 1][W - 1][i]) {
      res = i;
      break;
    }
  cout << res << endl;
  return 0;
}


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

const int MAX = 12800;

int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<int>> A(H, vector<int>(W));
  auto B = A;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> A[i][j];
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> B[i][j];
      A[i][j] = abs(A[i][j] - B[i][j]);
    }
  }
  vector<vector<vector<bool>>> dp(
      H, vector<vector<bool>>(W, vector<bool>(MAX + 1)));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (i == 0 && j == 0) {
        dp[i][j][A[i][j]] = true;
        continue;
      }
      if (0 <= i - 1) {
        for (int k = 0; k <= MAX; k++)
          if (dp[i - 1][j][k]) {
            dp[i][j][(k + A[i][j])] = true;
            dp[i][j][abs(k - A[i][j])] = true;
          }
      }
      if (0 <= j - 1) {
        for (int k = 0; k <= MAX; k++)
          if (dp[i][j - 1][k]) {
            dp[i][j][(k + A[i][j])] = true;
            dp[i][j][abs(k - A[i][j])] = true;
          }
      }
    }
  }
  int res = INT_MAX;
  for (int i = 0; i <= MAX; i++)
    if (dp[H - 1][W - 1][i]) {
      res = i;
      break;
    }
  cout << res << endl;
  return 0;
}
Problem p02839 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int table[80][80];
bool dp[80][80][15000] = {};

int main() {
  int H, W;
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> table[i][j];
    }
  }
  int m = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int b;
      cin >> b;
      table[i][j] = abs(table[i][j] - b);
      m = max(m, table[i][j]);
    }
  }
  dp[0][0][table[0][0]] = true;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      for (int k = 0; k <= (H * W) * m; k++) {
        if (dp[i][j][k]) {
          if (j < W - 1) {
            dp[i][j + 1][k + table[i][j + 1]] = true;
            dp[i][j + 1][abs(k - table[i][j + 1])] = true;
          }
          if (i < H - 1) {
            dp[i + 1][j][k + table[i + 1][j]] = true;
            dp[i + 1][j][abs(k - table[i + 1][j])] = true;
          }
        }
      }
    }
  }
  for (int k = 0; k <= (H + W) * m; k++) {
    if (dp[H - 1][W - 1][k]) {
      cout << k << endl;
      break;
    }
  }
  return 0;
}

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

int table[80][80];
bool dp[80][80][15000] = {};

int main() {
  int H, W;
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> table[i][j];
    }
  }
  int m = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int b;
      cin >> b;
      table[i][j] = abs(table[i][j] - b);
      m = max(m, table[i][j]);
    }
  }
  dp[0][0][table[0][0]] = true;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      for (int k = 0; k <= (H + W) * m; k++) {
        if (dp[i][j][k]) {
          if (j < W - 1) {
            dp[i][j + 1][k + table[i][j + 1]] = true;
            dp[i][j + 1][abs(k - table[i][j + 1])] = true;
          }
          if (i < H - 1) {
            dp[i + 1][j][k + table[i + 1][j]] = true;
            dp[i + 1][j][abs(k - table[i + 1][j])] = true;
          }
        }
      }
    }
  }
  for (int k = 0; k <= (H + W) * m; k++) {
    if (dp[H - 1][W - 1][k]) {
      cout << k << endl;
      break;
    }
  }
  return 0;
}
Problem p02839 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define rep(i, n) for (int(i) = 0; i < (n); ++(i))

typedef long long ll;
const ll MOD = 1e9 + 7;

int main() {
  int H, W;
  std::cin >> H >> W;

  static std::vector<std::vector<int>> A(H, std::vector<int>(W));
  static std::vector<std::vector<int>> B(H, std::vector<int>(W));
  rep(i, H) {
    rep(j, W) { std::cin >> A[i][j]; }
  }
  rep(i, H) {
    rep(j, W) { std::cin >> B[i][j]; }
  }

  int M = 80 * (H + W);
  std::vector<std::vector<std::map<int, bool>>> dp(
      H, std::vector<std::map<int, bool>>(W));
  dp[0][0][std::abs(A[0][0] - B[0][0])] = true;

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      for (int k = 0; k <= M; ++k) {
        if (!dp[i][j][k])
          continue;
        if (i + 1 < H) {
          int d = std::abs(A[i + 1][j] - B[i + 1][j]);
          dp[i + 1][j][k + d] = dp[i + 1][j][std::abs(k - d)] = true;
        }
        if (j + 1 < W) {
          int d = std::abs(A[i][j + 1] - B[i][j + 1]);
          dp[i][j + 1][k + d] = dp[i][j + 1][std::abs(k - d)] = true;
        }
      }
    }
  }

  int ans;
  for (int k = 0; k <= M; ++k) {
    if (dp[H - 1][W - 1][k]) {
      ans = k;
      break;
    }
  }
  std::cout << ans << '\n';
}


Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int(i) = 0; i < (n); ++(i))

typedef long long ll;
const ll MOD = 1e9 + 7;

int main() {
  int H, W;
  std::cin >> H >> W;

  static std::vector<std::vector<int>> A(H, std::vector<int>(W));
  static std::vector<std::vector<int>> B(H, std::vector<int>(W));
  rep(i, H) {
    rep(j, W) { std::cin >> A[i][j]; }
  }
  rep(i, H) {
    rep(j, W) { std::cin >> B[i][j]; }
  }

  int M = 80 * (H + W);
  std::vector<std::vector<std::vector<bool>>> dp(
      H, std::vector<std::vector<bool>>(W));
  rep(i, H) {
    rep(j, W) { dp[i][j] = std::vector<bool>(M + 1); }
  }
  dp[0][0][std::abs(A[0][0] - B[0][0])] = true;

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      for (int k = 0; k <= M; ++k) {
        if (!dp[i][j][k])
          continue;
        if (i + 1 < H) {
          int d = std::abs(A[i + 1][j] - B[i + 1][j]);
          dp[i + 1][j][k + d] = dp[i + 1][j][std::abs(k - d)] = true;
        }
        if (j + 1 < W) {
          int d = std::abs(A[i][j + 1] - B[i][j + 1]);
          dp[i][j + 1][k + d] = dp[i][j + 1][std::abs(k - d)] = true;
        }
      }
    }
  }

  int ans;
  for (int k = 0; k <= M; ++k) {
    if (dp[H - 1][W - 1][k]) {
      ans = k;
      break;
    }
  }
  std::cout << ans << '\n';
}
Problem p02839 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;
typedef long long ll;
bool dp[81][81][80 * 160 + 200];
int a[80][80];
int b[80][80];
int main() {
  int h, w;
  cin >> h >> w;
  rep(i, h) {
    rep(j, w) { cin >> a[i][j]; }
  }
  rep(i, h) {
    rep(j, w) { cin >> b[i][j]; }
  }
  dp[0][0][abs(a[0][0] - b[0][0])] = true;
  rep(i, h) {
    rep(j, w) {
      rep(k, 80 * 80 * 80) {
        if (dp[i][j][k] == false) {
          continue;
        }
        if (j == w - 1 && i == h - 1) {
          continue;
        } else if (j == w - 1) {
          dp[i + 1][j][k + abs(a[i + 1][j] - b[i + 1][j])] = true;
          dp[i + 1][j][abs(k - abs(a[i + 1][j] - b[i + 1][j]))] = true;
        } else if (i == h - 1) {
          dp[i][j + 1][k + abs(a[i][j + 1] - b[i][j + 1])] = true;
          dp[i][j + 1][abs(k - abs(a[i][j + 1] - b[i][j + 1]))] = true;
        } else {
          dp[i + 1][j][k + abs(a[i + 1][j] - b[i + 1][j])] = true;
          dp[i + 1][j][abs(k - abs(a[i + 1][j] - b[i + 1][j]))] = true;
          dp[i][j + 1][k + abs(a[i][j + 1] - b[i][j + 1])] = true;
          dp[i][j + 1][abs(k - abs(a[i][j + 1] - b[i][j + 1]))] = true;
        }
      }
    }
  }
  for (int i = 0; i < 80 * 160 + 200; i++) {
    if (dp[h - 1][w - 1][i]) {
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;
typedef long long ll;
bool dp[81][81][80 * 160 + 200];
int a[80][80];
int b[80][80];
int main() {
  int h, w;
  cin >> h >> w;
  rep(i, h) {
    rep(j, w) { cin >> a[i][j]; }
  }
  rep(i, h) {
    rep(j, w) { cin >> b[i][j]; }
  }
  dp[0][0][abs(a[0][0] - b[0][0])] = true;
  rep(i, h) {
    rep(j, w) {
      rep(k, 80 * 160 + 1) {
        if (dp[i][j][k] == false) {
          continue;
        }
        if (j == w - 1 && i == h - 1) {
          continue;
        } else if (j == w - 1) {
          dp[i + 1][j][k + abs(a[i + 1][j] - b[i + 1][j])] = true;
          dp[i + 1][j][abs(k - abs(a[i + 1][j] - b[i + 1][j]))] = true;
        } else if (i == h - 1) {
          dp[i][j + 1][k + abs(a[i][j + 1] - b[i][j + 1])] = true;
          dp[i][j + 1][abs(k - abs(a[i][j + 1] - b[i][j + 1]))] = true;
        } else {
          dp[i + 1][j][k + abs(a[i + 1][j] - b[i + 1][j])] = true;
          dp[i + 1][j][abs(k - abs(a[i + 1][j] - b[i + 1][j]))] = true;
          dp[i][j + 1][k + abs(a[i][j + 1] - b[i][j + 1])] = true;
          dp[i][j + 1][abs(k - abs(a[i][j + 1] - b[i][j + 1]))] = true;
        }
      }
    }
  }
  for (int i = 0; i < 80 * 160 + 200; i++) {
    if (dp[h - 1][w - 1][i]) {
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}
Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

bool d[81][81][12900];

int main() {
  int h, w;
  cin >> h >> w;
  int a[81][81], b[81][81];
  int c[81][81];
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> a[i][j];
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> b[i][j];
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      c[i][j] = abs(a[i][j] - b[i][j]);
  d[0][0][c[0][0]] = true;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (int k = 0; k < 12800; k++) {
        if (d[i][j][k]) {
          d[i + 1][j][k + c[i + 1][j]] = true;
          d[i + 1][j][abs(k - c[i + 1][j])] = true;
          d[i][j + 1][k + c[i][j + 1]] = true;
          d[i][j + 1][abs(k - c[i][j + 1])] = true;
        }
      }
    }
  }
  for (int i = 0; i < 12800; i++) {
    if (d[h - 1][w - 1][i]) {
      cout << i << endl;
      return 0;
    }
  }
}

Fixed submission:
#include <iostream>
using namespace std;

bool d[81][81][12900];

int main() {
  int h, w;
  cin >> h >> w;
  int a[81][81], b[81][81];
  int c[81][81];
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> a[i][j];
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> b[i][j];
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      c[i][j] = abs(a[i][j] - b[i][j]);
  d[0][0][c[0][0]] = true;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (int k = 0; k < 12800; k++) {
        if (d[i][j][k]) {
          if (i < h - 1) {
            d[i + 1][j][k + c[i + 1][j]] = true;
            d[i + 1][j][abs(k - c[i + 1][j])] = true;
          }
          if (j < w - 1) {
            d[i][j + 1][k + c[i][j + 1]] = true;
            d[i][j + 1][abs(k - c[i][j + 1])] = true;
          }
        }
      }
    }
  }
  for (int i = 0; i < 12800; i++) {
    if (d[h - 1][w - 1][i]) {
      cout << i << endl;
      return 0;
    }
  }
}
Problem p02839 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int dp[81][81][6400 * 2 + 2];
int find_it(int dif_sum, int i, int j, int &n, int &m,
            vector<vector<pair<int, int>>> &matrix) {
  if (i == n - 1 && j == m - 1) {
    return min(abs(dif_sum + matrix[i][j].first - matrix[i][j].second),
               abs(dif_sum + matrix[i][j].second - matrix[i][j].first));
  }
  if (dp[i][j][dif_sum] != -1) {
    return dp[i][j][dif_sum + 6401];
  }
  int minvalue = INT_MAX;
  int ff = dif_sum + matrix[i][j].first - matrix[i][j].second;
  int ss = dif_sum + matrix[i][j].second - matrix[i][j].first;
  if (i + 1 < n) {
    minvalue = min(minvalue, find_it(ff, i + 1, j, n, m, matrix));
    minvalue = min(minvalue, find_it(ss, i + 1, j, n, m, matrix));
  }
  if (j + 1 < m) {
    minvalue = min(minvalue, find_it(ff, i, j + 1, n, m, matrix));
    minvalue = min(minvalue, find_it(ss, i, j + 1, n, m, matrix));
  }
  return dp[i][j][dif_sum + 6401] = minvalue;
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, int>>> matrix(n, vector<pair<int, int>>(m));

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> matrix[i][j].first;
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> matrix[i][j].second;
    }
  }
  memset(dp, -1, sizeof(dp));
  cout << find_it(0, 0, 0, n, m, matrix);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[81][81][6400 * 2 + 2];
int find_it(int dif_sum, int i, int j, int &n, int &m,
            vector<vector<pair<int, int>>> &matrix) {
  if (i == n - 1 && j == m - 1) {
    return min(abs(dif_sum + matrix[i][j].first - matrix[i][j].second),
               abs(dif_sum + matrix[i][j].second - matrix[i][j].first));
  }
  if (dp[i][j][dif_sum + 6401] != -1) {
    return dp[i][j][dif_sum + 6401];
  }
  int minvalue = INT_MAX;
  int ff = dif_sum + matrix[i][j].first - matrix[i][j].second;
  int ss = dif_sum + matrix[i][j].second - matrix[i][j].first;
  if (i + 1 < n) {
    minvalue = min(minvalue, find_it(ff, i + 1, j, n, m, matrix));
    minvalue = min(minvalue, find_it(ss, i + 1, j, n, m, matrix));
  }
  if (j + 1 < m) {
    minvalue = min(minvalue, find_it(ff, i, j + 1, n, m, matrix));
    minvalue = min(minvalue, find_it(ss, i, j + 1, n, m, matrix));
  }
  return dp[i][j][dif_sum + 6401] = minvalue;
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, int>>> matrix(n, vector<pair<int, int>>(m));

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> matrix[i][j].first;
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> matrix[i][j].second;
    }
  }
  memset(dp, -1, sizeof(dp));
  cout << find_it(0, 0, 0, n, m, matrix);

  return 0;
}
Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> P;
const LL mod = 1000000007;
const LL LINF = 1LL << 60;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<int>> a(h, vector<int>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int b;
      cin >> b;
      a[i][j] = abs(a[i][j] - b);
    }
  }
  bitset<30000> bi[h][w];
  bi[0][0][15000] = 1;
  for (int k = 0; k <= h + w - 2; k++) {
    for (int i = max(0, k - h + 1); i <= min(w - 1, k); i++) {
      int x = i, y = k - i;
      bi[y + 1][x] = bi[y + 1][x] | (bi[y][x] >> a[y][x]);
      bi[y + 1][x] = bi[y + 1][x] | (bi[y][x] << a[y][x]);
      bi[y][x + 1] = bi[y][x + 1] | (bi[y][x] >> a[y][x]);
      bi[y][x + 1] = bi[y][x + 1] | (bi[y][x] << a[y][x]);
    }
  }
  int ans = INF;
  for (int i = 0; i < 30000; i++) {
    if (bi[h][w - 1][i])
      ans = min(ans, abs(15000 - i));
  }
  for (int i = 0; i < 30000; i++) {
    if (bi[h - 1][w][i])
      ans = min(ans, abs(15000 - i));
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> P;
const LL mod = 1000000007;
const LL LINF = 1LL << 60;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<int>> a(h, vector<int>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int b;
      cin >> b;
      a[i][j] = abs(a[i][j] - b);
    }
  }
  bitset<30000> bi[h + 1][w + 1];
  bi[0][0][15000] = 1;
  for (int k = 0; k <= h + w - 2; k++) {
    for (int i = max(0, k - h + 1); i <= min(w - 1, k); i++) {
      int x = i, y = k - i;
      bi[y + 1][x] = bi[y + 1][x] | (bi[y][x] >> a[y][x]);
      bi[y + 1][x] = bi[y + 1][x] | (bi[y][x] << a[y][x]);
      bi[y][x + 1] = bi[y][x + 1] | (bi[y][x] >> a[y][x]);
      bi[y][x + 1] = bi[y][x + 1] | (bi[y][x] << a[y][x]);
    }
  }
  int ans = INF;
  for (int i = 0; i < 30000; i++) {
    if (bi[h][w - 1][i])
      ans = min(ans, abs(15000 - i));
  }
  for (int i = 0; i < 30000; i++) {
    if (bi[h - 1][w][i])
      ans = min(ans, abs(15000 - i));
  }
  cout << ans << endl;
  return 0;
}
Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
map<int, vector<pair<long long, long long>>> l;
long long sum(long long x) { return x * (x + 1) / 2; }
int main() {
  long long n, x, d, ans = 0;
  cin >> n >> x >> d;
  if (!d) {
    if (!x)
      cout << 1;
    else
      cout << n + 1;
  }
  for (int i = 0; i <= n; i++) {
    long long a = (i * x) % abs(d), cur = (i * x - a) / d;
    l[a].push_back({sum(i - 1) + cur, sum(n - 1) - sum(n - i - 1) + cur});
  }
  for (auto v : l) {
    auto cur = v.second;
    sort(cur.begin(), cur.end());
    long long r = -1e18;
    for (auto p : cur) {
      ans += max(p.second - max(r, p.first) + 1, 0LL);
      r = max(r, p.second + 1);
    }
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
map<int, vector<pair<long long, long long>>> l;
long long sum(long long x) { return x * (x + 1) / 2; }
int main() {
  long long n, x, d, ans = 0;
  cin >> n >> x >> d;
  if (!d) {
    if (!x)
      cout << 1;
    else
      cout << n + 1;
    return 0;
  }
  for (int i = 0; i <= n; i++) {
    long long a = (i * x) % abs(d), cur = (i * x - a) / d;
    l[a].push_back({sum(i - 1) + cur, sum(n - 1) - sum(n - i - 1) + cur});
  }
  for (auto v : l) {
    auto cur = v.second;
    sort(cur.begin(), cur.end());
    long long r = -1e18;
    for (auto p : cur) {
      ans += max(p.second - max(r, p.first) + 1, 0LL);
      r = max(r, p.second + 1);
    }
  }
  cout << ans;
}
Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using PP = pair<long, long>;
int n;
long a, b;
unordered_map<long, vector<PP>> mp;

int main() {
  cin >> n >> b >> a;
  if (a < 0) {
    a = -a;
    b = -b;
  }

  long mn = 0;
  long mx = 0;
  for (int i = 0; i <= n; ++i) {
    long k = b * i % a;
    mp[k].push_back(PP(mn, mx));
    mn += a * i + b;
    mx += a * (n - 1 - i) + b;
  }

  long sum = 0;
  for (auto &&q : mp) {
    vector<PP> ps = q.second;
    sort(ps.begin(), ps.end());

    vector<PP> nps;
    const long inf = 1e18;
    long pl = -inf;
    long pr = -inf;
    for (PP p : ps) {
      if (p.second <= pr)
        continue;
      if (p.first > pr) {
        if (pr > -inf)
          nps.push_back(PP(pl, pr));
        pl = p.first;
      }
      pr = p.second;
    }
    nps.push_back(PP(pl, pr));

    for (PP p : nps) {
      long l = p.first;
      long r = p.second;
      sum += a == 0 ? 1 : (r - l) / a + 1;
    }
  }
  cout << sum << endl;
}

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

using PP = pair<long, long>;
int n;
long a, b;
unordered_map<long, vector<PP>> mp;

int main() {
  cin >> n >> b >> a;
  if (a < 0) {
    a = -a;
    b = -b;
  }

  long mn = 0;
  long mx = 0;
  for (int i = 0; i <= n; ++i) {
    long k = a == 0 ? 0 : b * i % a;
    mp[k].push_back(PP(mn, mx));
    mn += a * i + b;
    mx += a * (n - 1 - i) + b;
  }

  long sum = 0;
  for (auto &&q : mp) {
    vector<PP> ps = q.second;
    sort(ps.begin(), ps.end());

    vector<PP> nps;
    const long inf = 1e18;
    long pl = -inf;
    long pr = -inf;
    for (PP p : ps) {
      if (p.second <= pr)
        continue;
      if (p.first > pr) {
        if (pr > -inf)
          nps.push_back(PP(pl, pr));
        pl = p.first;
      }
      pr = p.second;
    }
    nps.push_back(PP(pl, pr));

    for (PP p : nps) {
      long l = p.first;
      long r = p.second;
      sum += a == 0 ? 1 : (r - l) / a + 1;
    }
  }
  cout << sum << endl;
}
Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#ifdef DGC
#include "debug.h"
#else
#define debug(...) 9715
#endif
typedef long long ll;
typedef long double ld;
typedef complex<ld> point;
#define F first
#define S second

int main() {
#ifdef DGC
  // freopen("a.in", "r", stdin);
  // freopen("b.out", "w", stdout);
#endif

  ios_base::sync_with_stdio(0), cin.tie(0);

  ll n, x, d;
  cin >> n >> x >> d;

  map<ll, vector<pair<ll, ll>>> mp;
  for (ll k = 0; k <= n; ++k)
    mp[k * x % d].push_back({k * x / d + k * (k - 1) / 2,
                             k * x / d + (k * (n - 1) - k * (k - 1) / 2)});

  ll ans = 0;
  for (auto &i : mp) {
    auto &a = i.S;
    sort(a.begin(), a.end());
    auto cur = a[0];
    a.push_back({(ll)1e18, 0});
    for (auto i : a) {
      if (i.F <= cur.S)
        cur.S = max(cur.S, i.S);
      else
        ans += cur.S - cur.F + 1, cur = i;
    }
  }

  cout << ans << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#ifdef DGC
#include "debug.h"
#else
#define debug(...) 9715
#endif
typedef long long ll;
typedef long double ld;
typedef complex<ld> point;
#define F first
#define S second

int main() {
#ifdef DGC
  // freopen("a.in", "r", stdin);
  // freopen("b.out", "w", stdout);
#endif

  ios_base::sync_with_stdio(0), cin.tie(0);

  ll n, x, d;
  cin >> n >> x >> d;

  if (d == 0) {
    if (x == 0)
      cout << "1\n";
    else
      cout << n + 1 << "\n";
    return 0;
  }

  map<ll, vector<pair<ll, ll>>> mp;
  for (ll k = 0; k <= n; ++k)
    mp[k * x % d].push_back({k * x / d + k * (k - 1) / 2,
                             k * x / d + (k * (n - 1) - k * (k - 1) / 2)});

  ll ans = 0;
  for (auto &i : mp) {
    auto &a = i.S;
    sort(a.begin(), a.end());
    auto cur = a[0];
    a.push_back({(ll)1e18, 0});
    for (auto i : a) {
      if (i.F <= cur.S)
        cur.S = max(cur.S, i.S);
      else
        ans += cur.S - cur.F + 1, cur = i;
    }
  }

  cout << ans << "\n";

  return 0;
}
Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath> //ABC 147-F
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define LL long long
#define MAXN 400001
#define MOD 1000000007ll
using namespace std;
LL N, X, D;
int stk[MAXN], sum;
bool vis[MAXN];
LL gcd(LL x, LL y) { return y ? gcd(y, x % y) : x; }
struct DB {
  LL v;
  int kind, num;
} p[MAXN];
bool comp(DB x, DB y) { return x.v == y.v ? x.kind < y.kind : x.v < y.v; }
int cnt;
LL sig(LL x) { return x < 0 ? -1 : 1; }
int main() {
  cin >> N >> D >> X;
  // D*=2;
  LL G = gcd(X, D);
  LL B = abs(X / G);
  LL A = abs(D / G);
  LL ANS = 0;
  for (LL i = 0; i < B && i <= N; i++) {
    cnt = 0;
    for (LL j = 0; j + i <= N; j += B) {
      LL U = (j + i) * (j + i - 1) / 2 + j / B * A * sig(X * D),
         V = (i + j) * (N + N - j - i - 1) / 2 + j / B * A * sig(X * D);

      cnt++;
      p[cnt].kind = 0;
      p[cnt].v = min(U, V);
      p[cnt].num = j / B;

      cnt++;
      p[cnt].kind = 1;
      p[cnt].v = max(U, V);
      p[cnt].num = j / B;
    }

    sort(p + 1, p + cnt + 1, comp);
    p[0].v = p[1].v;
    sum = 0;
    for (int j = 1; j <= cnt; j++) {
      if (sum) {
        ANS += (p[j].v - p[j - 1].v);
      } else
        ANS++;
      if (p[j].kind == 0) {
        sum++;

      } else {
        sum--;
      }
    }
    // ANS++;
  }
  cout << ANS << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath> //ABC 147-F
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define LL long long
#define MAXN 400001
#define MOD 1000000007ll
using namespace std;
LL N, X, D;
int stk[MAXN], sum;
bool vis[MAXN];
LL gcd(LL x, LL y) { return y ? gcd(y, x % y) : x; }
struct DB {
  LL v;
  int kind, num;
} p[MAXN];
bool comp(DB x, DB y) { return x.v == y.v ? x.kind < y.kind : x.v < y.v; }
int cnt;
LL sig(LL x) { return x < 0 ? -1 : 1; }
int main() {
  cin >> N >> D >> X;
  // D*=2;
  if (X == 0 && D == 0) {
    cout << 1 << endl;
    return 0;
  }
  if (X == 0) {
    cout << N + 1 << endl;
    return 0;
  }
  LL G = gcd(X, D);
  LL B = abs(X / G);
  LL A = abs(D / G);
  LL ANS = 0;
  for (LL i = 0; i < B && i <= N; i++) {
    cnt = 0;
    for (LL j = 0; j + i <= N; j += B) {
      LL U = (j + i) * (j + i - 1) / 2 + j / B * A * sig(X * D),
         V = (i + j) * (N + N - j - i - 1) / 2 + j / B * A * sig(X * D);

      cnt++;
      p[cnt].kind = 0;
      p[cnt].v = min(U, V);
      p[cnt].num = j / B;

      cnt++;
      p[cnt].kind = 1;
      p[cnt].v = max(U, V);
      p[cnt].num = j / B;
    }

    sort(p + 1, p + cnt + 1, comp);
    p[0].v = p[1].v;
    sum = 0;
    for (int j = 1; j <= cnt; j++) {
      if (sum) {
        ANS += (p[j].v - p[j - 1].v);
      } else
        ANS++;
      if (p[j].kind == 0) {
        sum++;

      } else {
        sum--;
      }
    }
    // ANS++;
  }
  cout << ANS << endl;

  return 0;
}
Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define rep(i, n) for (int(i) = 0; i < (n); ++(i))

typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 62;

int main() {
  int N;
  ll X, D;
  std::cin >> N >> X >> D;

  std::map<ll, std::vector<std::pair<ll, ll>>> ranges;

  for (ll k = 0; k <= N; ++k) {
    ll L = k * (k - 1) / 2;
    ll R = k * (2 * N - k - 1) / 2;
    if (L > R) {
      std::swap(L, R);
    }

    ll r = X * k % D;
    ll n = (X * k) / D;
    if (r < 0)
      r -= D;
    ranges[r].push_back(std::make_pair(n + L, n + R + 1));
  }

  ll ans = 0;
  for (auto &p : ranges) {
    auto &range_vec = p.second;

    ll last = -INF;
    std::sort(range_vec.begin(), range_vec.end());
    for (auto range : range_vec) {
      ll L = range.first;
      ll R = range.second;

      if (R <= last)
        continue;
      L = std::max(L, last);
      ans += R - L;
      last = R;
    }
  }

  std::cout << ans << '\n';
}


Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int(i) = 0; i < (n); ++(i))

typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 62;

int main() {
  int N;
  ll X, D;
  std::cin >> N >> X >> D;

  if (D == 0) {
    if (X == 0) {
      std::cout << "1" << '\n';
      return 0;
    } else {
      std::cout << N + 1 << '\n';
      return 0;
    }
  }

  std::map<ll, std::vector<std::pair<ll, ll>>> ranges;

  for (ll k = 0; k <= N; ++k) {
    ll L = k * (k - 1) / 2;
    ll R = k * (2 * N - k - 1) / 2;
    if (L > R) {
      std::swap(L, R);
    }

    ll r = X * k % D;
    ll n = (X * k) / D;
    if (r < 0)
      r -= D;
    ranges[r].push_back(std::make_pair(n + L, n + R + 1));
  }

  ll ans = 0;
  for (auto &p : ranges) {
    auto &range_vec = p.second;

    ll last = -INF;
    std::sort(range_vec.begin(), range_vec.end());
    for (auto range : range_vec) {
      ll L = range.first;
      ll R = range.second;

      if (R <= last)
        continue;
      L = std::max(L, last);
      ans += R - L;
      last = R;
    }
  }

  std::cout << ans << '\n';
}
Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using LL = long long;
using PLL = pair<LL, LL>;
const LL INF = 1e18;
LL sum(LL n) { return (n * (n - 1)) / 2; }

int main() {
#ifdef LOCAL_EXEC
//	freopen("sample.in", "r", stdin);
//	freopen("sample.out", "w", stdout);
#else
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#endif

  LL n, x, d;
  cin >> n >> x >> d;
  LL ad = llabs(d);

  vector<pair<LL, PLL>> ranges;
  ranges.emplace_back(0, PLL(0, 0));
  for (int i = 1; i <= n; ++i) {
    LL rem = (i * x) % ad;
    LL q = (i * x - rem) / d;
    LL lo = sum(i);
    LL hi = sum(n) - sum(n - i);
    ranges.emplace_back(rem, PLL{q + lo, q + hi});
  }
  sort(ranges.begin(), ranges.end());
  LL prv = -1;
  LL ans = 0, lo = -INF, hi = -INF - 10;
  for (auto &e : ranges) {
    if (e.first != prv) {
      ans += max(0ll, hi - lo + 1);
      lo = -INF, hi = -INF - 10;
      prv = e.first;
    }

    LL clo, chi;
    tie(clo, chi) = e.second;
    if (clo > hi) {
      ans += max(0ll, hi - lo + 1);
      lo = clo, hi = chi;
    } else {
      hi = max(hi, chi);
    }
  }
  ans += max(0ll, hi - lo + 1);
  cout << ans << endl;

  return 0;
}


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

using LL = long long;
using PLL = pair<LL, LL>;
const LL INF = 1e18;
LL sum(LL n) { return (n * (n - 1)) / 2; }

int main() {
#ifdef LOCAL_EXEC
//	freopen("sample.in", "r", stdin);
//	freopen("sample.out", "w", stdout);
#else
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#endif

  LL n, x, d;
  cin >> n >> x >> d;
  if (d == 0) {
    if (x == 0) {
      cout << "1\n";
    } else {
      cout << n + 1 << endl;
    }
    return 0;
  }
  LL ad = llabs(d);

  vector<pair<LL, PLL>> ranges;
  ranges.emplace_back(0, PLL(0, 0));
  for (int i = 1; i <= n; ++i) {
    LL rem = (i * x) % ad;
    LL q = (i * x - rem) / d;
    LL lo = sum(i);
    LL hi = sum(n) - sum(n - i);
    ranges.emplace_back(rem, PLL{q + lo, q + hi});
  }
  sort(ranges.begin(), ranges.end());
  LL prv = -1;
  LL ans = 0, lo = -INF, hi = -INF - 10;
  for (auto &e : ranges) {
    if (e.first != prv) {
      ans += max(0ll, hi - lo + 1);
      lo = -INF, hi = -INF - 10;
      prv = e.first;
    }

    LL clo, chi;
    tie(clo, chi) = e.second;
    if (clo > hi) {
      ans += max(0ll, hi - lo + 1);
      lo = clo, hi = chi;
    } else {
      hi = max(hi, chi);
    }
  }
  ans += max(0ll, hi - lo + 1);
  cout << ans << endl;

  return 0;
}
Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll mod = 1e9 + 7;

int main() {
  ll N, X, D;
  cin >> N >> X >> D;
  ll ans = 0;
  ll p = D / gcd(X, D);
  for (ll M = 0; M <= N; M++) {
    // cout << ans << endl;
    if (M < p) {
      ans += ((N * (N - 1) / 2 - (N - M) * (N - M - 1)) +
              (N * (N - 1) / 2 - M * (M - 1))) /
                 2 +
             1;
      continue;
    }
    ll l = -N * (N - 1) / 2 + M * (M - 1);
    ll r = N * (N - 1) / 2 - (N - M) * (N - M - 1);
    ll lp = -N * (N - 1) / 2 + (M - p) * (M - p - 1) - 2 * X * p / D;
    ll rp = N * (N - 1) / 2 - (N - M + p) * (N - M + p - 1) - 2 * X * p / D;
    if (r < lp || l > rp)
      ans += (r - l) / 2 + 1;
    else {
      if (l <= rp && rp <= r)
        ans += (r - rp) / 2;
      if (l <= lp && lp <= r)
        ans += (lp - l) / 2;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll mod = 1e9 + 7;

int main() {
  ll N, X, D;
  cin >> N >> X >> D;
  if (D == 0) {
    if (X == 0)
      cout << 1 << endl;
    else
      cout << (N + 1) << endl;
    return 0;
  }
  ll ans = 0;
  ll p = D / gcd(X, D);
  for (ll M = 0; M <= N; M++) {
    // cout << ans << endl;
    if (M < p) {
      ans += ((N * (N - 1) / 2 - (N - M) * (N - M - 1)) +
              (N * (N - 1) / 2 - M * (M - 1))) /
                 2 +
             1;
      continue;
    }
    ll l = -N * (N - 1) / 2 + M * (M - 1);
    ll r = N * (N - 1) / 2 - (N - M) * (N - M - 1);
    ll lp = -N * (N - 1) / 2 + (M - p) * (M - p - 1) - 2 * X * p / D;
    ll rp = N * (N - 1) / 2 - (N - M + p) * (N - M + p - 1) - 2 * X * p / D;
    if (r < lp || l > rp)
      ans += (r - l) / 2 + 1;
    else {
      if (l <= rp && rp <= r)
        ans += (r - rp) / 2;
      if (l <= lp && lp <= r)
        ans += (lp - l) / 2;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

long long n, x, d;
long long smin[500000];
long long smax[500000];
map<int, vector<pair<long long, long long>>> st;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> x >> d;
  for (int i = 1; i <= n; i++)
    smin[i] = smin[i - 1] + i - 1;
  for (int i = 1; i <= n; i++)
    smax[i] = smax[i - 1] + n - i;
  for (int k = 0; k <= n; k++) {
    long long mn = k * x + smin[k] * d;
    long long mx = k * x + smax[k] * d;
    long long md = ((k * x) % d + d) % d;
    mn = (mn - md) / d;
    mx = (mx - md) / d;
    st[md].push_back({mn, mx});
  }
  long long ans = 0;
  for (auto vv : st) {
    vector<pair<long long, long long>> v = vv.second;
    sort(v.begin(), v.end());
    long long sm = -1e18;
    for (int i = 0; i < v.size(); i++) {
      v[i].first = max(v[i].first, sm);
      if (v[i].first > v[i].second)
        continue;
      else
        ans += v[i].second - v[i].first + 1, sm = v[i].second + 1;
    }
  }
  cout << ans;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

long long n, x, d;
long long smin[500000];
long long smax[500000];
map<int, vector<pair<long long, long long>>> st;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> x >> d;
  if (d == 0) {
    if (x == 0) {
      cout << 1;
      return 0;
    }
    cout << n + 1;
    return 0;
  }
  for (int i = 1; i <= n; i++)
    smin[i] = smin[i - 1] + i - 1;
  for (int i = 1; i <= n; i++)
    smax[i] = smax[i - 1] + n - i;
  for (int k = 0; k <= n; k++) {
    long long mn = k * x + smin[k] * d;
    long long mx = k * x + smax[k] * d;
    long long md = ((k * x) % d + d) % d;
    mn = (mn - md) / d;
    mx = (mx - md) / d;
    st[md].push_back({mn, mx});
  }
  long long ans = 0;
  for (auto vv : st) {
    vector<pair<long long, long long>> v = vv.second;
    sort(v.begin(), v.end());
    long long sm = -1e18;
    for (int i = 0; i < v.size(); i++) {
      v[i].first = max(v[i].first, sm);
      if (v[i].first > v[i].second)
        continue;
      else
        ans += v[i].second - v[i].first + 1, sm = v[i].second + 1;
    }
  }
  cout << ans;
}
Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

const ll M = 1e9 + 7;
const ll INF = 1e9 + 7;

ll n, x, d, cnt, ans;
priority_queue<P, vector<P>, greater<P>> pq[220000];
map<ll, ll> mp;

int main() {
  cin >> n >> x >> d;

  if (d < 0)
    x = -x, d = -d;

  for (ll p = 0; p <= n; p++) {
    ll xp = x * p;
    ll st = xp / d + p * (p - 1) / 2;
    ll gl = xp / d + p * (2 * n - p - 1) / 2;
    if (mp[xp % d] == 0)
      mp[xp % d] = ++cnt;
    pq[mp[xp % d]].push(P(st, gl));
  }

  for (ll i = 0; i < 210000; i++) {
    ll s = -1e18;
    while (pq[i].size()) {
      ll nt = pq[i].top().first;
      ll ns = pq[i].top().second;
      pq[i].pop();
      if (s < ns) {
        ans += ns - max(s, nt - 1);
        s = ns;
      }
    }
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

const ll M = 1e9 + 7;
const ll INF = 1e9 + 7;

ll n, x, d, cnt, ans;
priority_queue<P, vector<P>, greater<P>> pq[220000];
map<ll, ll> mp;

int main() {
  cin >> n >> x >> d;

  if (d == 0) {
    if (x == 0) {
      cout << 1 << endl;
      return 0;
    } else {
      cout << n + 1 << endl;
      return 0;
    }
  }

  if (d < 0)
    x = -x, d = -d;

  for (ll p = 0; p <= n; p++) {
    ll xp = x * p;
    ll st = xp / d + p * (p - 1) / 2;
    ll gl = xp / d + p * (2 * n - p - 1) / 2;
    if (mp[xp % d] == 0)
      mp[xp % d] = ++cnt;
    pq[mp[xp % d]].push(P(st, gl));
  }

  for (ll i = 0; i < 210000; i++) {
    ll s = -1e18;
    while (pq[i].size()) {
      ll nt = pq[i].top().first;
      ll ns = pq[i].top().second;
      pq[i].pop();
      if (s < ns) {
        ans += ns - max(s, nt - 1);
        s = ns;
      }
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct line {
  ll l, r;
  bool operator<(const line &rhs) const { return r < rhs.l; }
};
unordered_map<int, set<line>> eq;
void add(int ind, ll l, ll r) {
  line x = {l, r};
  while (1) {
    auto it = eq[ind].find(x);
    if (it == eq[ind].end())
      break;
    x = {min(x.l, it->l), max(x.r, it->r)};
    eq[ind].erase(it);
  }
  eq[ind].insert(x);
}
int main() {
  ll N, X, D;
  scanf("%lld%lld%lld", &N, &X, &D);
  if (D < 0) {
    X += (N - 1) * D;
    D = -D;
  }
  for (ll k = 0; k <= N; k++) {
    int ind = (k * X) % D;
    if (ind < 0)
      ind += D;
    ll det = (ind - k * X) / D;
    add(ind, k * (k - 1) / 2 - det, k * (2 * N - k - 1) / 2 - det);
  }
  ll ans = 0;
  for (auto item : eq)
    for (auto x : item.second)
      ans += x.r - x.l + 1;
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct line {
  ll l, r;
  bool operator<(const line &rhs) const { return r < rhs.l; }
};
unordered_map<int, set<line>> eq;
void add(int ind, ll l, ll r) {
  line x = {l, r};
  while (1) {
    auto it = eq[ind].find(x);
    if (it == eq[ind].end())
      break;
    x = {min(x.l, it->l), max(x.r, it->r)};
    eq[ind].erase(it);
  }
  eq[ind].insert(x);
}
int main() {
  ll N, X, D;
  scanf("%lld%lld%lld", &N, &X, &D);
  if (!D) {
    printf("%lld\n", X ? N + 1 : 1ll);
    return 0;
  }
  if (D < 0) {
    X += (N - 1) * D;
    D = -D;
  }
  for (ll k = 0; k <= N; k++) {
    int ind = (k * X) % D;
    if (ind < 0)
      ind += D;
    ll det = (ind - k * X) / D;
    add(ind, k * (k - 1) / 2 - det, k * (2 * N - k - 1) / 2 - det);
  }
  ll ans = 0;
  for (auto item : eq)
    for (auto x : item.second)
      ans += x.r - x.l + 1;
  printf("%lld\n", ans);
  return 0;
}
Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define int long long
inline int read() {
  char ch = getchar();
  int nega = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      nega = -1;
    ch = getchar();
  }
  int ans = 0;
  while (isdigit(ch)) {
    ans = ans * 10 + ch - 48;
    ch = getchar();
  }
  if (nega == -1)
    return -ans;
  return ans;
}
#define N 200005
int f[N], sum[N];
signed main() {
#ifdef __LOCAL__
  freopen("in.txt", "r", stdin);
#endif
  int n, x, d;
  cin >> n >> x >> d;
  if (x < 0 && d < 0)
    x = -x, d = -d;
  for (int i = 1; i <= n; i++)
    sum[i] = sum[i - 1] + i - 1;
  int k = __gcd(x, d);
  x /= k, d /= k;
  int ans = 0;
  if (x >= 0 && d >= 0) {
    for (int i = 0; i <= n; i++) {
      int l = sum[i], r = sum[n] - sum[n - i];
      if (l > r)
        swap(l, r);
      ans += r - l + 1;
      if (i + d <= n) {
        l = max(l, sum[i + d] + x);
        ans -= max(0LL, r - l + 1);
      }
    }
  } else {
    if (x < 0)
      x *= -1;
    if (d < 0)
      d *= -1;
    for (int i = 0; i <= n; i++) {
      int l = sum[i], r = sum[n] - sum[n - i];
      if (l > r)
        swap(l, r);
      ans += r - l + 1;
      if (i + d <= n) {
        r = min(r, sum[n] - sum[n - i - d] - x);
        l = max(l, sum[i + d] - x);
        ans -= max(0LL, r - l + 1);
      }
    }
  }
  cout << ans << endl;
#ifdef __LOCAL__
  printf("Time Used : %d\n", clock());
#endif
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define int long long
inline int read() {
  char ch = getchar();
  int nega = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      nega = -1;
    ch = getchar();
  }
  int ans = 0;
  while (isdigit(ch)) {
    ans = ans * 10 + ch - 48;
    ch = getchar();
  }
  if (nega == -1)
    return -ans;
  return ans;
}
#define N 200005
int f[N], sum[N];
signed main() {
#ifdef __LOCAL__
  freopen("in.txt", "r", stdin);
#endif
  int n, x, d;
  cin >> n >> x >> d;
  if (x == 0 && d == 0) {
    cout << "1\n";
    return 0;
  }
  if (x < 0 && d < 0)
    x = -x, d = -d;
  for (int i = 1; i <= n; i++)
    sum[i] = sum[i - 1] + i - 1;
  int k = __gcd(x, d);
  x /= k, d /= k;
  int ans = 0;
  if (x >= 0 && d >= 0) {
    for (int i = 0; i <= n; i++) {
      int l = sum[i], r = sum[n] - sum[n - i];
      if (l > r)
        swap(l, r);
      ans += r - l + 1;
      if (i + d <= n) {
        l = max(l, sum[i + d] + x);
        ans -= max(0LL, r - l + 1);
      }
    }
  } else {
    if (x < 0)
      x *= -1;
    if (d < 0)
      d *= -1;
    for (int i = 0; i <= n; i++) {
      int l = sum[i], r = sum[n] - sum[n - i];
      if (l > r)
        swap(l, r);
      ans += r - l + 1;
      if (i + d <= n) {
        r = min(r, sum[n] - sum[n - i - d] - x);
        l = max(l, sum[i + d] - x);
        ans -= max(0LL, r - l + 1);
      }
    }
  }
  cout << ans << endl;
#ifdef __LOCAL__
  printf("Time Used : %d\n", clock());
#endif
  return 0;
}
Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <list>
#include <map>
#include <utility>
#include <vector>
using namespace std;

void merge(list<pair<long long, long long>> &L, long long mn, long long mx) {
  for (auto p = L.begin(); p != L.end();) {
    if (mn <= p->first && p->second <= mx) {
      p = L.erase(p);
    } else if (p->first <= mn && mx <= p->second) {
      mn = p->first;
      mx = p->second;
      p = L.erase(p);
    } else if (mn <= p->first && p->first <= mx) {
      mx = p->second;
      p = L.erase(p);
    } else if (p->first <= mn && mn <= p->second) {
      mn = p->first;
      p = L.erase(p);
    } else
      p++;
  }
  L.push_back(make_pair(mn, mx));
}

int main() {
  long long N, X, D;
  cin >> N >> X >> D;

  if (D < 0) {
    X *= -1LL;
    D *= -1LL;
  }

  map<long long, list<pair<long long, long long>>> M;

  long long mn = 0;
  long long mx = 0;
  M[0].push_back(make_pair(0LL, 0LL));

  for (long long i = 0; i < N; i++) {
    mn += i;
    mx += N - i - 1;

    long long t = X * (i + 1);
    list<pair<long long, long long>> &L = M[(t % D + D) % D];
    t -= (t % D + D) % D;
    t /= D;
    merge(L, t + mn, t + mx);
  }

  long long ans = 0;
  for (auto L : M)
    for (auto e : L.second)
      ans += e.second - e.first + 1;
  cout << ans << endl;
}


Fixed submission:
#include <iostream>
#include <list>
#include <map>
#include <utility>
#include <vector>
using namespace std;

void merge(list<pair<long long, long long>> &L, long long mn, long long mx) {
  for (auto p = L.begin(); p != L.end();) {
    if (mn <= p->first && p->second <= mx) {
      p = L.erase(p);
    } else if (p->first <= mn && mx <= p->second) {
      mn = p->first;
      mx = p->second;
      p = L.erase(p);
    } else if (mn <= p->first && p->first <= mx) {
      mx = p->second;
      p = L.erase(p);
    } else if (p->first <= mn && mn <= p->second) {
      mn = p->first;
      p = L.erase(p);
    } else
      p++;
  }
  L.push_back(make_pair(mn, mx));
}

int main() {
  long long N, X, D;
  cin >> N >> X >> D;

  if (D < 0) {
    X *= -1LL;
    D *= -1LL;
  }

  if (D == 0) {
    cout << (X == 0 ? 1 : N + 1) << endl;
    return 0;
  }

  map<long long, list<pair<long long, long long>>> M;

  long long mn = 0;
  long long mx = 0;
  M[0].push_back(make_pair(0LL, 0LL));

  for (long long i = 0; i < N; i++) {
    mn += i;
    mx += N - i - 1;

    long long t = X * (i + 1);
    list<pair<long long, long long>> &L = M[(t % D + D) % D];
    t -= (t % D + D) % D;
    t /= D;
    merge(L, t + mn, t + mx);
  }

  long long ans = 0;
  for (auto L : M)
    for (auto e : L.second)
      ans += e.second - e.first + 1;
  cout << ans << endl;
}
Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <map>
#include <stdio.h>
using namespace std;

long long N, X, D;

int main() {
  scanf("%lld %lld %lld", &N, &X, &D);
  map<long long, map<long long, int>> cnt;
  for (long long i = 0; i <= N; i++) {
    long long r = i * X % D;
    long long s = (i * X / D) + i * (i - 1) / 2;
    long long e = (i * X / D) + (N * (N - 1) / 2 - (N - i) * (N - i - 1) / 2);
    cnt[r][s]++;
    cnt[r][e + 1]--;
  }

  long long ans = 0;
  for (auto &v : cnt) {
    int c = 0;
    for (auto &p : v.second)
      if (p.second) {
        if (c == 0)
          ans -= p.first;
        c += p.second;
        if (c == 0)
          ans += p.first;
      }
  }
  printf("%lld\n", ans);

  return 0;
}

// i -> i * (i - 1) / 2 ~ N * (N - 1) / 2 - (N - i) * (N - i - 1) / 2

Fixed submission:
#include <algorithm>
#include <map>
#include <stdio.h>
using namespace std;

long long N, X, D;

int main() {
  scanf("%lld %lld %lld", &N, &X, &D);
  if (D == 0) {
    if (X)
      printf("%lld\n", N + 1);
    else
      puts("1");
    return 0;
  }

  map<long long, map<long long, int>> cnt;
  for (long long i = 0; i <= N; i++) {
    long long r = i * X % D;
    long long s = (i * X / D) + i * (i - 1) / 2;
    long long e = (i * X / D) + (N * (N - 1) / 2 - (N - i) * (N - i - 1) / 2);
    cnt[r][s]++;
    cnt[r][e + 1]--;
  }

  long long ans = 0;
  for (auto &v : cnt) {
    int c = 0;
    for (auto &p : v.second)
      if (p.second) {
        if (c == 0)
          ans -= p.first;
        c += p.second;
        if (c == 0)
          ans += p.first;
      }
  }
  printf("%lld\n", ans);

  return 0;
}

// i -> i * (i - 1) / 2 ~ N * (N - 1) / 2 - (N - i) * (N - i - 1) / 2