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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

map<int, int> treeA;
map<int, int> treeB;
map<int, int> treeC;

void getTree(string line, int tree, int lvl) {
  if (line == "" || line == "(" || line == ")")
    return;
  int b = 0;
  stack<int> cnt;
  while (line[b] != 0) {
    if (line[b] == '(') {
      cnt.push(1);
    } else if (line[b] == ')') {
      cnt.pop();
    } else {
      if (cnt.size() == 1)
        break;
    }
    b++;
  }
  string S_R = "", S_L = "";
  for (int x = 1; x < b; ++x) {
    S_L += line[x];
  }
  for (int x = b + 1; x < line.size() - 1; ++x) {
    S_R += line[x];
  }
  if (S_L != "") {
    getTree(S_L, tree, lvl * 2);
  }
  if (S_R != "") {
    getTree(S_R, tree, lvl * 2 + 1);
  }
  if (tree == 1) {
    treeA[lvl] = 1;
  } else {
    treeB[lvl] = 2;
  }
}

void uniont() {
  for (map<int, int>::iterator it = treeA.begin(); it != treeA.end(); ++it) {
    treeC[it->first] = 3;
  }
  for (map<int, int>::iterator it = treeB.begin(); it != treeB.end(); ++it) {
    treeC[it->first] = 3;
  }
}

int locate(int n) {
  for (map<int, int>::iterator it = treeB.begin(); treeB.end() != it; ++it) {
    if (n == it->first)
      return 0;
    if (it->first > n)
      return -1;
  }
  return -1;
}

void intert() {
  for (map<int, int>::iterator it = treeA.begin(); it != treeA.end(); ++it) {
    if (locate(it->first) != -1) {
      treeC[it->first] = 3;
    }
  }
}

string imp(int lvl) {
  if (treeC[lvl] == 0)
    return "";
  stringstream buf;
  buf << "(" << imp(lvl * 2) << "," << imp(lvl * 2 + 1) << ")";
  return buf.str();
}

int main() {
  char l;
  string s1, s2;
  while (scanf(" %c ", &l) != EOF) {
    cin >> s1 >> s2;
    treeA.clear();
    treeB.clear();
    treeC.clear();
    getTree(s1, 1, 1);
    getTree(s2, 2, 1);
    if (l == 'u') {
      uniont();
    } else {
      intert();
    }
    cout << imp(1) << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int treeA[500];
int treeB[500];
int treeC[500];

void getTree(string line, int tree, int lvl) {
  if (line == "" || line == "(" || line == ")")
    return;
  stack<int> cnt;
  int b = 0;
  while (line[b] != 0) {
    if (line[b] == '(') {
      cnt.push(1);
    } else if (line[b] == ')') {
      cnt.pop();
    } else {
      if (cnt.size() == 1)
        break;
    }
    b++;
  }
  string S_R = "";
  string S_L = "";
  for (int x = 1; x < b; ++x) {
    S_L += line[x];
  }
  for (int x = b + 1; x < line.size() - 1; ++x) {
    S_R += line[x];
  }
  if (S_R != "") {
    getTree(S_R, tree, 2 * lvl + 1);
  }
  if (S_L != "") {
    getTree(S_L, tree, 2 * lvl);
  }
  if (tree == 1) {
    treeA[lvl] = 1;
  } else {
    treeB[lvl] = 2;
  }
}

string imp(int lvl) {
  if (lvl >= 500)
    return "";
  if (treeC[lvl] == 0)
    return "";
  stringstream buf;
  buf << "(" << imp(2 * lvl) << "," << imp(2 * lvl + 1) << ")";
  return buf.str();
}

void unionT() {
  for (int x = 1; x < 500; ++x) {
    if (treeA[x] || treeB[x])
      treeC[x] = 3;
  }
  cout << imp(1) << endl;
}

void inter() {
  for (int x = 1; x < 500; ++x) {
    if (treeA[x] && treeB[x])
      treeC[x] = 3;
  }
  cout << imp(1) << endl;
}

int main() {
  char l;
  string s1, s2;
  while (scanf(" %c ", &l) != EOF) {
    cin >> s1 >> s2;
    cout << s1 << endl;
    cout << s2 << endl;
    for (int x = 0; x < 500; ++x) {
      treeA[x] = 0;
      treeB[x] = 0;
      treeC[x] = 0;
    }
    getTree(s1, 1, 1);
    getTree(s2, 2, 1);
    if (l == 'u') {
      unionT();
    } else {
      inter();
    }
  }
  return 0;
}