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

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main(void) {
  int N = 0;
  int A = 0, B = 0;
  int P[20];
  int q1 = 0, q2 = 0, q3 = 0;
  int ans = 0;

  // 入力
  cin >> N >> A >> B;

  for (int i = 0; i < N; i++) {
    cin >> P[i];
  }

  // 処理
  for (int i = 0; i < N; i++) {
    if (P[i] <= A) {
      q1++;
    }
  }
  for (int i = 0; i < N; i++) {
    if (P[i] > A && P[i] <= B) {
      q2++;
    }
  }
  for (int i = 0; i < N; i++) {
    if (P[i] > B) {
      q3++;
    }
  }

  ans = q1;
  if (ans > q2) {
    ans = q2;
  }
  if (ans > q3) {
    ans = q3;
  }

  // 出力
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
int main(void) {
  int N = 0;
  int A = 0, B = 0;
  int P[100];
  int q1 = 0, q2 = 0, q3 = 0;
  int ans = 0;

  // 入力
  cin >> N >> A >> B;

  for (int i = 0; i < N; i++) {
    cin >> P[i];
  }

  // 処理
  for (int i = 0; i < N; i++) {
    if (P[i] <= A) {
      q1++;
    }
  }
  for (int i = 0; i < N; i++) {
    if (P[i] > A && P[i] <= B) {
      q2++;
    }
  }
  for (int i = 0; i < N; i++) {
    if (P[i] > B) {
      q3++;
    }
  }

  ans = q1;
  if (ans > q2) {
    ans = q2;
  }
  if (ans > q3) {
    ans = q3;
  }

  // 出力
  cout << ans << endl;

  return 0;
}
Problem p03156 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
#define mem(a, b) memset(a, b, sizeof(a))
int max(int a, int b) { return a > b ? a : b; }

int main() {
  int n, a[50], A, B;
  scanf("%d", &n);
  scanf("%d%d", &A, &B);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  sort(a + 1, a + n + 1);
  int x1 = 0, x2 = 0, x3 = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] <= A)
      x1++;
    else if (a[i] > B)
      x3++;
    else
      x2++;
  }
  printf("%d\n", min(x1, min(x2, x3)));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
#define mem(a, b) memset(a, b, sizeof(a))
int max(int a, int b) { return a > b ? a : b; }

int main() {
  int n, a[200], A, B;
  scanf("%d", &n);
  scanf("%d%d", &A, &B);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  sort(a + 1, a + n + 1);
  int x1 = 0, x2 = 0, x3 = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] <= A)
      x1++;
    else if (a[i] > B)
      x3++;
    else
      x2++;
  }
  printf("%d\n", min(x1, min(x2, x3)));
  return 0;
}
Problem p03156 (C++)

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

int main() {
  int n, a, b;
  cin >> n >> a >> b;
  int x, y, z;
  x = 0;
  y = 0;
  z = 0;
  int p[25];
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    if (p[i] <= a) {
      x++;
    }
    if (p[i] > a && p[i] <= b) {
      y++;
    }
    if (p[i] > b) {
      z++;
    }
  }
  int ans;
  ans = min(x, y);
  ans = min(ans, z);
  cout << ans << endl;

  return 0;
}


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

int main() {
  int n, a, b;
  cin >> n >> a >> b;
  int x, y, z;
  x = 0;
  y = 0;
  z = 0;
  int p[10000];
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    if (p[i] <= a) {
      x++;
    }
    if (p[i] > a && p[i] <= b) {
      y++;
    }
    if (p[i] > b) {
      z++;
    }
  }
  int ans;
  ans = min(x, y);
  ans = min(ans, z);
  cout << ans << endl;

  return 0;
}
Problem p03156 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

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

