alexjercan/bugnet huggingface dataset:
Problem p00556 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define INF 1e+9
using namespace std;

int main() {
  int n, m, a[100000], cnt[20] = {}, imos[100001][20] = {};
  int dp[1 << 20] = {};
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    cnt[a[i]]++;
    for (int j = 0; j < m; j++)
      imos[i + 1][j] = imos[i][j];
    imos[i + 1][a[i]]++;
  }
  for (int i = 0; i < (1 << 20); i++) {
    dp[i] = INF;
  }
  dp[0] = 0;
  for (int i = 1; i < (1 << m); i++) {
    int sum = 0;
    for (int j = 0; j < m; j++) {
      if ((i >> j) & 1)
        sum += cnt[j];
    }
    for (int j = 0; j < m; j++) {
      if ((i >> j) & 1) {
        dp[i] = min(dp[i], dp[i & ~(1 << j)] + cnt[j] -
                               (imos[sum][j] - imos[sum - cnt[j]][j]));
      }
    }
  }
  cout << dp[(1 << m) - 1] << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#define INF 1e+9
using namespace std;

int main() {
  int n, m, a[100000], cnt[20] = {}, imos[100001][20] = {};
  int dp[1 << 20] = {};
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    cnt[a[i]]++;
    for (int j = 0; j < m; j++)
      imos[i + 1][j] = imos[i][j];
    imos[i + 1][a[i]]++;
  }
  for (int i = 0; i < (1 << 20); i++) {
    dp[i] = INF;
  }
  dp[0] = 0;
  for (int i = 1; i < (1 << n); i++) {
    int sum = 0;
    for (int j = 0; j < m; j++) {
      if ((i >> j) & 1)
        sum += cnt[j];
    }
    for (int j = 0; j < m; j++) {
      if ((i >> j) & 1) {
        dp[i] = min(dp[i], dp[i & ~(1 << j)] + cnt[j] -
                               (imos[sum][j] - imos[sum - cnt[j]][j]));
      }
    }
  }
  cout << dp[(1 << m) - 1] << endl;
  return 0;
}
Problem p00556 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

#define UNDEF (1 << 31)

using namespace std;

unsigned int dp[1048576];
int N, M, S[100000], R[20][100001];

bool ibs(int bits, int ref) { return ((1 << ref) & bits) == (1 << ref); }

int cbt(int bits, int ref) { return (((0xffffffff) xor (1 << ref)) & bits); }

int sbt(int bits, int ref) { return bits | (1 << ref); }

unsigned int getcount(int s, int l, int i) {}

unsigned int dfs(int bits,
                 int cnt) //???????????§???????????????????????????????????????
{
  if (dp[bits] == UNDEF) {
    int tb;
    for (int i = 0; i < M; i++) {
      if (ibs(bits, i)) {
        tb = cbt(bits, i);
        dp[bits] =
            min(dp[bits], dfs(tb, cnt - R[i][N]) +
                              (R[i][N] - (R[i][cnt] - R[i][cnt - R[i][N]])));
      }
    }
  }
  return dp[bits];
}

int main(void) {
  cin >> N >> M;
  for (int i = 0; i < 1048576; i++) {
    dp[i] = UNDEF;
  }
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
    S[i]--;
    for (int k = 0; k < 20; k++) {
      R[k][i + 1] = R[k][i];
    }
    R[S[i]][i + 1]++;
  }
  int ref = 0;
  for (int i = 0; i < M; i++) {
    ref = sbt(ref, i);
  }
  int res = dfs(ref, N);
  cout << res << endl;
}

Error: TLE

Fixed submission:

#include <iostream>

#define UNDEF (1 << 31)

using namespace std;

unsigned int dp[1048576];
int N, M, S[100000], R[20][100001];

bool ibs(int bits, int ref) { return ((1 << ref) & bits) == (1 << ref); }

int sbt(int bits, int ref) { return bits | (1 << ref); }

void dfs(int bits, int cnt) {
  {
    int tb;
    for (int i = 0; i < M; i++) {
      if (!ibs(bits, i)) {
        tb = sbt(bits, i);
        dp[tb] = min(dp[tb],
                     dp[bits] + (R[i][N] - (R[i][cnt + R[i][N]] - R[i][cnt])));
        dfs(tb, cnt + R[i][N]);
      }
    }
  }
}

