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

// #define MYDEBUG
#include <bits/stdc++.h>
#ifdef MYDEBUG
#define debug(x) cout << #x << ": " << x << endl
#define ifcin(x) std::ifstream cin(x)
#else
#define debug(x)
#define ifcin(x)
#endif
#define ll long long
#define ull unsigned long long
#define eps 1e-14
#define all(x) x.begin(), x.end()
#define rep(i, from, to) for (int i = from; i < to; ++i)
#define REP(i, from, to) for (int i = from; i <= to; ++i)
using std::cin;
using std::cout;
using std::endl;
using std::max;
using std::min;
using std::ostream;
using std::queue;
using std::string;
using std::swap;
using std::vector;
typedef struct edge edge;
typedef struct unionfind unionfind;
typedef struct Graph Graph;

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  size_t last = v.size() - 1;
  for (size_t i = 0; i < v.size(); ++i) {
    out << v[i];
    if (i != last)
      out << ",";
  }
  out << "]";
  return out;
}

struct unionfind {
  vector<int> par;
  vector<int> rank;
  unionfind(int n) {
    par = vector<int>(n);
    rank = vector<int>(n);
    rep(i, 0, n) { // 0????????????
      par[i] = i;
      rank[i] = 1;
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    }
    return par[x] = find(par[x]);
  }

  bool same(int x, int y) { return find(x) == find(y); }

  void unite(int x, int y) { // rank[x] > rank[y]?????????
    x = find(x);
    y = find(y);
    if (x == y) {
      return;
    }
    if (rank[x] < rank[y]) {
      swap(x, y);
    }
    par[y] = x;
    rank[x] += rank[y];
    rank[y] = 0;
  }

  int size(int x) { return rank[x]; }
};

struct edge {
  int from, to;
  double cost;
  edge(int from, int to, double cost) {
    this->from = from;
    this->to = to;
    this->cost = cost;
  }

  edge() { *this = edge(-1, -1, -1); }

  bool operator<(const edge &e) const { return cost < e.cost; }
};

class Graph {
public:
  static double kruskal(vector<edge> &es, int n) {
    sort(all(es));
    double min_cost = 0;
    unionfind uf(n);
    rep(i, 0, es.size()) {
      edge &e = es[i];
      if (!uf.same(e.from, e.to)) {
        min_cost += e.cost;
        uf.unite(e.from, e.to);
      }
    }
    return min_cost;
  }
};

int id_global;
const int maxa = 32768;
int dp[maxa];
struct area {
  string str;
  vector<area> child;
  int depth, popu, id, half;
  area(string str) {
    this->str = str;
    this->id = id_global;
    id_global++;
    child = vector<area>();
    divide(str);
    popu = 0;
    half = 0;
  }
  int getPopulation() {
    if (child.size() == 0) {
      string s = str;
      s = s.erase(0, 1);
      s = s.erase(s.size() - 1, 1);
      return popu = std::atoi(s.c_str());
    } else {
      int ret = 0;
      rep(i, 0, child.size()) { ret += child[i].getPopulation(); }
      return popu = ret;
    }
  }
  area() { *this = area(""); }
  bool operator<(const area &a) const { return half < a.half; }

  void overHalf(area &a) {
    if (a.child.size() != 0) {
      for (size_t i = 0; i < a.child.size(); ++i) {
        overHalf(a.child[i]);
      }
      sort(all(a.child));
      a.half = 0;
      rep(i, 0, (a.child.size() + 1) / 2) {
        a.half += a.child[i].half;
        // cout << a.half << "+=" << a.child[i].str << endl;
      }
      // cout << a.str << ": " << a.half << endl;
    } else {
      a.half = (a.popu + 1) / 2;
    }
  }
  void divide(string s) {
    if (s[0] == '[' && s[1] != '[') {
      return;
    }
    string str = "";
    int depth = 0;
    for (size_t i = 1; i + 1 < s.size(); ++i) {
      str += s[i];
      if (s[i] == '[') {
        depth++;
      } else if (s[i] == ']') {
        depth--;
      }
      if (depth == 0 && str != "") {
        child.push_back(area(str));
        str = "";
      } else if (depth < 0) {
        break;
      }
    }
  }
};
typedef struct area area;
area root;
string s;
int n;
void solve() {
  ifcin("/storage/emulated/0/AppProjects/AtCoder/jni/in.txt");
  cin >> n;
  rep(test_case, 0, n) {
    cin >> s;
    id_global = 0;
    memset(dp, 0, sizeof(dp));
    root = area(s);
    root.getPopulation();
    root.overHalf(root);
    cout << root.half << endl;
  }
}

int main() { solve(); }

Error: 0

Fixed submission:

// #define MYDEBUG
#include <bits/stdc++.h>
#ifdef MYDEBUG
#define debug(x) cout << #x << ": " << x << endl
#define ifcin(x) std::ifstream cin(x)
#else
#define debug(x)
#define ifcin(x)
#endif
#define ll long long
#define ull unsigned long long
#define eps 1e-14
#define all(x) x.begin(), x.end()
#define rep(i, from, to) for (int i = from; i < to; ++i)
#define REP(i, from, to) for (int i = from; i <= to; ++i)
using std::cin;
using std::cout;
using std::endl;
using std::max;
using std::min;
using std::ostream;
using std::queue;
using std::string;
using std::swap;
using std::vector;
typedef struct edge edge;
typedef struct unionfind unionfind;
typedef struct Graph Graph;

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  size_t last = v.size() - 1;
  for (size_t i = 0; i < v.size(); ++i) {
    out << v[i];
    if (i != last)
      out << ",";
  }
  out << "]";
  return out;
}

struct unionfind {
  vector<int> par;
  vector<int> rank;
  unionfind(int n) {
    par = vector<int>(n);
    rank = vector<int>(n);
    rep(i, 0, n) { // 0????????????
      par[i] = i;
      rank[i] = 1;
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    }
    return par[x] = find(par[x]);
  }

  bool same(int x, int y) { return find(x) == find(y); }

  void unite(int x, int y) { // rank[x] > rank[y]?????????
    x = find(x);
    y = find(y);
    if (x == y) {
      return;
    }
    if (rank[x] < rank[y]) {
      swap(x, y);
    }
    par[y] = x;
    rank[x] += rank[y];
    rank[y] = 0;
  }

  int size(int x) { return rank[x]; }
};

struct edge {
  int from, to;
  double cost;
  edge(int from, int to, double cost) {
    this->from = from;
    this->to = to;
    this->cost = cost;
  }

  edge() { *this = edge(-1, -1, -1); }

  bool operator<(const edge &e) const { return cost < e.cost; }
};

class Graph {
public:
  static double kruskal(vector<edge> &es, int n) {
    sort(all(es));
    double min_cost = 0;
    unionfind uf(n);
    rep(i, 0, es.size()) {
      edge &e = es[i];
      if (!uf.same(e.from, e.to)) {
        min_cost += e.cost;
        uf.unite(e.from, e.to);
      }
    }
    return min_cost;
  }
};

int id_global;
const int maxa = 32768;
int dp[maxa];
struct area {
  string str;
  vector<area> child;
  int depth, popu, id;
  area(string str) {
    this->str = str;
    this->id = id_global;
    id_global++;
    child = vector<area>();
    divide(str);
  }
  int getPopulation() {
    if (child.size() == 0) {
      string s = str;
      s = s.erase(0, 1);
      s = s.erase(s.size() - 1, 1);
      return popu = std::atoi(s.c_str());
    } else {
      int ret = 0;
      rep(i, 0, child.size()) { ret += child[i].getPopulation(); }
      return popu = ret;
    }
  }
  area() { *this = area(""); }
  bool operator<(const area &a) const { return popu < a.popu; }
  void sortChild(area &a) {
    if (a.child.size() != 0) {
      sort(all(a.child));
      rep(i, 0, child.size()) { sortChild(a.child[i]); }
    }
  }
  int overHalf(area a) {
    if (dp[a.id] != 0) {
      return dp[a.id];
    }
    int ret = 0;
    int half = (a.child.size() + 1) / 2;
    if (half == 0) {
      ret = (a.popu + 1) / 2;
    } else {
      rep(i, 0, half) { ret += overHalf(a.child[i]); }
    }
    return dp[a.id] = ret;
  }
  void divide(string s) {
    if (s[0] == '[' && s[1] != '[') {
      return;
    }
    string str = "";
    int depth = 0;
    for (size_t i = 1; i + 1 < s.size(); ++i) {
      str += s[i];
      if (s[i] == '[') {
        depth++;
      } else if (s[i] == ']') {
        depth--;
      }
      if (depth == 0 && str != "") {
        child.push_back(area(str));
        str = "";
      } else if (depth < 0) {
        break;
      }
    }
  }
};
typedef struct area area;
area root;
string s;
int n;
void solve() {
  ifcin("/storage/emulated/0/AppProjects/AtCoder/jni/in.txt");
  cin >> n;
  rep(test_case, 0, n) {
    cin >> s;
    id_global = 0;
    memset(dp, 0, sizeof(dp));
    root = area(s);
    root.getPopulation();
    root.sortChild(root);
    cout << root.overHalf(root) << endl;
    debug(id_global);
  }
}

int main() { solve(); }