int main() {
  int N;
  int p[30];
  int a, b;
  cin >> N;
  cin >> a >> b;
  for (int i = 0; i < N; i++) {
    cin >> p[i];
  }

  int x = 0, y = 0, z = 0;
  for (int i = 0; i < N; i++) {
    if (p[i] <= a) {
      x++;
    } else if (p[i] <= b) {
      y++;
    } else {
      z++;
    }
  }

  cout << min(x, min(y, z)) << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <complex>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

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

int main() {
  int N;
  int p[110];
  int a, b;
  cin >> N;
  cin >> a >> b;
  for (int i = 0; i < N; i++) {
    cin >> p[i];
  }

  int x = 0, y = 0, z = 0;
  for (int i = 0; i < N; i++) {
    if (p[i] <= a) {
      x++;
    } else if (p[i] <= b) {
      y++;
    } else {
      z++;
    }
  }

  cout << min(x, min(y, z)) << endl;

  return 0;
}
Problem p03156 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
int main() {
  int n;
  cin >> n;
  int a, b;
  cin >> a >> b;
  int p[20];
  int cnt1 = 0, cnt2 = 0, cnt3 = 0;
  rep(i, n) { cin >> p[i]; }
  rep(i, n) {
    if (p[i] <= a) {
      cnt1++;
    } else if (a + 1 <= p[i] && p[i] <= b) {
      cnt2++;
    } else if (b + 1 <= p[i]) {
      cnt3++;
    }
  }
  int ans = min(min(cnt1, cnt2), cnt3);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
int main() {
  int n;
  cin >> n;
  int a, b;
  cin >> a >> b;
  int p[100];
  int cnt1 = 0, cnt2 = 0, cnt3 = 0;
  rep(i, n) { cin >> p[i]; }
  rep(i, n) {
    if (p[i] <= a) {
      cnt1++;
    } else if (a + 1 <= p[i] && p[i] <= b) {
      cnt2++;
    } else if (b + 1 <= p[i]) {
      cnt3++;
    }
  }
  int ans = min(min(cnt1, cnt2), cnt3);
  cout << ans << endl;
  return 0;
}
Problem p03157 (Python)

Buggy submission (status: Runtime Error):
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
dx = (0, 0, 1, -1)
dy = (1, -1, 0, 0)
seen = [[False] * w for _ in range(h)]

numb = 0
numw = 0


def dfs(x, y):
    global numb, numw
    seen[x][y] = True
    if s[x][y] == "#":
        numb += 1
    else:
        numw += 1
    for di in range(4):
        nx = x + dx[di]
        ny = y + dy[di]
        if nx < 0 or nx >= h or ny < 0 or ny >= w:
            continue
        if s[x][y] == s[nx][ny]:
            continue
        if seen[nx][ny]:
            continue
        dfs(nx, ny)


ans = 0
for x in range(h):
    for y in range(w):
        if seen[x][y]:
            continue
        if s[x][y] == ".":
            continue
        numb = 0
        numw = 0
        dfs(x, y)
        ans += numb * numw
print(ans)


Fixed submission:
import sys

sys.setrecursionlimit(10**6)
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
dx = (0, 0, 1, -1)
dy = (1, -1, 0, 0)
seen = [[False] * w for _ in range(h)]

numb = 0
numw = 0


def dfs(x, y):
    global numb, numw
    seen[x][y] = True
    if s[x][y] == "#":
        numb += 1
    else:
        numw += 1
    for di in range(4):
        nx = x + dx[di]
        ny = y + dy[di]
        if nx < 0 or nx >= h or ny < 0 or ny >= w:
            continue
        if s[x][y] == s[nx][ny]:
            continue
        if seen[nx][ny]:
            continue
        dfs(nx, ny)


ans = 0
for x in range(h):
    for y in range(w):
        if seen[x][y]:
            continue
        if s[x][y] == ".":
            continue
        numb = 0
        numw = 0
        dfs(x, y)
        ans += numb * numw
print(ans)
Problem p03157 (Python)

Buggy submission (status: Runtime Error):
def update(M, S, i, j, H, W, cluster):
    for i_n in [i - 1, i, i + 1]:
        for j_n in [j - 1, j, j + 1]:
            if i_n >= H or i_n < 0:
                continue
            if j_n >= W or j_n < 0:
                continue

            if i_n == i and j_n == j:
                continue
            if abs(i - i_n) + abs(j - j_n) == 2:
                continue

            if S[i][j] != S[i_n][j_n] and M[i_n][j_n] == 0:
                # print(M)
                M[i_n][j_n] = cluster
                update(M, S, i_n, j_n, H, W, cluster)


H, W = list(map(int, input().split()))
S = []
for i in range(H):
    S.append(input().strip())

cluster = 1
counts_B = {}
counts_W = {}
M = [[0 for j in range(W)] for i in range(H)]
for i in range(H):
    for j in range(W):
        if M[i][j] == 0:
            M[i][j] = cluster
            update(M, S, i, j, H, W, cluster)
            cluster = cluster + 1
        counts_B[M[i][j]] = 0
        counts_W[M[i][j]] = 0


for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            counts_B[M[i][j]] += 1
        else:
            counts_W[M[i][j]] += 1

    #     print(M[i][j], end=" ")
    # print("")

total = 0
for k in counts_B.keys():
    if k in counts_W:
        total += counts_B[k] * counts_W[k]
    # print(counts[k])

print(total)


Fixed submission:
import sys

sys.setrecursionlimit(2000000)


def update(M, S, i, j, H, W, cluster):
    for i_n in [i - 1, i, i + 1]:
        for j_n in [j - 1, j, j + 1]:
            if i_n >= H or i_n < 0:
                continue
            if j_n >= W or j_n < 0:
                continue

            if i_n == i and j_n == j:
                continue
            if abs(i - i_n) + abs(j - j_n) == 2:
                continue

            if S[i][j] != S[i_n][j_n] and M[i_n][j_n] == 0:
                # print(M)
                M[i_n][j_n] = cluster
                update(M, S, i_n, j_n, H, W, cluster)


H, W = list(map(int, input().split()))
S = []
for i in range(H):
    S.append(input().strip())

cluster = 1
counts_B = {}
counts_W = {}
M = [[0 for j in range(W)] for i in range(H)]
for i in range(H):
    for j in range(W):
        if M[i][j] == 0:
            M[i][j] = cluster
            update(M, S, i, j, H, W, cluster)
            cluster = cluster + 1
        counts_B[M[i][j]] = 0
        counts_W[M[i][j]] = 0


for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            counts_B[M[i][j]] += 1
        else:
            counts_W[M[i][j]] += 1

    #     print(M[i][j], end=" ")
    # print("")

total = 0
for k in counts_B.keys():
    if k in counts_W:
        total += counts_B[k] * counts_W[k]
    # print(counts[k])

print(total)
Problem p03157 (Python)

Buggy submission (status: Runtime Error):
#!/usr/bin/env python3
from itertools import product

H, W = map(int, input().split())
S = [input() for _ in range(H)]
color = [[None] * W for _ in range(H)]
memo = [[0, 0] for _ in range(H * W + 1)]
cnt = 1


def dfs(p, c):
    x, y = p
    color[x][y] = c
    memo[cnt][c] += 1
    for dx, dy in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
        nx = x + dx
        ny = y + dy
        if (
            0 <= nx < H
            and 0 <= ny < W
            and color[nx][ny] is None
            and c != (S[nx][ny] == "#")
        ):
            dfs([nx, ny], not c)


for i, j in product(range(H), range(W)):
    if color[i][j] is None:
        dfs([i, j], S[i][j] == "#")
        cnt += 1

ans = 0
for i in range(H * W):
    ans += memo[i][0] * memo[i][1]
print(ans)


Fixed submission:
#!/usr/bin/env python3
from itertools import product
import sys

sys.setrecursionlimit(200000)

H, W = map(int, input().split())
S = [input() for _ in range(H)]
color = [[None] * W for _ in range(H)]
memo = [[0, 0] for _ in range(H * W + 1)]
cnt = 1


def dfs(p, c):
    x, y = p
    color[x][y] = c
    memo[cnt][c] += 1
    for dx, dy in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
        nx = x + dx
        ny = y + dy
        if (
            0 <= nx < H
            and 0 <= ny < W
            and color[nx][ny] is None
            and c != (S[nx][ny] == "#")
        ):
            dfs([nx, ny], not c)


for i, j in product(range(H), range(W)):
    if color[i][j] is None:
        dfs([i, j], S[i][j] == "#")
        cnt += 1

ans = 0
for i in range(H * W):
    ans += memo[i][0] * memo[i][1]
print(ans)
Problem p03157 (Python)

Buggy submission (status: Runtime Error):
h, w = map(int, input().split())
s = [input() for _ in range(h)]

is_used = [[False] * w for _ in range(h)]

di = [+1, 0, -1, 0]
dj = [0, +1, 0, -1]


def dfs(i, j):
    if s[i][j] == "#":
        cnt_w, cnt_b = 0, 1
    else:
        cnt_w, cnt_b = 1, 0

    for k in range(4):
        ni = i + di[k]
        nj = j + dj[k]
        if ni < 0 or ni >= h or nj < 0 or nj >= w:
            continue
        if is_used[ni][nj]:
            continue
        if s[i][j] == s[ni][nj]:
            continue

        is_used[ni][nj] = True
        cw, cb = dfs(ni, nj)
        cnt_w += cw
        cnt_b += cb
    return (cnt_w, cnt_b)


ans = 0
for i in range(h):
    for j in range(w):
        if not is_used[i][j]:
            is_used[i][j] = True
            cnt_w, cnt_b = dfs(i, j)
            ans += cnt_w * cnt_b

print(ans)


Fixed submission:
import sys

sys.setrecursionlimit(10**6)

h, w = map(int, input().split())
s = [input() for _ in range(h)]

is_used = [[False] * w for _ in range(h)]

di = [+1, 0, -1, 0]
dj = [0, +1, 0, -1]


def dfs(i, j):
    if s[i][j] == "#":
        cnt_w, cnt_b = 0, 1
    else:
        cnt_w, cnt_b = 1, 0

    for k in range(4):
        ni = i + di[k]
        nj = j + dj[k]
        if ni < 0 or ni >= h or nj < 0 or nj >= w:
            continue
        if is_used[ni][nj]:
            continue
        if s[i][j] == s[ni][nj]:
            continue

        is_used[ni][nj] = True
        cw, cb = dfs(ni, nj)
        cnt_w += cw
        cnt_b += cb
    return (cnt_w, cnt_b)


ans = 0
for i in range(h):
    for j in range(w):
        if not is_used[i][j]:
            is_used[i][j] = True
            cnt_w, cnt_b = dfs(i, j)
            ans += cnt_w * cnt_b

print(ans)
Problem p03157 (Python)

Buggy submission (status: Runtime Error):
def dfs(x, y, matrix, visited, isWhite):
    global black, white
    dxy = [[1, 0], [-1, 0], [0, -1], [0, 1]]
    visited[y][x] = True
    w = len(matrix[0])
    h = len(matrix)

    for dx, dy in dxy:
        nx = x + dx
        ny = y + dy

        if nx < 0 or w <= nx or ny < 0 or h <= ny:
            continue

        if visited[ny][nx] is True:
            continue

        if (matrix[ny][nx] == "#" and isWhite is False) or (
            matrix[ny][nx] == "." and isWhite is True
        ):
            continue

        if matrix[ny][nx] == "#":
            black += 1
        else:
            white += 1

        isWhite = not isWhite
        dfs(nx, ny, matrix, visited, isWhite)
        isWhite = not isWhite

    return


h, w = map(int, input().split())
matrix = [""] * h
for i in range(h):
    matrix[i] = input()

ans = 0
visited = [[False] * w for _ in range(h)]

for i in range(h):
    for j in range(w):
        if matrix[i][j] == "#" and visited[i][j] is False:
            black = 1
            white = 0
            dfs(j, i, matrix, visited, False)
            ans += black * white

print(ans)


Fixed submission:
import sys

sys.setrecursionlimit(10**6)


def dfs(x, y, matrix, visited, isWhite):
    global black, white
    dxy = [[1, 0], [-1, 0], [0, -1], [0, 1]]
    visited[y][x] = True
    w = len(matrix[0])
    h = len(matrix)

    for dx, dy in dxy:
        nx = x + dx
        ny = y + dy

        if nx < 0 or w <= nx or ny < 0 or h <= ny:
            continue

        if visited[ny][nx] is True:
            continue

        if (matrix[ny][nx] == "#" and isWhite is False) or (
            matrix[ny][nx] == "." and isWhite is True
        ):
            continue

        if matrix[ny][nx] == "#":
            black += 1
        else:
            white += 1

        isWhite = not isWhite
        dfs(nx, ny, matrix, visited, isWhite)
        isWhite = not isWhite

    return


h, w = map(int, input().split())
matrix = [""] * h
for i in range(h):
    matrix[i] = input()

ans = 0
visited = [[False] * w for _ in range(h)]

for i in range(h):
    for j in range(w):
        if matrix[i][j] == "#" and visited[i][j] is False:
            black = 1
            white = 0
            dfs(j, i, matrix, visited, False)
            ans += black * white

print(ans)
Problem p03157 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FLASH                                                                  \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios_base::sync_with_stdio(false);
#define pb push_back
#define int long long
#define fr(i, a, b) for (int i = a; i < b; i++)
#define mod 1000000007
#define FILEIO                                                                 \
  freopen("/home/aman/Desktop/Kachda/input.txt", "r", stdin);                  \
  freopen("/home/aman/Desktop/Kachda/output.txt", "w", stdout);

using namespace std;

int n, m, b = 0, w = 0;
char s[401][401];

bool vis[404][401];

void dfs(int i, int j) {
  if (vis[i][j])
    return;
  char clr = s[i][j];
  vis[i][j] = 1;
  if (clr == '.')
    b++;
  else
    w++;

  if (i > 1 and s[i - 1][j] != clr)
    dfs(i - 1, j);
  if (i < n and s[i + 1][j] != clr)
    dfs(i + 1, j);
  if (j > 1 and s[i][j - 1] != clr)
    dfs(i, j - 1);
  if (j < m and s[i][j + 1] != clr)
    dfs(i, j + 1);
}
signed main() {
#ifndef ONLINE_JUDGE
  FILEIO
#endif
  FLASH

  cin >> n >> m;
  fr(i, 1, n + 1) {
    fr(j, 1, m + 1) { cin >> s[i][j]; }
  }

  int ans = 0;

  fr(i, 1, n + 1) {
    fr(j, 1, m + 1) {
      if (!vis[i][j]) {
        dfs(i, j);
        ans += b * w;
        b = w = 0;
      }
    }
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define FLASH                                                                  \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios_base::sync_with_stdio(false);
#define pb push_back
#define int long long
#define fr(i, a, b) for (int i = a; i < b; i++)
#define mod 1000000007
#define FILEIO                                                                 \
  freopen("/home/aman/Desktop/Kachda/input.txt", "r", stdin);                  \
  freopen("/home/aman/Desktop/Kachda/output.txt", "w", stdout);

using namespace std;

int n, m, b = 0, w = 0;
char s[401][401];

bool vis[404][401];

void dfs(int i, int j) {
  if (vis[i][j])
    return;
  char clr = s[i][j];
  vis[i][j] = 1;
  if (clr == '.')
    b++;
  else
    w++;

  if (i > 1 and s[i - 1][j] != clr)
    dfs(i - 1, j);
  if (i < n and s[i + 1][j] != clr)
    dfs(i + 1, j);
  if (j > 1 and s[i][j - 1] != clr)
    dfs(i, j - 1);
  if (j < m and s[i][j + 1] != clr)
    dfs(i, j + 1);
}
signed main() {

  FLASH

  cin >> n >> m;
  fr(i, 1, n + 1) {
    fr(j, 1, m + 1) { cin >> s[i][j]; }
  }

  int ans = 0;

  fr(i, 1, n + 1) {
    fr(j, 1, m + 1) {
      if (!vis[i][j]) {
        dfs(i, j);
        ans += b * w;
        b = w = 0;
      }
    }
  }
  cout << ans;
}
Problem p03157 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
// #define int long long
pair<int, int> kk(int t[][500], int s[][500], int i, int j, int h, int w) {
  pair<int, int> d = make_pair(0, 0);
  pair<int, int> addd;

  t[i][j] = 0;

  if (i > 0 && s[i][j] != s[i - 1][j] && t[i - 1][j] == -1) {
    addd = kk(t, s, i - 1, j, h, w);
    d.first += addd.first;
    d.second += addd.second;
  }
  if (j > 0 && s[i][j] != s[i][j - 1] && t[i][j - 1] == -1) {
    addd = kk(t, s, i, j - 1, h, w);
    d.first += addd.first;
    d.second += addd.second;
  }
  if (i < h && s[i][j] != s[i + 1][j] && t[i + 1][j] == -1) {

    addd = kk(t, s, i + 1, j, h, w);
    d.first += addd.first;
    d.second += addd.second;
  }
  if (j < w && s[i][j] != s[i][j + 1] && t[i][j + 1] == -1) {

    addd = kk(t, s, i, j + 1, h, w);
    d.first += addd.first;
    d.second += addd.second;
  }

  if (s[i][j] == 1)
    d.first++;
  else
    d.second++;
  return d;
}

signed main() {
  int h, w;
  cin >> h >> w;
  int s[h][500];
  int t[h][500];
  string l;
  for (int i = 0; i < h; i++) {
    cin >> l;
    for (int j = 0; j < w; j++) {
      if (l[j] == '#')
        s[i][j] = 1;
      else
        s[i][j] = 0;
      t[i][j] = -1;
    }
  }
  pair<int, int> d;
  long ans = 0;
  for (int i = 0; i < h; i++) {

    for (int j = 0; j < w; j++) {
      if (t[i][j] == -1) {
        d = kk(t, s, i, j, h, w);
        ans += long(d.first) * long(d.second);
      }
    }
  }

  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// #define int long long
pair<int, int> kk(int t[][500], int s[][500], int i, int j, int h, int w) {
  pair<int, int> d = make_pair(0, 0);
  pair<int, int> addd;

  t[i][j] = 0;

  if (i > 0 && s[i][j] != s[i - 1][j] && t[i - 1][j] == -1) {
    addd = kk(t, s, i - 1, j, h, w);
    d.first += addd.first;
    d.second += addd.second;
  }
  if (j > 0 && s[i][j] != s[i][j - 1] && t[i][j - 1] == -1) {
    addd = kk(t, s, i, j - 1, h, w);
    d.first += addd.first;
    d.second += addd.second;
  }
  if (i < h && s[i][j] != s[i + 1][j] && t[i + 1][j] == -1) {

    addd = kk(t, s, i + 1, j, h, w);
    d.first += addd.first;
    d.second += addd.second;
  }
  if (j < w && s[i][j] != s[i][j + 1] && t[i][j + 1] == -1) {

    addd = kk(t, s, i, j + 1, h, w);
    d.first += addd.first;
    d.second += addd.second;
  }

  if (s[i][j] == 1)
    d.first++;
  else
    d.second++;
  return d;
}

signed main() {
  int h, w;
  cin >> h >> w;
  int s[h][500];
  int t[h][500];
  string l;
  for (int i = 0; i < h; i++) {
    cin >> l;
    for (int j = 0; j < w; j++) {
      if (l[j] == '#')
        s[i][j] = 1;
      else
        s[i][j] = 0;
      t[i][j] = -1;
    }
  }
  pair<int, int> d;
  long long ans = 0;
  for (int i = 0; i < h; i++) {

    for (int j = 0; j < w; j++) {
      if (t[i][j] == -1) {
        d = kk(t, s, i, j, h, w);
        ans += long(d.first) * long(d.second);
      }
    }
  }

  cout << ans;

  return 0;
}
Problem p03157 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
using ll = long long;
int x[4] = {-1, 0, 1, 0};
int y[4] = {0, 1, 0, -1};
int H, W;
bool S[400][400];
bool has_been_discovered[400][400];
int members[400][2];
void DepthFirstSearch(int row, int column, int group_number) {
  has_been_discovered[row][column] = true;
  members[group_number][S[row][column]]++;
  for (int i = 0; i < 4; ++i) {
    if (0 <= row + x[i] && row + x[i] < H && 0 <= column + y[i] &&
        column + y[i] < W && !has_been_discovered[row + x[i]][column + y[i]] &&
        (S[row][column] ^ S[row + x[i]][column + y[i]])) {
      DepthFirstSearch(row + x[i], column + y[i], group_number);
    }
  }
}

int main() {
  scanf("%d %d", &H, &W);
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (!j)
        scanf("\n");
      char tmp;
      scanf("%c", &tmp);
      S[i][j] = tmp == '#';
    }
  }
  int group_number_ = 0;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j)
      if (!has_been_discovered[i][j])
        DepthFirstSearch(i, j, group_number_++);
  }
  ll answer = 0;
  for (int i = 0; i < group_number_; ++i)
    answer += members[i][0] * members[i][1];
  printf("%lld\n", answer);
  return 0;
}

Fixed submission:
#include <cstdio>
using ll = long long;
int x[4] = {-1, 0, 1, 0};
int y[4] = {0, 1, 0, -1};
int H, W;
bool S[400][400];
bool has_been_discovered[400][400];
ll members[160000][2];
void DepthFirstSearch(int row, int column, int group_number) {
  has_been_discovered[row][column] = true;
  members[group_number][S[row][column]]++;
  for (int i = 0; i < 4; ++i) {
    if (0 <= row + x[i] && row + x[i] < H && 0 <= column + y[i] &&
        column + y[i] < W && !has_been_discovered[row + x[i]][column + y[i]] &&
        (S[row][column] ^ S[row + x[i]][column + y[i]])) {
      DepthFirstSearch(row + x[i], column + y[i], group_number);
    }
  }
}

int main() {
  scanf("%d %d", &H, &W);
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (!j)
        scanf("\n");
      char tmp;
      scanf("%c", &tmp);
      S[i][j] = tmp == '#';
    }
  }
  int group_number_ = 0;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j)
      if (!has_been_discovered[i][j])
        DepthFirstSearch(i, j, group_number_++);
  }
  ll answer = 0;
  for (int i = 0; i < group_number_; ++i)
    answer += members[i][0] * members[i][1];
  printf("%lld\n", answer);
  return 0;
}
Problem p03157 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll _() {
  ll x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -f;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}