int main(void) {
  cin >> N >> M;
  for (int i = 0; i < 1048576; i++) {
    dp[i] = UNDEF;
  }
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
    S[i]--;
    for (int k = 0; k < 20; k++) {
      R[k][i + 1] = R[k][i];
    }
    R[S[i]][i + 1]++;
  }
  dfs(0x00000000, 0);
  int ref = 0;
  for (int i = 0; i < M; i++) {
    ref = sbt(ref, i);
  }
  cout << dp[ref] << endl;
}
Problem p00556 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n, m, b[21][100001] = {};
int memo[1 << 20];
int dp(int k, int bit) {
  if (k == n)
    return 0;
  if (memo[bit] != -1)
    return memo[bit];
  int ret = INT_MAX;
  for (int i = 1; i <= m; i++) {
    if ((bit & 1 << i - 1) == 0) {
      int isum = n - b[i][n];
      int rui = b[i][k + isum] - b[i][k];
      ret = min(ret, dp(k + isum, bit | 1 << i - 1) + rui);
    }
  }
  return memo[bit] = ret;
}
int main() {
  scanf("%d %d", &n, &m);
  int a;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a);
    for (int j = 1; j <= m; j++) {
      if (j != a)
        b[j][i]++;
      b[j][i] += b[j][i - 1];
    }
  }
  fill(memo, memo + (1 << 20), -1);
  printf("%d\\n", dp(0, 0));
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m, sum, rui;
int b[21][100000];
int memo[1 << 20];
int minimum = INT_MAX;
void dp(int k, int bit, int total) {

  if (k != n) {
    for (int i = 1; i <= m; i++) {
      if ((bit & (1 << i - 1)) == 0) {
        sum = n - b[i][n - 1];
        rui = (k == 0) ? b[i][k + sum - 1] : b[i][k + sum - 1] - b[i][k - 1];
        // printf("!%d(%d~%d),%d ",i,k,k+sum-1,total+rui);
        dp(k + sum, bit | (1 << i - 1), total + rui);
      }
    }
  } else {
    minimum = min(minimum, total);
    // puts("");
  }
}
int main() {
  int a;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    for (int j = 1; j <= m; j++) {
      if (j != a)
        b[j][i]++;
      if (i != 0)
        b[j][i] += b[j][i - 1];
    }
  }

  dp(0, 0, 0);
  printf("%d\\n", minimum);

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

// Plush Toys
#include <climits>
#include <iostream>

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;

  int A[N + 1], cumsum[M][N + 1];
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N + 1; j++) {
      cumsum[i][j] = 0;
    }
  }

  for (int i = 1; i < N + 1; i++) {
    cin >> A[i];
    A[i]--;
    cumsum[A[i]][i]++;
  }

  for (int i = 0; i < M; i++) {
    for (int j = 1; j < N + 1; j++) {
      cumsum[i][j] += cumsum[i][j - 1];
    }
  }

  int dp[1 << M];
  for (int i = 0; i < (1 << M); i++)
    dp[i] = INT_MAX;
  dp[0] = 0;
  for (int s = 0; s < (1 << M); s++) {
    int p = 0;
    for (int i = 0; i < M; i++) {
      if (s & 1 << i)
        p += cumsum[i][N];
    }
    for (int i = 0; i < M; i++) {
      if (s & 1 << i)
        continue;
      int cost =
          dp[s] + (cumsum[i][N] - cumsum[i][p + cumsum[i][N]] + cumsum[i][p]);
      dp[s + (1 << i)] = min(dp[s + (1 << i)], cost);
    }
  }

  cout << dp[(1 << M) - 1] << endl;
}


Error: TLE

Fixed submission:

// Plush Toys
#include <algorithm>
#include <climits>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

int A[100000], B[21] = {};
int N, M;

