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

#include <bits/stdc++.h>
#define N 100005
using namespace std;
typedef unsigned long long ull;

const ull M = (1e9 + 7);
string s;
int q;
ull H[N], D[N];

void cal_hash() {
  ull h = 0;
  for (int i = 0; i < s.size(); i++) {
    h = h * M + s[i];
    H[i + 1] = h;
  }
  h = 1;
  for (int i = 0; i < N; i++) {
    D[i] = h;
    h *= M;
  }
  s = 'X' + s;
}

int get_first_diff_idx(int l, int r, int t) {

  int L = 0, R = r - l - t + 2;

  while (L < R) {

    int Mid = (L + R) / 2;
    ull v1 = H[l + Mid] - H[l - 1] * D[Mid + 1];
    ull v2 = H[l + Mid + t] - H[l + t - 1] * D[Mid + 1];

    if (v1 == v2)
      L = Mid + 1;
    else
      R = Mid;
  }

  return L;
}

bool isSame(int l, int r, int t, int A, int B) {

  int i1 = l + t, i2 = r;
  ull h1 = 0, h2 = 0;

  if (i1 <= A && A <= i2) {

    if (i1 < A && A < i2)
      h1 = H[i2] - H[A] * D[i2 - A] +
           ((H[A - 1] - H[i1 - 1] * D[A - i1]) * D[1] + s[B]) * D[i2 - A];
    else if (A == i1)
      h1 = H[i2] - H[i1] * D[i2 - i1] + s[B] * D[i2 - i1];
    else if (A == i2)
      h1 = s[B] + (H[i2 - 1] - H[i1 - 1] * D[i2 - i1]) * D[1];

  } else
    h1 = H[i2] - H[i1 - 1] * D[i2 - i1 + 1];

  i1 = l, i2 = r - t;

  if (i1 <= A && A <= i2) {

    if (i1 < A && A < i2)
      h2 = H[i2] - H[A] * D[i2 - A] +
           ((H[A - 1] - H[i1 - 1] * D[A - i1]) * D[1] + s[B]) * D[i2 - A];
    else if (A == i1)
      h2 = H[i2] - H[i1] * D[i2 - i1] + s[B] * D[i2 - i1];
    else if (A == i2)
      h2 = s[B] + (H[i2 - 1] - H[i1 - 1] * D[i2 - i1]) * D[1];

  } else
    h2 = H[i2] - H[i1 - 1] * D[i2 - i1 + 1];

  return h1 == h2;
}

bool check(int l, int r, int t) {

  int idx = get_first_diff_idx(l, r, t);

  int i1 = l + idx, i2 = l + t + idx;

  return isSame(l, r, t, i1, i2) | isSame(l, r, t, i2, i1);
}

int main() {

  cin >> s;

  cal_hash();

  cin >> q;

  int l, r, t;

  for (int i = 0; i < q; i++) {
    scanf("%d %d %d", &l, &r, &t);
    if (check(l, r, t))
      printf("Yes\\n");
    else
      printf("No\\n");
  }

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define INF 1e9
#define MAX_V 505
using namespace std;
/*?????§?????¢?????´?????????(Ford_Fulerson????????¨)???O(F|E|)*/

//????????¨????§???????(???????????????????????????)
struct edge {
  int to;
  vector<int> cap;
  int rev;
};

vector<edge> G[MAX_V]; //??°???????????£??\\?????????
bool used[MAX_V];      // DFS??§?????§??????????????????????????°

// from??????to??????????????????cap???????????°?????????????????????
void add_edge(int from, int to, vector<int> &cap) {
  G[from].push_back((edge){to, cap, (int)G[to].size()});
  G[to].push_back(
      (edge){from, vector<int>((int)cap.size(), 0), (int)G[from].size() - 1});
}

bool check(vector<int> &A) {
  for (int i = 0; i < A.size(); i++)
    if (A[i] != 0)
      return A[i] > 0;
  return false;
}

bool check2(vector<int> &A) {
  for (int i = 0; i < A.size(); i++)
    if (A[i] == INF)
      return false;
  return true;
}

void Minus(vector<int> &res, vector<int> &v) {
  for (int i = 0; i < res.size(); i++)
    res[i] -= v[i];
}

void Plus(vector<int> &res, vector<int> &v) {
  for (int i = 0; i < res.size(); i++)
    res[i] += v[i];
}

vector<int> Min(vector<int> &A, vector<int> &B) {
  for (int i = 0; i < A.size(); i++) {
    if (A[i] < B[i])
      return A;
    if (A[i] > B[i])
      return B;
  }
  return A;
}

//?¢?????????????DFS??§??¢???
vector<int> dfs(int v, int t, vector<int> f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && check(e.cap)) {
      vector<int> d = dfs(e.to, t, Min(f, e.cap));
      if (check(d) && check2(d)) {
        Minus(e.cap, d);
        Plus(G[e.to][e.rev].cap, d);
        return d;
      }
    }
  }
  return vector<int>((int)f.size(), 0);
}