#define _ _()
int n, m;
ll f[405 * 405], w[405 * 405], b[405 * 405];
ll ans;
char s[405][405];
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
inline int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int main() {
  n = _;
  m = _;
  for (int i = 1; i <= n * m; i++)
    f[i] = i;
  for (int i = 1; i <= n; i++)
    scanf("%s", s[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k < 4; k++) {
        int nx = i + dx[k], ny = j + dy[k];
        if (nx < 1 || ny < 1 || nx > n || ny > m)
          continue;
        if (s[nx][ny] != s[i][j]) {
          int fx = find((nx - 1) * m + ny), fy = (i - 1) * m + j;
          f[fx] = f[fy];
        }
      }
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (s[i][j] == '#')
        b[find((i - 1) * m + j)]++;
      else
        w[find((i - 1) * m + j)]++;
  for (int i = 1; i <= n * m; i++)
    ans += b[i] * w[i];
  printf("%lld\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll _() {
  ll x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -f;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}
#define _ _()
int n, m;
ll f[405 * 405], w[405 * 405], b[405 * 405];
ll ans;
char s[405][405];
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
inline int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int main() {
  n = _;
  m = _;
  for (int i = 1; i <= n * m; i++)
    f[i] = i;
  for (int i = 1; i <= n; i++)
    scanf("%s", s[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k < 4; k++) {
        int nx = i + dx[k], ny = j + dy[k];
        if (nx < 1 || ny < 1 || nx > n || ny > m)
          continue;
        if (s[nx][ny] != s[i][j]) {
          int fx = find((nx - 1) * m + ny), fy = find((i - 1) * m + j);
          f[fx] = f[fy];
        }
      }
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (s[i][j] == '#')
        b[find((i - 1) * m + j)]++;
      else
        w[find((i - 1) * m + j)]++;
  for (int i = 1; i <= n * m; i++)
    ans += b[i] * w[i];
  printf("%lld\n", ans);
}
Problem p03157 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define priq priority_queue<int>
#define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key)))
#define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key)))
#define tii tuple<int, int, int>
#define Priq priority_queue<int, vi, greater<int>>
#define pb push_back
#define mp make_pair
#define INF (1ll << 60)
signed main() {
  int H, W;
  cin >> H >> W;
  vector<string> S(H);
  rep(i, 0, H) cin >> S[i];
  vi P = {0, 1, 0, -1}, R = {1, 0, -1, 0};
  int ans = 0;
  vector<vector<bool>> F(H, vector<bool>(W, 1));
  rep(i, 0, H) {
    rep(j, 0, W) {
      if (F[i][j]) {
        queue<pii> Q;
        Q.push(mp(i, j));
        F[i][j] = 0;
        int C = 0, D = 0;
        while (Q.size() > 0) {
          int X = Q.front().first, Y = Q.front().second;
          Q.pop();
          C++;
          if (S[X][Y] == '.')
            D++;
          rep(k, 0, 4) {
            int A = P[k] + X, B = R[k] + Y;
            if (0 <= A && A < H && 0 <= B && B < W && S[X][Y] != S[A][B]) {
              Q.push(mp(A, B));
              F[A][B] = 0;
            }
          }
        }
        ans += (C - D) * D;
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define priq priority_queue<int>
#define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key)))
#define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key)))
#define tii tuple<int, int, int>
#define Priq priority_queue<int, vi, greater<int>>
#define pb push_back
#define mp make_pair
#define INF (1ll << 60)
signed main() {
  int H, W;
  cin >> H >> W;
  vector<string> S(H);
  rep(i, 0, H) cin >> S[i];
  vi P = {0, 1, 0, -1}, R = {1, 0, -1, 0};
  int ans = 0;
  vector<vector<bool>> F(H, vector<bool>(W, 1));
  rep(i, 0, H) {
    rep(j, 0, W) {
      if (F[i][j]) {
        queue<pii> Q;
        Q.push(mp(i, j));
        F[i][j] = 0;
        int C = 0, D = 0;
        while (Q.size() > 0) {
          int X = Q.front().first, Y = Q.front().second;
          Q.pop();
          C++;
          if (S[X][Y] == '.')
            D++;
          rep(k, 0, 4) {
            int A = P[k] + X, B = R[k] + Y;
            if (0 <= A && A < H && 0 <= B && B < W && S[X][Y] != S[A][B] &&
                F[A][B]) {
              Q.push(mp(A, B));
              F[A][B] = 0;
            }
          }
        }
        ans += (C - D) * D;
      }
    }
  }
  cout << ans << endl;
}
Problem p03157 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <vector>
#define N 405
using namespace std;

typedef long long ll;
ll n, m, num, cnt[N], s[N], z[N][N];
ll dr[4] = {-1, 0, 0, 1}, dc[4] = {0, -1, 1, 0};
bool v[N][N];
char a[N][N];
// vector<ll> x, y;
void f(ll p, ll q) {
  ll i, j, x, y;
  if (p < 1 || p > n || q < 1 || q > m || v[p][q] || a[p][q] != '#')
    return;
  v[p][q] = 1;
  s[num]++;
  for (i = 0; i < 4; i++) {
    x = p + dr[i];
    y = q + dc[i];
    if (a[x][y] != '.')
      continue;
    if (z[x][y] != num) {
      cnt[num]++;
      z[x][y] = num;
      for (j = 0; j < 4; j++) {
        f(x + dr[j], y + dc[j]);
      }
    }
  }
}
int main() {
  ll i, j;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    scanf("%s", &a[i][1]);
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (v[i][j] || a[i][j] != '#')
        continue;
      num++;
      f(i, j);
    }
  }
  ll ans = 0;
  for (i = 1; i <= num; i++) {
    ans += s[i] * cnt[i];
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <cstdio>
#include <iostream>
#include <vector>
#define N 405
using namespace std;

typedef long long ll;
ll n, m, num, cnt[160005], s[160005], z[N][N];
ll dr[4] = {-1, 0, 0, 1}, dc[4] = {0, -1, 1, 0};
bool v[N][N];
char a[N][N];
// vector<ll> x, y;
void f(ll p, ll q) {
  ll i, j, x, y;
  if (p < 1 || p > n || q < 1 || q > m || v[p][q] || a[p][q] != '#')
    return;
  v[p][q] = 1;
  s[num]++;
  for (i = 0; i < 4; i++) {
    x = p + dr[i];
    y = q + dc[i];
    if (a[x][y] != '.')
      continue;
    if (z[x][y] != num) {
      cnt[num]++;
      z[x][y] = num;
      for (j = 0; j < 4; j++) {
        f(x + dr[j], y + dc[j]);
      }
    }
  }
}
int main() {
  ll i, j;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    scanf("%s", &a[i][1]);
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (v[i][j] || a[i][j] != '#')
        continue;
      num++;
      f(i, j);
    }
  }
  ll ans = 0;
  for (i = 1; i <= num; i++) {
    ans += s[i] * cnt[i];
  }
  cout << ans;
  return 0;
}
Problem p03157 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, cnt, fa[404 * 404], sh[404], sb[404], id[404][404], vis[404][404];
ll ans;
char mp[404][404];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    scanf("%s", mp[i] + 1);
    for (int j = 1; j <= m; j++)
      id[i][j] = ++cnt, fa[cnt] = cnt;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (mp[i][j] == '.')
        sb[id[i][j]] = 1;
      else
        sh[id[i][j]] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (i < n && mp[i + 1][j] != mp[i][j]) {
        int u = find(id[i][j]), v = find(id[i + 1][j]);
        if (u != v) {
          fa[u] = v;
          sh[v] += sh[u], sb[v] += sb[u];
          sh[u] = sb[u] = 0;
        }
      }
      if (j < m && mp[i][j + 1] != mp[i][j]) {
        int u = find(id[i][j]), v = find(id[i][j + 1]);
        if (u != v) {
          fa[u] = v;
          sh[v] += sh[u], sb[v] += sb[u];
          sh[u] = sb[u] = 0;
        }
      }
    }
  for (int i = 1; i <= n * m; i++)
    if (sh[i] && sb[i])
      ans += 1ll * sh[i] * sb[i];
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, cnt, fa[404 * 404], sh[404 * 404], sb[404 * 404], id[404][404],
    vis[404][404];
ll ans;
char mp[404][404];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    scanf("%s", mp[i] + 1);
    for (int j = 1; j <= m; j++)
      id[i][j] = ++cnt, fa[cnt] = cnt;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (mp[i][j] == '.')
        sb[id[i][j]] = 1;
      else
        sh[id[i][j]] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (i < n && mp[i + 1][j] != mp[i][j]) {
        int u = find(id[i][j]), v = find(id[i + 1][j]);
        if (u != v) {
          fa[u] = v;
          sh[v] += sh[u], sb[v] += sb[u];
          sh[u] = sb[u] = 0;
        }
      }
      if (j < m && mp[i][j + 1] != mp[i][j]) {
        int u = find(id[i][j]), v = find(id[i][j + 1]);
        if (u != v) {
          fa[u] = v;
          sh[v] += sh[u], sb[v] += sb[u];
          sh[u] = sb[u] = 0;
        }
      }
    }
  for (int i = 1; i <= n * m; i++)
    if (sh[i] && sb[i])
      ans += 1ll * sh[i] * sb[i];
  cout << ans << endl;
}
Problem p03157 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define ll long long
void file() {
#ifndef ONLINE_JUDGE
  freopen("a_input.txt", "r", stdin);
  freopen("a_output.txt", "w", stdout);
#endif
}
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
vector<vector<char>> adj;
vector<vector<bool>> mem;
ll sum2 = 0, sum1 = 0;
int n, m;
bool valied(int i, int j) { return i >= 0 && j >= 0 && i < n && j < m; }
void dfs(int i, int j, char c) {
  if (adj[i][j] != c)
    return;
  mem[i][j] = 1;
  if (c == '#')
    c = '.', sum1++;
  else
    c = '#', sum2++;
  for (int k = 0; k < 4; k++) {
    int x = dx[k] + i;
    int y = dy[k] + j;
    if (valied(x, y) && !mem[x][y])
      dfs(x, y, c);
  }
}
int main() {
  file();
  fast();
  cin >> n >> m;
  adj = vector<vector<char>>(n, vector<char>(m));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> adj[i][j];
  long long sum = 0;
  mem = vector<vector<bool>>(n, vector<bool>(m, 0));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (adj[i][j] == '#' && !mem[i][j]) {
        sum2 = 0;
        sum1 = 0;
        dfs(i, j, '#');
        sum += sum2 * sum1;
      }
  cout << sum;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define ll long long
void file() {
#ifndef ONLINE_JUDGE
  freopen("a_input.txt", "r", stdin);
  freopen("a_output.txt", "w", stdout);
#endif
}
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
vector<vector<char>> adj;
vector<vector<bool>> mem;
ll sum2 = 0, sum1 = 0;
int n, m;
bool valied(int i, int j) { return i >= 0 && j >= 0 && i < n && j < m; }
void dfs(int i, int j, char c) {
  if (adj[i][j] != c)
    return;
  mem[i][j] = 1;
  if (c == '#')
    c = '.', sum1++;
  else
    c = '#', sum2++;
  for (int k = 0; k < 4; k++) {
    int x = dx[k] + i;
    int y = dy[k] + j;
    if (valied(x, y) && !mem[x][y])
      dfs(x, y, c);
  }
}
int main() {
  // file();
  fast();
  cin >> n >> m;
  adj = vector<vector<char>>(n, vector<char>(m));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> adj[i][j];
  long long sum = 0;
  mem = vector<vector<bool>>(n, vector<bool>(m, 0));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (adj[i][j] == '#' && !mem[i][j]) {
        sum2 = 0;
        sum1 = 0;
        dfs(i, j, '#');
        sum += sum2 * sum1;
      }
  cout << sum;
}
Problem p03157 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
typedef long long ll;

const int maxn = 430;
int n, m, col[maxn], vis[maxn], black, white;
char s[maxn][maxn];
inline int code(int x, int y) { return x * m + y + 1; }
vector<int> G[160010];

void dfs(int u) {
  if (col[u])
    black++;
  else
    white++;
  vis[u] = true;
  for (auto v : G[u])
    if (!vis[v])
      dfs(v);
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i)
    scanf("%s", s[i]);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      col[code(i, j)] = s[i][j] == '#';
      auto chkLink = [&](int x, int y) {
        if (x >= 0 && x < n && y >= 0 && y < m) {
          if (s[x][y] != s[i][j]) {
            G[code(i, j)].push_back(code(x, y));
          }
        }
      };
      chkLink(i - 1, j);
      chkLink(i + 1, j);
      chkLink(i, j - 1);
      chkLink(i, j + 1);
    }
  }
  int tot = n * m;
  ll ans = 0;
  for (int i = 1; i <= tot; ++i)
    if (!vis[i]) {
      black = white = 0;
      dfs(i);
      ans += 1LL * black * white;
    }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
typedef long long ll;

const int maxn = 430;
int n, m, col[160010], vis[160010], black, white;
char s[maxn][maxn];
inline int code(int x, int y) { return x * m + y + 1; }
vector<int> G[160010];

void dfs(int u) {
  if (col[u])
    black++;
  else
    white++;
  vis[u] = true;
  for (auto v : G[u])
    if (!vis[v])
      dfs(v);
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i)
    scanf("%s", s[i]);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      col[code(i, j)] = s[i][j] == '#';
      auto chkLink = [&](int x, int y) {
        if (x >= 0 && x < n && y >= 0 && y < m) {
          if (s[x][y] != s[i][j]) {
            G[code(i, j)].push_back(code(x, y));
          }
        }
      };
      chkLink(i - 1, j);
      chkLink(i + 1, j);
      chkLink(i, j - 1);
      chkLink(i, j + 1);
    }
  }
  int tot = n * m;
  ll ans = 0;
  for (int i = 1; i <= tot; ++i)
    if (!vis[i]) {
      black = white = 0;
      dfs(i);
      ans += 1LL * black * white;
    }
  printf("%lld\n", ans);
  return 0;
}
Problem p03157 (C++)

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