int main() {
  cin >> N >> M;

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

  vector<int> v(M);
  iota(v.begin(), v.end(), 1);

  int cost = INT_MAX;
  do {
    int cnt = 0;
    int i = 0;

    for (auto j : v) {
      int k = B[j];
      while (k > 0 && cnt < cost) {
        if (A[i++] != j)
          cnt++;
        k--;
      }
    }
    cost = min(cost, cnt);
  } while (next_permutation(v.begin(), v.end()));

  cout << cost << endl;
}
Problem p00556 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  int T[N], C[N + 1][M];
  for (int i = 0; i <= N; ++i)
    fill(C[i], C[i] + M, 0);
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j)
      C[i + 1][j] = C[i][j];
    cin >> T[i];
    --T[i];
    ++C[i + 1][T[i]];
  }
  /*
  for(int i = 0; i < N; ++i){
    for(int j = 0; j < M; ++j) cout << C[i][j];
    cout << endl;
  }
  */
  int dp[1 << M], INF = 1000000000;
  fill(dp, dp + (1 << M), INF);
  dp[0] = 0;
  for (int i = 0; i < (1 << M); ++i) {
    int t = 0;
    for (int j = 0; j < M; ++j) {
      if ((i >> j) & 1)
        t += C[N][j];
    }
    for (int j = 0; j < M; ++j) {
      if (!((i >> j) & 1)) {
        int c = C[N][j], s = dp[i] + c;
        s -= C[t + c][j];
        // if(t > 0) s += C[t-1][j];
        s += C[t][j];
        // cout << i << " " << j << " " << t << " " << c << endl;
        dp[(i | (1 << j))] = min(dp[(i | (1 << j))], s);
      }
    }
  }
  // for(int i = 0; i < (1<<M); ++i) cout << dp[i] << " ";
  // cout << endl;
  cout << dp[(1 << M) - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  int T[N], C[M];
  fill(C, C + M, 0);
  for (int i = 0; i < N; ++i) {
    cin >> T[i];
    --T[i];
    ++C[T[i]];
  }

  int dp[1 << M], INF = 1000000000;
  fill(dp, dp + (1 << M), INF);
  dp[0] = 0;
  for (int i = 0; i < (1 << M); ++i) {
    int t = 0;
    for (int j = 0; j < M; ++j) {
      if (i >> j & 1)
        t += C[j];
    }
    for (int j = 0; j < M; ++j) {
      if (!((i >> j) & 1)) {
        int s = dp[i] + C[j];
        for (int k = t; k < t + C[j]; ++k) {
          if (T[k] == j)
            --s;
        }
        dp[i | (1 << j)] = min(dp[i | (1 << j)], s);
      }
    }
  }

  cout << dp[(1 << M) - 1] << endl;
  return 0;
}
Problem p00557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>

typedef std::pair<int, int> Pair;

constexpr int MAX_HW = 1000;
constexpr int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int H, W;
int field[MAX_HW][MAX_HW];
int tmp[MAX_HW][MAX_HW];

void bfs(int h, int w);

int main() {

  std::cin >> H >> W;

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      std::cin >> field[i][j];
    }
  }

  int ans = 0;

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      bool flag = true;
      for (int k = 0; k < 4; ++k) {
        int nh = i + dx[k], nw = j + dy[k];
        if (nh < 0 || nw < 0 || nh >= H || nw >= W) {
          continue;
        }
        if (field[nh][nw] < field[i][j]) {
          flag = false;
          break;
        }
      }
      if (flag) {
        bfs(i, j);
      }
    }
  }

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (tmp[i][j] > 1) {
        ++ans;
      }
    }
  }

  std::cout << ans << std::endl;

  return 0;
}

void bfs(int h, int w) {
  bool is_reached[MAX_HW][MAX_HW] = {};
  std::queue<Pair> que;
  que.push(Pair(h, w));

  while (!que.empty()) {
    Pair cur = que.front();
    que.pop();
    for (int i = 0; i < 4; ++i) {
      int nh = cur.first + dx[i], nw = cur.second + dy[i];
      if (nh < 0 || nw < 0 || nh >= H || nw >= W) {
        continue;
      }
      if (field[nh][nw] > field[cur.first][cur.second] && !is_reached[nh][nw]) {
        que.push(Pair(nh, nw));
        is_reached[nh][nw] = true;
        ++tmp[nh][nw];
      }
    }
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>

typedef std::pair<int, int> Pair;

constexpr int MAX_HW = 1000;
constexpr int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int H, W;
int field[MAX_HW][MAX_HW];

bool bfs(int h, int w);

int main() {

  std::cin >> H >> W;

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      std::cin >> field[i][j];
    }
  }

  int ans = 0;

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (bfs(i, j)) {
        ++ans;
      }
    }
  }

  std::cout << ans << std::endl;

  return 0;
}

bool bfs(int h, int w) {
  int puddle = 0;
  bool is_reached[MAX_HW][MAX_HW] = {};
  std::queue<Pair> que;
  que.push(Pair(h, w));

  while (!que.empty()) {
    Pair cur = que.front();
    que.pop();

    bool flag = true;
    for (int i = 0; i < 4; ++i) {
      int nh = cur.first + dx[i], nw = cur.second + dy[i];
      if (nh < 0 || nw < 0 || nh >= H || nw >= W) {
        continue;
      }
      if (field[nh][nw] < field[cur.first][cur.second]) {
        que.push(Pair(nh, nw));
        flag = false;
      }
    }
    if (flag && !is_reached[cur.first][cur.second]) {
      is_reached[cur.first][cur.second] = true;
      ++puddle;
    }
  }

  if (puddle > 1) {
    return true;
  }

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

#include <algorithm>
#include <cstdio>
#include <vector>

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

using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> PP;

const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};

