Problem p00726 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define INF 0x334334334LL

typedef long long int LLI;
typedef pair<int, LLI> Pair;

int n;
int I;
int num_node;
int ls[1145141];
char buf[1145141];
bool is_leaf[1145141];
LLI size[1145141];
vector<Pair> es[1145141];

int dfs1(int k, int idx) {
  char *res;
  es[idx].clear();
  size[idx] = 0;
  while (k < n && buf[k] != ')') {
    int g = num_node++;
    LLI t = strtol(buf + k, &res, 10);
    if (buf + k == res) {
      t = 1;
      size[g] = 0;
      ls[g] = k;
      is_leaf[g] = true;
      while (k < n && 'A' <= buf[k] && buf[k] <= 'Z') {
        size[g]++;
        k++;
      }
    } else {
      is_leaf[g] = false;
      if (*res == '(') {
        k = dfs1(res - buf + 1, g);
      } else {
        k = res - buf;
        size[g] = 0;
        ls[g] = k;
        is_leaf[g] = true;
        while (k < n && 'A' <= buf[k] && buf[k] <= 'Z') {
          size[g]++;
          k++;
        }
      }
    }
    es[idx].push_back(Pair(g, t));
    size[idx] = min(INF, size[idx] + size[g] * t);
  }
  return k + 1;
}

void dfs2(int v, int I) {
  // printf("I: %d\\n", I);
  // printf("size[%d]: %lld\\n", v, size[v]);
  // printf("is_leaf[%d]: %d\\n", v, is_leaf[v]);
  // if (is_leaf[v]) printf("ls[%d]: %d\\n", v, ls[v]);
  if (is_leaf[v] && I < size[v]) {
    printf("%c\\n", buf[ls[v] + I]);
    return;
  }

  bool found = false;
  // printf("%d:", v);
  for (int i = 0; i < es[v].size(); i++) {
    Pair p = es[v][i];
    int u = p.first;
    LLI t = p.second;

    // printf(" %d", u);
    if (I < size[u] * t) {
      found = true;
      dfs2(u, I % size[u]);
      break;
    } else
      I -= size[u] * t;
  }
  // puts("");

  if (!found)
    puts("0");
}

int main() {
  while (1) {
    scanf("%s%d", buf, &I);
    if (buf[0] == '0' && I == 0)
      return 0;

    n = strlen(buf);
    num_node = 1;
    assert(dfs1(0, 0) >= n);
    dfs2(0, I);
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define INF 0x334334334LL

typedef long long int LLI;
typedef pair<int, LLI> Pair;

int n;
int I;
int num_node;
int ls[11451419];
char buf[11451419];
bool is_leaf[11451419];
LLI size[11451419];
vector<Pair> es[11451419];

int dfs1(int k, int idx) {
  char *res;
  es[idx].clear();
  size[idx] = 0;
  while (k < n && buf[k] != ')') {
    int g = num_node++;
    LLI t = strtol(buf + k, &res, 10);
    if (buf + k == res) {
      t = 1;
      size[g] = 0;
      ls[g] = k;
      is_leaf[g] = true;
      while (k < n && 'A' <= buf[k] && buf[k] <= 'Z') {
        size[g]++;
        k++;
      }
    } else {
      is_leaf[g] = false;
      if (*res == '(') {
        k = dfs1(res - buf + 1, g);
      } else {
        k = dfs1(res - buf, g);
      }
    }
    es[idx].push_back(Pair(g, t));
    size[idx] = min(INF, size[idx] + size[g] * t);
  }
  return k + 1;
}

void dfs2(int v, int I) {
  if (is_leaf[v] && I < size[v]) {
    printf("%c\\n", buf[ls[v] + I]);
    return;
  }

  bool found = false;
  for (int i = 0; i < es[v].size(); i++) {
    Pair p = es[v][i];
    int u = p.first;
    LLI t = p.second;

    if (I < size[u] * t) {
      found = true;
      dfs2(u, I % size[u]);
      break;
    } else
      I -= size[u] * t;
  }

  if (!found)
    puts("0");
}

int main() {
  while (1) {
    scanf("%s%d", buf, &I);
    if (buf[0] == '0' && I == 0)
      return 0;

    n = strlen(buf);
    num_node = 1;
    assert(dfs1(0, 0) >= n);
    dfs2(0, I);
  }
}