class unionfind {
  vector<int> p;

public:
  void init(int n) {
    for (int i = 0; i < n; i++) {
      p.push_back(i);
    }
  }

  void merge(int m, int n) {
    if (find(m) == find(n))
      return;
    p.at(find(m)) = find(n);
  }

  int find(int n) {
    if (p.at(n) == n)
      return n;
    return p.at(n) = find(p.at(n));
  }
};

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  for (int i = 0; i < h; i++)
    cin >> s.at(i);
  unionfind t;
  t.init(h * w);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (j < w - 1 && s.at(i).at(j) != s.at(i).at(j + 1)) {
        t.merge(i * w + j, i * w + j + 1);
      }
      if (i < h - 1 && s.at(i).at(j) != s.at(i + 1).at(j)) {
        t.merge(i * w + j, (i + 1) * w + j);
      }
    }
  }
  vector<pair<int, int>> c;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int root = t.find(i * w + j);
      if (s.at(i).at(j) == '#') {
        c.at(root).first++;
      } else {
        c.at(root).second++;
      }
    }
  }
  long total = 0;
  for (int i = 0; i < h * w; i++) {
    total += (long)c.at(i).first * c.at(i).second;
  }
  cout << total << endl;
}


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

class unionfind {
  vector<int> p;

public:
  void init(int n) {
    for (int i = 0; i < n; i++) {
      p.push_back(i);
    }
  }

