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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, int> pip;
typedef vector<pip> vip;
const int inf = 1 << 29;
const ll INF = 1ll << 58;
const double pi = acos(-1);
const double eps = 1e-7;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

vi f(int n, int n0, int nd, int p) {
  vi a(n);
  a[0] = n0;
  for (int i = 1; i < n; i++)
    a[i] = (a[i - 1] * 58 + nd) % (p + 1);
  sort(a.begin(), a.end());
  return a;
}

int n, m, n0, nd, m0, md;
vl date, nx;

ll Query(int k, int x) {
  ll K = nx[k], res;
  if (k) {
    res = date[k] - x;
    date[k - 1] += res;
    date[k] += (K == n ? 0 : date[K]) - res;
  } else
    res = date[K];
  date[0] = 0;
  nx[k] = (K == n ? n : nx[K]);
  return res;
}

int main() {
  cin >> n >> m >> n0 >> nd >> m0 >> md;
  vi a = f(n, n0, nd, m), b = f(m, m0, md, n);
  date = nx = vl(n + 1);
  ll I = n + 1, tmp = 0, res = 0;
  for (int i = 0; i < m; i++)
    if (b[i])
      date[b[i]]++;
  for (int i = 0; i <= n; i++)
    nx[i] = i + 1;
  for (int i = 0; i < n; i++)
    if (a[i]) {
      while (I && tmp < a[i]) {
        I--;
        tmp += date[I];
      }
      if (I) {
        res += a[i];
        tmp -= Query(I, tmp - a[i]);
      } else {
        res += tmp;
        tmp -= Query(I, 0);
      }
    }
  cout << res << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, int> pip;
typedef vector<pip> vip;
const int inf = 1 << 29;
const ll INF = 1ll << 58;
const double pi = acos(-1);
const double eps = 1e-7;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

vi f(int n, int n0, int nd, int p) {
  vi a(n);
  a[0] = n0;
  for (int i = 1; i < n; i++)
    a[i] = (a[i - 1] * 58 + nd) % (p + 1);
  sort(a.begin(), a.end());
  return a;
}

int n, m, n0, nd, m0, md;
vl date, nx;

ll Query(int k, int x) {
  ll K = nx[k], res;
  if (k) {
    res = date[k] - x;
    date[k - 1] += res;
    date[k] += (K == n ? 0 : date[K]) - res;
  } else
    res = date[K];
  date[0] = 0;
  nx[k] = (K == n ? n : nx[K]);
  return res;
}

int main() {
  cin >> n >> m >> n0 >> nd >> m0 >> md;
  vi a = f(n, n0, nd, m), b = f(m, m0, md, n);
  date = nx = vl(n + 1);
  ll I = n + 1, tmp = 0, res = 0;
  for (int i = 0; i < m; i++)
    if (b[i])
      date[b[i]]++;
  for (int i = 0; i <= n; i++)
    nx[i] = i + 1;
  for (int i = 0; i < n; i++) {
    while (I && tmp < a[i]) {
      I--;
      tmp += date[I];
    }
    int tmp1;
    if (I) {
      res += a[i];
      tmp -= Query(I, tmp - a[i]);
    } else {
      res += tmp;
      tmp -= Query(I, 0);
    }
  }
  cout << res << endl;
}