int h, w;
int m[1000][1000];
int mem[1000][1000];

int type(int i, int j) {
  rep(k, 4) {
    int p = i + dx[k];
    int q = j + dy[k];
    if (0 <= p && p < h && 0 <= q && q < w && m[p][q] < m[i][j]) {
      int t = mem[p][q];
      if (t == 0 || (mem[i][j] != -1 && mem[i][j] != t)) {
        return mem[i][j] = 0;
      }
      mem[i][j] = t;
    }
  }
  return mem[i][j] != -1 ? mem[i][j] : mem[i][j] = m[i][j];
}

int main() {
  scanf("%d%d", &h, &w);
  rep(i, h) {
    rep(j, w) { scanf("%d", &m[i][j]); }
  }
  rep(i, h) { fill_n(mem[i], w, -1); }
  vector<PP> v;
  rep(i, h) {
    rep(j, w) { v.push_back(PP(m[i][j], P(i, j))); }
  }
  sort(v.begin(), v.end());
  int ans = 0;
  for (vector<PP>::iterator it = v.begin(); it != v.end(); ++it) {
    ans += type(it->second.first, it->second.second) == 0;
  }
  printf("%d\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>

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

using namespace std;

const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};

int h, w;
int m[1000][1000];
int mem[1000][1000];

int type(int i, int j) {
  if (mem[i][j] != -1) {
    return mem[i][j];
  }
  rep(k, 4) {
    int p = i + dx[k];
    int q = j + dy[k];
    if (0 <= p && p < h && 0 <= q && q < w && m[p][q] < m[i][j]) {
      int t = type(p, q);
      if (t == 0 || (mem[i][j] != -1 && mem[i][j] != t)) {
        return mem[i][j] = 0;
      }
      mem[i][j] = t;
    }
  }
  if (mem[i][j] == -1) {
    mem[i][j] = m[i][j];
  }
  return mem[i][j];
}

int main() {
  scanf("%d%d", &h, &w);
  rep(i, h) {
    rep(j, w) { scanf("%d", &m[i][j]); }
  }
  fill_n((int *)mem, 1000 * 1000, -1);
  int ans = 0;
  rep(i, h) {
    rep(j, w) { ans += type(i, j) == 0; }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p00557 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int h, w;
int mp[1001][1001];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int used[1001][1001], mem[1001][1001];

int dfs(int y, int x) {

  if (used[y][x]++)
    return mem[y][x];

  int &res = mem[y][x] = 0;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx < 0 || nx >= w || ny < 0 || ny >= h || mp[y][x] < mp[ny][nx])
      continue;
    int r = dfs(ny, nx);
    if (r == 0)
      r = ny * w + nx + 1;
    if (r == -1)
      res = -1;
    if (r && res && res != r)
      res = -1;
    else if (r)
      res = r;
  }
  return res;
}

typedef pair<int, int> P;
P A[1001 * 1001];
int main() {

  cin >> h >> w;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> mp[i][j], A[i * w + j] = P(mp[i][j], i * w + j);

  sort(A, A + h * w);

  int ans = 0;
  for (int i = 0; i < h * w; i++)
    if (dfs(A[i].second / w, A[i].second % w) == -1)
      ans++;

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int h, w;
int mp[1001][1001];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int used[1001][1001], mem[1001][1001];

int dfs(int y, int x) {

  if (used[y][x]++)
    return mem[y][x];

  int &res = mem[y][x] = 0;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx < 0 || nx >= w || ny < 0 || ny >= h || mp[y][x] < mp[ny][nx])
      continue;
    int r = dfs(ny, nx);
    if (r == 0)
      r = ny * w + nx + 1;
    if (r == -1)
      res = -1;
    if (r && res && res != r)
      res = -1;
    else if (r)
      res = r;
  }
  return res;
}