  void merge(int m, int n) {
    if (find(m) == find(n))
      return;
    p.at(find(m)) = find(n);
  }

  int find(int n) {
    if (p.at(n) == n)
      return n;
    return p.at(n) = find(p.at(n));
  }
};

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  for (int i = 0; i < h; i++)
    cin >> s.at(i);
  unionfind t;
  t.init(h * w);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (j < w - 1 && s.at(i).at(j) != s.at(i).at(j + 1)) {
        t.merge(i * w + j, i * w + j + 1);
      }
      if (i < h - 1 && s.at(i).at(j) != s.at(i + 1).at(j)) {
        t.merge(i * w + j, (i + 1) * w + j);
      }
    }
  }
  vector<pair<int, int>> c(h * w);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int root = t.find(i * w + j);
      if (s.at(i).at(j) == '#') {
        c.at(root).first++;
      } else {
        c.at(root).second++;
      }
    }
  }
  long total = 0;
  for (int i = 0; i < h * w; i++) {
    total += (long)c.at(i).first * c.at(i).second;
  }
  cout << total << endl;
}
Problem p03158 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
using namespace std;
long long int n, q, m, p[20000] = {}, a[300000] = {}, b, lef[200000], h, w,
                       tab[500][500] = {}, same = 0, qu[300000],
                       rui[300000] = {}, giza[300000] = {}, res[300000] = {};