bool isall0(vector<int> &v) {
  for (int i = 0; i < v.size(); i++)
    if (v[i] != 0)
      return false;
  return true;
}

bool isallnotINF(vector<int> &v) {
  for (int i = 0; i < v.size(); i++)
    if (v[i] >= INF)
      return false;
  return true;
}

int V = 55;

// s??????t???????????§???????±???????
vector<int> max_flow(int s, int t) {
  vector<int> flow = vector<int>(V, 0);
  for (;;) {
    memset(used, 0, sizeof(used));
    vector<int> f = dfs(s, t, vector<int>(V, INF));
    if (isall0(f))
      return flow;
    if (isallnotINF(f))
      Plus(flow, f);
  }
}

vector<int> tov(string s) {

  vector<int> res = vector<int>(V, 0);

  int num1, num2, p = 0;

  if ('0' <= s[p] && s[p] <= '9') {
    num1 = 0;
    while ('0' <= s[p] && s[p] <= '9')
      num1 = num1 * 10 + s[p++] - '0';
  } else
    num1 = 1;

  if (p == s.size()) {
    num2 = 0;
  } else {
    if (p + 1 == s.size())
      num2 = 1;
    else {
      p += 2;
      num2 = 0;
      while ('0' <= s[p] && s[p] <= '9')
        num2 = num2 * 10 + s[p++] - '0';
    }
  }

  res[num2] = num1;

  return res;
}

vector<int> tocap(string s) {

  vector<int> res = vector<int>(V, 0);
  s += '+';

  string t;

  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '+') {
      vector<int> r = tov(t);
      Plus(res, r);
      t = "";
    } else
      t += s[i];
  }

  reverse(res.begin(), res.end());

  return res;
}

string itos(int num) {

  string res;

  while (num) {
    res = (char)(num % 10 + '0') + res;
    num /= 10;
  }

  return res;
}

void output(vector<int> ans) {

  reverse(ans.begin(), ans.end());

  for (int i = 0; i < ans.size(); i++)
    if (ans[i] < 0)
      ans[i] = 0;

  string s;
  int flag = 0;

  for (int i = ans.size() - 1; i >= 0; i--) {
    if (ans[i] == 0)
      continue;
    if (flag)
      s += "+";
    flag = 1;
    if (i == 0 || (i != 0 && ans[i] != 1))
      s += itos(ans[i]);
    if (i == 1)
      s += "x";
    else if (i)
      s += "x^", s += itos(i);
  }
  if (s == "")
    s = "0";
  cout << s << endl;
}

signed main() {

  int n, m;

  while (1) {

    cin >> n >> m;
    if (!n && !m)
      break;

    int a, b;
    string s;

    for (int i = 0; i < m; i++) {
      cin >> a >> b >> s;
      vector<int> cap = tocap(s);
      add_edge(a - 1, b - 1, cap);
      add_edge(b - 1, a - 1, cap);
    }

    output(max_flow(0, n - 1));

    for (int i = 0; i < MAX_V; i++)
      G[i].clear();
  }

  return 0;
}