int main() {

  cin >> h >> w;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> mp[i][j];

  int ans = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if (dfs(i, j) == -1)
        ans++;
    }
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
map<int, set<int>> m;
typedef pair<int, int> P;
typedef pair<int, P> PP;
int h, w;
bool ok[1111][1111];
bool in(int y, int x) { return (0 <= y && y < h && 0 <= x && x < w); }
int main() {
  cin >> h >> w;
  int s[h][w];
  priority_queue<PP> q;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> s[i][j];
      q.push(PP(-s[i][j], P(i, j)));
    }
  }
  int ans = 0;
  int ax[] = {1, -1, 0, 0};
  int ay[] = {0, 0, 1, -1};
  while (!q.empty()) {
    PP pp = q.top();
    q.pop();
    P p = pp.second;
    int t = -pp.first, y = p.first, x = p.second;
    for (int k = 0; k < 4; k++) {
      if (!in(y + ay[k], x + ax[k]))
        continue;
      int z = s[y + ay[k]][x + ax[k]];
      if (z > t)
        continue;
      if (m.find(z) != m.end()) {
        if (m[z].size() == 1)
          m[t].insert(*m[z].begin());
        else
          ok[y][x] = 1;
      } else
        m[t].insert(z);
    }
    if (m.find(t) != m.end())
      ok[y][x] |= m[t].size() > 1;
    if (ok[y][x]) {
      ans++;
      m[t].clear();
    }
  }

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

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
map<int, set<int>> m;
typedef pair<int, int> P;
typedef pair<int, P> PP;
int h, w;
bool in(int y, int x) { return (0 <= y && y < h && 0 <= x && x < w); }
int main() {

  cin >> h >> w;
  int s[h][w];
  priority_queue<PP> q;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> s[i][j];
      q.push(PP(-s[i][j], P(i, j)));
    }
  }
  int ans = 0;
  int ax[] = {1, -1, 0, 0};
  int ay[] = {0, 0, 1, -1};
  while (!q.empty()) {
    PP pp = q.top();
    q.pop();
    P p = pp.second;
    int t = -pp.first, y = p.first, x = p.second;
    for (int k = 0; k < 4; k++) {
      if (!in(y + ay[k], x + ax[k]))
        continue;
      int z = s[y + ay[k]][x + ax[k]];
      if (z > t)
        continue;
      if (m.find(z) != m.end()) {
        for (set<int>::iterator it = m[z].begin(); it != m[z].end(); ++it)
          m[t].insert(*it);
      } else
        m[t].insert(z);
    }
    if (m.find(t) != m.end())
      ans += m[t].size() > 1;
  }

  cout << ans << endl;
  return 0;
}
Problem p00557 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
using namespace std;

typedef pair<int, int> P;

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int h, w, res = 0, pos = 0;
int field[1010][1010];
int cnt[1010][1010];
bool used[1010][1010];
stack<P> que;
queue<P> visit;

int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      scanf("%d", &field[i][j]);
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      bool flag = true;
      for (int k = 0; k < 4; k++) {
        int nx = i + dx[k], ny = j + dy[k];
        if (nx >= 0 && nx < h && ny >= 0 && ny < w &&
            field[nx][ny] < field[i][j])
          flag = false;
      }
      if (flag) {
        que.push(P(i, j));
        while (!que.empty()) {
          P p = que.top();
          que.pop();
          int x = p.first, y = p.second;
          visit.push(P(x, y));
          for (int i = 0; i < 4; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if (nx >= 0 && nx < h && ny >= 0 && ny < w &&
                field[nx][ny] > field[x][y] && !used[nx][ny]) {
              cnt[nx][ny]++;
              if (cnt[nx][ny] < 3) {
                used[nx][ny] = true;
                que.push(P(nx, ny));
              }
            }
          }
        }
      }
      while (!visit.empty()) {
        int x = visit.front().first, y = visit.front().second;
        visit.pop();
        used[x][y] = false;
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      if (cnt[i][j] >= 2)
        res++;
  }
  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
using namespace std;

typedef pair<int, int> P;

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int h, w, res = 0, pos = 0;
int field[1010][1010];
int cnt[1010][1010];
bool used[1010][1010];
queue<P> que;

void dfs(int x, int y) {
  used[x][y] = true;
  que.push(P(x, y));
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx >= 0 && nx < h && ny >= 0 && ny < w && field[nx][ny] > field[x][y] &&
        !used[nx][ny]) {
      cnt[nx][ny]++;
      if (cnt[nx][ny] < 3)
        dfs(nx, ny);
    }
  }
}

int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      scanf("%d", &field[i][j]);
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      bool flag = true;
      for (int k = 0; k < 4; k++) {
        int nx = i + dx[k], ny = j + dy[k];
        if (nx >= 0 && nx < h && ny >= 0 && ny < w &&
            field[nx][ny] < field[i][j])
          flag = false;
      }
      if (flag)
        dfs(i, j);
      while (!que.empty()) {
        int x = que.front().first, y = que.front().second;
        que.pop();
        used[x][y] = false;
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      if (cnt[i][j] >= 2)
        res++;
  }
  cout << res << endl;
  return 0;
}