int nibutan(long long int check, int left, int right) {

  int nibu = (left + right) / 2;
  if (right - left == 1 && check < lef[nibu]) {
    return nibu + 1;
  }

  if (check >= lef[nibu] && check < lef[nibu - 1]) {
    return nibu;
  } else if (check >= lef[nibu]) {

    return nibutan(check, left, nibu);
  } else {
    return nibutan(check, nibu, right);
  }
}

int main() {

  string str[600];
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    rui[i] = rui[i - 1] + a[i];
  }
  for (int i = 1; i <= q; i++) {
    cin >> qu[i];
  }
  giza[1] = a[1];
  for (int i = 2; i <= n; i++) {

    giza[i] = giza[i - 2] + a[i];
  }
  res[0] = giza[n];
  res[1] = giza[n];

  for (int i = 2; i <= n / 2; i++) {

    res[i] = rui[n] - rui[n - i] + giza[n - 2 * i];
  }
  res[n / 2 + 1] = rui[n] - rui[n / 2];

  for (int i = 0; i <= n / 2; i++) {
    lef[i] = (a[n - i] + a[n - 2 * i]) / 2 + 1;
  }
  lef[n / 2 + 1] = 0;

  for (int i = 1; i <= q; i++) {

    cout << res[nibutan(qu[i], 0, n / 2 + 1)] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
using namespace std;
long long int n, q, m, p[20000] = {}, a[300000] = {}, b, lef[200000], h, w,
                       tab[500][500] = {}, same = 0, qu[300000],
                       rui[300000] = {}, giza[300000] = {}, res[300000] = {};
int nibutan(long long int check, int left, int right) {

  int nibu = (left + right) / 2;
  if (right - left == 1 && check < lef[nibu]) {
    return nibu + 1;
  }

  if (check >= lef[nibu] && check < lef[nibu - 1]) {
    return nibu;
  } else if (check >= lef[nibu]) {

    return nibutan(check, left, nibu);
  } else {
    return nibutan(check, nibu, right);
  }
}

int main() {

  string str[600];
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    rui[i] = rui[i - 1] + a[i];
  }
  for (int i = 1; i <= q; i++) {
    cin >> qu[i];
  }
  giza[1] = a[1];
  for (int i = 2; i <= n; i++) {

    giza[i] = giza[i - 2] + a[i];
  }
  res[0] = giza[n];
  res[1] = giza[n];

  for (int i = 2; i <= n / 2; i++) {

    res[i] = rui[n] - rui[n - i] + giza[n - 2 * i];
  }
  res[n / 2 + 1] = rui[n] - rui[n / 2];

  for (int i = 0; i <= n / 2; i++) {
    lef[i] = (a[n - i] + a[n - 2 * i]) / 2 + 1;
  }
  lef[n / 2 + 1] = 0;

  for (int i = 1; i <= q; i++) {
    if (qu[i] > a[n]) {
      cout << res[0] << endl;
    } else {
      cout << res[nibutan(qu[i], 0, n / 2 + 1)] << endl;
    }
  }
}
Problem p03158 (C++)

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

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;

