Problem p00557 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
//---------------------------------------------------
//????????????????????????????????????
#define int long long
#define str string
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
typedef long long ll;
typedef long double ld;
const ll inf = 4523372036854775807;
const ll Mod = 1000000007;
const ll gosenchou = 5000000000000000;
short gh[2][4] = {{0, 0, -1, 1}, {-1, 1, 0, 0}};
struct P {
  ll pos, cost;
};
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
struct B { //??£??\\???????????¨???
  ll to, cost;
};
struct S { //???????????±?????\\???????????°
  int from, to, cost;
};
struct H {
  int x, y;
};
bool operator<(H a, H b) {
  if (a.x != b.x)
    return a.x < b.x;
  return a.y < b.y;
}
bool operator>(H a, H b) {
  if (a.x != b.x)
    return a.x > b.x;
  return a.y > b.y;
}
ll gcm(ll i, ll j) { //?????§??¬?´???°
  if (i > j)
    swap(i, j);
  if (i == 0)
    return j;
  return gcm(j % i, i);
}
ld rad(ld a, ld b, ld c, ld d) {
  return sqrt(pow(a - c, 2) + pow(b - d, 2));
} // rad?????§?¨???????2??????????????¢
int ari(int a, int b, int c) { return (a + b) * c / 2; } //????????°?????????
bool suf(ld a, ld b, ld c, ld d) {
  if (b <= c || d <= a)
    return 0;
  return 1;
} //[a,b),[c,d)
//---------------------------------------------------
//+++++++++++++++++++++++++++++++++++++++++++++++++++
int h, w, a[1000][1000];
H b[1000001];
signed main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> a[i][j];
  priority_queue<H, vector<H>, greater<H>> p;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      b[a[i][j]] = H{i, j};
      if (i > 0 && a[i - 1][j] < a[i][j])
        continue;
      if (i < h - 1 && a[i + 1][j] < a[i][j])
        continue;
      if (j > 0 && a[i][j - 1] < a[i][j])
        continue;
      if (j < w - 1 && a[i][j + 1] < a[i][j])
        continue;
      p.push(H{a[i][j], a[i][j]});
    }
  }
  int ans = 0;
  while (!p.empty()) {
    int f = p.top().x, sum = 0;
    int x = b[f].x, y = b[f].y;
    while (!p.empty() && p.top().x == f) {
      int t = p.top().y;
      while (!p.empty() && p.top().y == t && p.top().x == f) {
        p.pop();
      }
      if (x > 0 && a[x - 1][y] > a[x][y])
        p.push(H{a[x - 1][y], t});
      if (x < h - 1 && a[x + 1][y] > a[x][y])
        p.push(H{a[x + 1][y], t});
      if (y > 0 && a[x][y - 1] > a[x][y])
        p.push(H{a[x][y - 1], t});
      if (y < w - 1 && a[x][y + 1] > a[x][y])
        p.push(H{a[x][y + 1], t});
      sum++;
    }
    if (sum > 1)
      ans++;
  }
  cout << ans << endl;
  getchar();
  getchar();
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
//---------------------------------------------------
//????????????????????????????????????
#define int long long
#define str string
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
typedef long long ll;
typedef long double ld;
const ll inf = 4523372036854775807;
const ll Mod = 1000000007;
const ll gosenchou = 5000000000000000;
short gh[2][4] = {{0, 0, -1, 1}, {-1, 1, 0, 0}};
struct P {
  ll pos, cost;
};
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
struct B { //??£??\\???????????¨???
  ll to, cost;
};
struct S { //???????????±?????\\???????????°
  int from, to, cost;
};
struct H {
  int x, y;
};
bool operator<(H a, H b) {
  if (a.x != b.x)
    return a.x < b.x;
  return a.y < b.y;
}
ll gcm(ll i, ll j) { //?????§??¬?´???°
  if (i > j)
    swap(i, j);
  if (i == 0)
    return j;
  return gcm(j % i, i);
}
ld rad(ld a, ld b, ld c, ld d) {
  return sqrt(pow(a - c, 2) + pow(b - d, 2));
} // rad?????§?¨???????2??????????????¢
int ari(int a, int b, int c) { return (a + b) * c / 2; } //????????°?????????
bool suf(ld a, ld b, ld c, ld d) {
  if (b <= c || d <= a)
    return 0;
  return 1;
} //[a,b),[c,d)
//---------------------------------------------------
//+++++++++++++++++++++++++++++++++++++++++++++++++++
set<int> c[1000][1000];
int h, w, a[1000][1000];
H b[100001];
void solve(int i, int j, int x, int y) {
  set<int>::iterator it = c[x][y].begin();
  while (it != c[x][y].end()) {
    c[i][j].insert((*it));
    it++;
  }
}
signed main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      b[a[i][j]] = H{i, j};
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (i != 0 && a[i - 1][j] < a[i][j])
        continue;
      if (i < h - 1 && a[i + 1][j] < a[i][j])
        continue;
      if (j != 0 && a[i][j - 1] < a[i][j])
        continue;
      if (j < w - 1 && a[i][j + 1] < a[i][j])
        continue;
      c[i][j].insert(a[i][j]);
    }
  }
  for (int i = 1; i <= h * w; i++) {
    int x = b[i].x, y = b[i].y;
    if (x > 0 && a[x - 1][y] > a[x][y])
      solve(x - 1, y, x, y);
    if (x < h - 1 && a[x + 1][y] > a[x][y])
      solve(x + 1, y, x, y);
    if (y > 0 && a[x][y - 1] > a[x][y])
      solve(x, y - 1, x, y);
    if (y < w - 1 && a[x][y + 1] > a[x][y])
      solve(x, y + 1, x, y);
  }
  int sum = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (c[i][j].size() >= 2)
        sum++;
    }
  }
  cout << sum << endl;
  getchar();
  getchar();
}