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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  Info(int arg_row, int arg_col) {
    row = arg_row;
    col = arg_col;
  }
  bool operator<(const struct Info &arg) const {
    if (row != arg.row) {
      return row < arg.row;
    } else {
      return col < arg.col;
    }
  };
  int row, col;
};

struct Data {
  Data(int arg_row, int arg_col, int arg_height) {
    row = arg_row;
    col = arg_col;
    height = arg_height;
  }
  bool operator<(const struct Data &arg) const { return height < arg.height; };

  int row, col, height;
};

struct Shishimaru {
  vector<Info> info;
};

int H, W;
int diff_row[4] = {-1, 0, 0, 1}, diff_col[4] = {0, -1, 1, 0};
int memo[1000][1000], table[1000][1000];
Shishimaru shishimaru[1000][1000];

bool rangeCheck(int row, int col) {
  if (row >= 0 && row <= H - 1 && col >= 0 && col <= W - 1)
    return true;
  else {
    return false;
  }
}

vector<Info> recursive(int row, int col, int depth) {

  if (memo[row][col] != -1) {
    return shishimaru[row][col].info;
  }

  vector<Info> sum, catcher;

  int adj_row, adj_col;

  for (int i = 0; i < 4; i++) {
    adj_row = row + diff_row[i];
    adj_col = col + diff_col[i];

    if (rangeCheck(adj_row, adj_col) == false ||
        table[adj_row][adj_col] > table[row][col])
      continue;

    catcher = recursive(adj_row, adj_col, depth + 1);

    for (int k = 0; k < catcher.size(); k++) {
      sum.push_back(catcher[k]);
    }
    catcher.clear();
  }

  if (sum.size() == 0) {
    memo[row][col] = 0;
    if (depth > 0) {
      shishimaru[row][col].info.push_back(Info(row, col));
      sum.push_back(Info(row, col));
    }
    return sum;
  }

  sort(sum.begin(), sum.end());

  int pre_row = sum[0].row, pre_col = sum[0].col;
  shishimaru[row][col].info.push_back(sum[0]);

  for (int i = 1; i < sum.size(); i++) {
    if (sum[i].row != pre_row || sum[i].col != pre_col) {
      shishimaru[row][col].info.push_back(sum[i]);
    }
    pre_row = sum[i].row;
    pre_col = sum[i].col;
  }

  memo[row][col] = shishimaru[row][col].info.size();

  return shishimaru[row][col].info;
}

int main() {

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

  priority_queue<Data> Q;

  for (int row = 0; row < H; row++) {
    for (int col = 0; col < W; col++) {
      scanf("%d", &table[row][col]);
      Q.push(Data(row, col, table[row][col]));
      memo[row][col] = -1;
    }
  }

  while (!Q.empty()) {
    if (memo[Q.top().row][Q.top().col] == -1) {
      recursive(Q.top().row, Q.top().col, 0);
    }
    Q.pop();
  }

  int ans = 0;
  for (int row = 0; row < H; row++) {
    for (int col = 0; col < W; col++) {
      if (memo[row][col] > 1) {
        ans++;
      }
    }
  }

  printf("%d\\n", ans);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  Info(int arg_row, int arg_col) {
    row = arg_row;
    col = arg_col;
  }
  bool operator<(const struct Info &arg) const {
    if (row != arg.row) {
      return row < arg.row;
    } else {
      return col < arg.col;
    }
  };
  int row, col;
};

struct Data {
  Data(int arg_row, int arg_col, int arg_height) {
    row = arg_row;
    col = arg_col;
    height = arg_height;
  }
  bool operator<(const struct Data &arg) const { return height < arg.height; };

  int row, col, height;
};

int H, W;
int diff_row[4] = {-1, 0, 0, 1}, diff_col[4] = {0, -1, 1, 0};
int memo[1000][1000], table[1000][1000];

bool rangeCheck(int row, int col) {
  if (row >= 0 && row <= H - 1 && col >= 0 && col <= W - 1)
    return true;
  else {
    return false;
  }
}

vector<Info> recursive(int row, int col, int depth) {

  vector<Info> sum, catcher, ret;

  int adj_row, adj_col;

  for (int i = 0; i < 4; i++) {
    adj_row = row + diff_row[i];
    adj_col = col + diff_col[i];

    if (rangeCheck(adj_row, adj_col) == false ||
        table[adj_row][adj_col] > table[row][col])
      continue;

    catcher = recursive(adj_row, adj_col, depth + 1);

    for (int k = 0; k < catcher.size(); k++) {
      sum.push_back(catcher[k]);
    }
    catcher.clear();
  }

  if (sum.size() == 0) {
    memo[row][col] = 0;
    if (depth > 0) {
      sum.push_back(Info(row, col));
    }
    return sum;
  }

  sort(sum.begin(), sum.end());

  int pre_row = sum[0].row, pre_col = sum[0].col;
  ret.push_back(sum[0]);

  for (int i = 1; i < sum.size(); i++) {
    if (sum[i].row != pre_row || sum[i].col != pre_col) {
      ret.push_back(sum[i]);
    }
    pre_row = sum[i].row;
    pre_col = sum[i].col;
  }

  memo[row][col] = ret.size();

  return ret;
}

int main() {

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

  priority_queue<Data> Q;

  for (int row = 0; row < H; row++) {
    for (int col = 0; col < W; col++) {
      scanf("%d", &table[row][col]);
      Q.push(Data(row, col, table[row][col]));
      memo[row][col] = -1;
    }
  }

  while (!Q.empty()) {
    if (memo[Q.top().row][Q.top().col] == -1) {
      recursive(Q.top().row, Q.top().col, 0);
    }
    Q.pop();
  }

  int ans = 0;
  for (int row = 0; row < H; row++) {
    for (int col = 0; col < W; col++) {
      if (memo[row][col] > 1) {
        ans++;
      }
    }
  }

  printf("%d\\n", ans);

  return 0;
}