const int INF = 1e9;
const int maxN = 1e3 + 5;

int n, Q;
int a[maxN];
int X[maxN];
bool check(int x, int m) {
  int z = a[n - m + 1];
  if (z < x)
    return false;
  int d = z - x;
  int y = x - d;
  int p = lower_bound(a + 1, a + 1 + n, y) - a;
  int q = n - m + 1;
  if ((q - p + 1) >= m) {
    return true;
  } else {
    return false;
  }
}
ll s[maxN];
ll t[maxN];
int main() {
  scanf("%d%d", &n, &Q);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    s[i] = s[i - 1] + (ll)a[i];
  }
  t[1] = a[1];
  t[2] = a[2];
  for (int i = 3; i <= n; i++) {
    t[i] = t[i - 2] + (ll)a[i];
  }
  for (int i = 1; i <= Q; i++) {
    scanf("%d", &X[i]);
    int L = 1, R = n;
    int ans = 0;
    ll cnt = 0;
    while (L <= R) {
      int mid = (L + R) / 2;
      if (check(X[i], mid)) {
        ans = mid;
        L = mid + 1;
      } else {
        R = mid - 1;
      }
    }
    int p = n - ans + 1;
    int q = n - 2 * ans + 1;
    cnt += (s[n] - s[p - 1]);
    cnt += t[q - 1];
    cout << cnt << endl;
  }
  return 0;
}

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

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;

const int INF = 1e9;
const int maxN = 1e5 + 5;

int n, Q;
int a[maxN];
int X[maxN];
bool check(int x, int m) {
  int z = a[n - m + 1];
  if (z < x)
    return false;
  int d = z - x;
  int y = x - d;
  int p = lower_bound(a + 1, a + 1 + n, y) - a;
  int q = n - m + 1;
  if ((q - p + 1) >= m) {
    return true;
  } else {
    return false;
  }
}
ll s[maxN];
ll t[maxN];
int main() {
  scanf("%d%d", &n, &Q);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    s[i] = s[i - 1] + (ll)a[i];
  }
  t[1] = a[1];
  t[2] = a[2];
  for (int i = 3; i <= n; i++) {
    t[i] = t[i - 2] + (ll)a[i];
  }
  for (int i = 1; i <= Q; i++) {
    scanf("%d", &X[i]);
    int L = 1, R = n;
    int ans = 0;
    ll cnt = 0;
    while (L <= R) {
      int mid = (L + R) / 2;
      if (check(X[i], mid)) {
        ans = mid;
        L = mid + 1;
      } else {
        R = mid - 1;
      }
    }
    int p = n - ans + 1;
    int q = n - 2 * ans + 1;
    cnt += (s[n] - s[p - 1]);
    cnt += t[q - 1];
    cout << cnt << endl;
  }
  return 0;
}