alexjercan/bugnet huggingface dataset:
Problem p01354 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define double long double
using namespace std;
typedef pair<int, int> P;
const int INF = 1e9;
const double EPS = 1e-9;

int m, n;
double p[17][17];
double memo[1LL << 16][17];

double rec(int mask, int id) {
  if (id == n) {
    // cout << mask << ' ' << id << ' ' << 1.0 << endl;
    return 1.0;
  }
  if (mask == 0) {
    // cout << mask << ' ' << id << ' ' << 0.0 << endl;
    return 0.0;
  }
  if (memo[mask][id] > -EPS)
    return memo[mask][id];
  double res = 0.0;
  rep(i, 0, m) {
    if (!(mask & (1LL << i)))
      continue;
    double tmp = 0.0, now = 1.0;
    rep(j, id, n) {
      // j番目に負けた場合
      tmp += now * (1 - p[i][j]) * rec(mask - (1LL << i), j);
      now *= p[i][j];
    }
    tmp += now;
    // tmp = p[i][id] *  rec(mask - (1LL << now), i, id);
    res = max(res, tmp);
  }
  // res = p[now][id] * rec(mask, now, id + 1) + (1 - p[now][id]) * res;
  // cout << mask << ' ' << id << ' ' << res << endl;
  return memo[mask][id] = res;
}

int main() {
  cin >> m >> n;
  rep(i, 0, m) {
    rep(j, 0, n) { cin >> p[i][j]; }
  }
  rep(i, 0, 1 << m) rep(k, 0, n + 1) memo[i][k] = -1.0;
  double ans = 0.0;
  ans = rec((1LL << m) - 1, 0);
  printf("%.20Lf\\n", ans);
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
using namespace std;
typedef pair<int, int> P;
const int INF = 1e9;
const double EPS = 1e-9;

int m, n;
double p[17][17];
double memo[1LL << 16][16][16];

double rec(int mask, int now, int id) {
  if (id == n) {
    // cout << mask << ' ' << now << ' ' << id << ' ' << 1.0 << endl;
    return 1.0;
  }
  if (mask == 0 || (mask & (1LL << now)) == 0) {
    // cout << mask << ' ' << now << ' ' << id << ' ' << 0.0 << endl;
    return 0.0;
  }
  if (memo[mask][now][id] > -EPS)
    return memo[mask][now][id];
  double res = 0.0;
  rep(i, 0, m) {
    if (!(mask & (1LL << i)))
      continue;
    if (i == now)
      continue;
    double tmp = rec(mask - (1LL << now), i, id);
    res = max(res, tmp);
  }
  res = p[now][id] * rec(mask, now, id + 1) + (1 - p[now][id]) * res;
  // cout << mask << ' ' << now << ' ' << id << ' ' << res << endl;
  return memo[mask][now][id] = res;
}

int main() {
  cin >> m >> n;
  rep(i, 0, m) {
    rep(j, 0, n) { cin >> p[i][j]; }
  }
  rep(i, 0, 1 << m) rep(j, 0, m) rep(k, 0, n + 1) memo[i][j][k] = -1.0;
  double ans = 0.0;
  rep(i, 0, m) { ans = max(ans, rec((1LL << m) - 1, i, 0)); }
  // rep(i, 0, 1 << m){
  //     rep(j, 0, m){
  //         rep(k, 0, n + 1){
  //             cout << i << ' ' << j << ' ' << k << ' ' << memo[i][j][k] <<
  //             endl;
  //         }
  //     }
  // }
  printf("%.10f\\n", ans);
}
Problem p01354 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
long double x[20][20], m, n, neko[20][20][20], dp[20][100000];
inline long double solve(int pos, int ret) {
  if (pos == n)
    return 1.0l;
  if (dp[pos][ret] >= -0.1l)
    return dp[pos][ret];
  long double maxn = 0.0l;
  for (int i = 0; i < m; i++) {
    if ((ret / (1 << i)) & 1 == 1)
      continue;
    long double cnt = 0.0;
    for (int j = pos; j <= n; j++) {
      long double w1 = solve(j, ret + (1 << i)) * neko[i][pos][j];
      cnt += w1;
    }
    maxn = max(maxn, cnt);
  }
  dp[pos][ret] = maxn;
  return maxn;
}
int main() {
  for (int i = 0; i < 20; i++) {
    for (int j = 0; j < 100000; j++)
      dp[i][j] = -1.0l;
  }
  cin >> m >> n;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++)
      cin >> x[i][j];
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      neko[i][j][j] = 1.0l;
      long double cnt = 1.0l;
      for (int k = j; k < n; k++) {
        cnt *= x[i][k];
        neko[i][j][k + 1] = cnt;
      }
      for (int k = j; k <= n; k++) {
        neko[i][j][k] = neko[i][j][k] - neko[i][j][k + 1];
      }
    }
  }
  printf("%.12Lf\\n", solve(0, 0));
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
long double x[20][20], m, n, neko[20][20][20], dp[20][100000];
long double solve(int pos, int ret) {
  if (pos == n)
    return 1.0l;
  if (dp[pos][ret] >= -0.1l)
    return dp[pos][ret];
  long double maxn = 0.0l;
  for (int i = 0; i < m; i++) {
    if ((ret / (1 << i)) & 1 == 1)
      continue;
    long double cnt = 0.0;
    for (int j = pos; j <= n; j++) {
      long double w1 = solve(j, ret + (1 << i)) * neko[i][pos][j];
      cnt += w1;
    }
    maxn = max(maxn, cnt);
  }
  dp[pos][ret] = maxn;
  return maxn;
}
int main() {
  for (int i = 0; i < 20; i++) {
    for (int j = 0; j < 100000; j++)
      dp[i][j] = -1.0l;
  }
  cin >> m >> n;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++)
      cin >> x[i][j];
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      neko[i][j][j] = 1.0l;
      long double cnt = 1.0l;
      for (int k = j; k < n; k++) {
        cnt *= x[i][k];
        neko[i][j][k + 1] = cnt;
      }
      for (int k = j; k <= n; k++) {
        neko[i][j][k] = neko[i][j][k] - neko[i][j][k + 1];
      }
    }
  }
  printf("%.12Lf\\n", solve(0, 0));
  return 0;
}
Problem p01356 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;

long long n, m, a, b, p, q, minx, d, s, B;
long long x[10000], y[10000], c[10000];
long long rem;

int main() {
  cin >> n >> m >> a >> b >> p >> q;
  x[0] = 1;
  y[0] = 1;
  s = min((long long)10000, n);

  if (a >= 1000000000 && b >= 1000000000) {
    if (p >= 1000000000 && q >= 1000000000) {
      cout << min(m, abs(m - p - q)) << endl;
    } else {
      cout << min(m, min(abs(m - p - q), min(abs(m - a * p - b * q),
                                             abs(m - a * p - b * q - p - q))))
           << endl;
    }
    return 0;
  }

  for (int i = 1; i < s; i++) {
    x[i] = x[i - 1] * a;
    if (x[i] > m) {
      break;
    }
  }
  for (int i = 1; i < s; i++) {
    y[i] = y[i - 1] * b;
    if (x[i] > m) {
      break;
    }
  }

  if (a == 1 && b == 1) {
    if (n >= 1000001 && (p + q) >= 1000001) {
      goto F;
    }
    if (m <= n * (p + q)) {
    F:;
      B = m % (p + q);
      if (B > (p + q) / 2) {
        B = (p + q) - B;
      }
      cout << B << endl;
    } else {
      cout << m - (long long)(n * (p + q)) << endl;
    }
    return 0;
  }

  for (int i = 0; i < s; i++) {
    c[i] = p * x[i] + q * y[i];
    if (c[i] > m) {
      d = i;
      minx = c[i] - m;
      goto E;
    }
  }
  minx = m;
  d = s;
E:;
  rem = m - c[d - 1];
  minx = min(minx, abs(rem));
  for (int i = d - 2; i >= 0; i--) {
    minx = min(minx, (long long)abs(rem - c[i]));
    if (rem >= c[i]) {
      rem -= c[i];
    }
  }
  cout << minx << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

long long n, m, a, b, p, q, minx, d, s;
long long x[10000], y[10000], c[10000];
long long rem;

int main() {
  cin >> n >> m >> a >> b >> p >> q;
  x[0] = 1;
  y[0] = 1;
  s = min((long long)10000, n);
  for (int i = 1; i < s; i++) {
    x[i] = x[i - 1] * a;
    if (x[i] > m) {
      break;
    }
  }
  for (int i = 1; i < s; i++) {
    y[i] = y[i - 1] * b;
    if (x[i] > m) {
      break;
    }
  }
  for (int i = 0; i < s; i++) {
    c[i] = p * x[i] + q * y[i];
    if (c[i] > m) {
      d = i;
      minx = c[i] - m;
      goto E;
    }
  }
  minx = m;
E:;
  d = n;
  rem = m - c[d - 1];
  minx = min(minx, abs(rem));
  for (int i = d - 2; i >= 0; i--) {
    minx = min(minx, (long long)abs(rem - c[i]));
    if (rem >= c[i]) {
      rem -= c[i];
    }
  }
  cout << minx << endl;
  return 0;
}
Problem p01356 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

long long n, m, a, b, p, q;

long long mul(long long a, long long b) {
  __int128 r = a;
  r *= b;
  if (r > 1e18)
    throw 0;
  return r;
}

int main() {
  cin >> n >> m >> a >> b >> p >> q;
  if (a == 1 && b == 1) {
    long long c = m / (p + q);
    cout << min(m - min(n, c) * (p + q), llabs(min(n, c + 1) * (p + q) - m))
         << endl;
  } else {
    long long t[55] = {};
    t[0] = p + q;
    long long ak = 1, bk = 1;
    try {
      for (int i = 1; i < min(n, 42ll); i++) {
        ak = mul(ak, a);
        bk = mul(bk, b);
        t[i] = mul(p, ak) + mul(q, bk);
      }
    } catch (...) {
    }
    vector<long long> s(1 << 21);
    for (int i = 0; i < 1 << 21; i++) {
      long long c = 0;
      for (int j = 0; j < 21; j++) {
        c += (i >> j & 1) * t[j + 21];
      }
      s[i] = c;
    }
    sort(begin(s), end(s));
    s.erase(unique(begin(s), end(s)), end(s));
    long long ans = 1e18;
    for (int i = 0; i < 1 << 21; i++) {
      long long c = 0;
      for (int j = 0; j < 21; j++) {
        c += (i >> j & 1) * t[j];
      }
      auto it = lower_bound(begin(s), end(s), m - c);
      if (it != s.end()) {
        ans = min(ans, llabs(m - *it - c));
      }
      if (it != s.begin()) {
        it--;
        ans = min(ans, llabs(m - *it - c));
      }
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <set>

using namespace std;

long long n, m, a, b, p, q;

long long mul(long long a, long long b) {
  __int128 r = a;
  r *= b;
  if (r > 1e18)
    throw 0;
  return r;
}

int main() {
  cin >> n >> m >> a >> b >> p >> q;
  if (a == 1 && b == 1) {
    long long c = m / (p + q);
    cout << min(m - min(n, c) * (p + q), llabs(min(n, c + 1) * (p + q) - m))
         << endl;
  } else {
    long long t[55] = {};
    t[0] = p + q;
    long long ak = 1, bk = 1;
    try {
      for (int i = 1; i < min(n, 42ll); i++) {
        ak = mul(ak, a);
        bk = mul(bk, b);
        t[i] = mul(p, ak) + mul(q, bk);
      }
    } catch (...) {
    }
    set<long long> s;
    for (int i = 0; i < 1 << 21; i++) {
      long long c = 0;
      for (int j = 0; j < 21; j++) {
        c += (i >> j & 1) * t[j + 21];
      }
      s.insert(c);
    }
    long long ans = 1e18;
    for (int i = 0; i < 1 << 21; i++) {
      long long c = 0;
      for (int j = 0; j < 21; j++) {
        c += (i >> j & 1) * t[j];
      }
      auto it = s.lower_bound(m - c);
      if (it != s.end()) {
        ans = min(ans, llabs(m - *it - c));
      }
      if (it != s.begin()) {
        it--;
        ans = min(ans, llabs(m - *it - c));
      }
    }
    cout << ans << endl;
  }
}
Problem p01356 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 45;
ll Sum[100], n, m, a, b, p, q;

ll calc(ll k) {
  ll A = 1, B = 1, res = p + q;
  while (res > 0 && res <= INF && k--)
    A *= a, B *= b, res = p * A + q * B;
  if (res < 0)
    res = INF;
  return min(INF, res);
}

ll ans = INF;
set<ll> used[101];
void dfs(ll pos, ll sum) {
  ans = min(ans, (ll)abs(m - sum));
  if (m < sum || pos == -1 || sum > INF || used[pos].count(sum))
    return;
  used[pos].insert(sum);

  if (Sum[pos] + sum < m) {
    ans = min(ans, m - (Sum[pos] + sum));
    return;
  }
  dfs(pos - 1, sum + calc(pos));
  dfs(pos - 1, sum);
}

int main() {
  cin >> n >> m >> a >> b >> p >> q;
  Sum[0] = calc(0);
  for (int i = 1; i < 60; i++)
    Sum[i] = min(INF, calc(i) + Sum[i - 1]);

  if (max(a, b) == 1) {
    ll s = abs(m - (m / (p + q)) * (p + q));
    ll t = abs(m - (m / (p + q)) * (p + q) - (p + q));
    if (m / (p + q) == n)
      t = INF;
    if (m / (p + q) <= n)
      cout << min(s, t) << endl;
    else
      cout << m - (p + q) * n << endl;
    return 0;
  }
  dfs(min(n - 1, 50LL), 0);
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 50;
ll n, m, a, b, p, q;

ll calc(ll k) {
  ll A = 1, B = 1;
  ll res = p + q;
  while (0 < res && res <= INF && k--)
    A *= a, B *= b, res = p * A + q * B;
  if (res < 0 || res > INF)
    return -1;
  return res;
}

ll ans = INF;
typedef pair<ll, ll> P;
set<P> used;
void dfs(ll pos, ll sum) {
  assert(sum >= 0);
  ans = min(ans, (ll)abs(m - sum));
  if (pos >= n || sum < 0LL || sum > INF)
    return;
  if (used.count(P(pos, sum)))
    return;
  used.insert(P(pos, sum));

  ll r = calc(pos);
  if (r <= 0)
    return;
  dfs(pos + 1, sum + r);
  dfs(pos + 1, sum);
}

int main() {
  cin >> n >> m >> a >> b >> p >> q;
  if (max(a, b) == 1) {
    ll s = abs(m - (m / (p + q)) * (p + q));
    ll t = abs(m - (m / (p + q)) * (p + q) - (p + q));
    if (m / (p + q) == n)
      t = INF;
    if (m / (p + q) <= n)
      cout << min(s, t) << endl;
    else
      cout << m - (p + q) * n << endl;
    return 0;
  }

  dfs(0, 0);
  cout << ans << endl;
  return 0;
}
Problem p01358 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int mat[2][510][510];
bool u[1001000];

vector<int> cards;

int n, p[2], m;
int f(int l, int id) {
  for (int i = 0; i < min<int>(cards.size(), l); i++)
    u[cards[i]] = true;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int ok = 1;
    for (int j = 0; j < n; j++)
      ok &= u[mat[id][i][j]];
    ans += ok;
    if (n != 1) {
      ok = 1;
      for (int j = 0; j < n; j++)
        ok &= u[mat[id][j][i]];
      ans += ok;
    }
  }
  if (n != 1) {
    int ok = 1;
    for (int j = 0; j < n; j++)
      ok &= u[mat[id][j][j]];
    ans += ok;
  }
  if (n != 1) {
    int ok = 1;
    for (int j = 0; j < n; j++)
      ok &= u[mat[id][n - j - 1][j]];
    ans += ok;
  }
  for (int i = 0; i < min<int>(cards.size(), l); i++)
    u[cards[i]] = false;
  return ans;
}

int bins(int id) {
  int l = 0, r = m + 1;
  while (l != r) {
    int mi = (l + r) / 2;
    if (f(mi, id) >= p[id])
      r = mi;
    else
      l = mi + 1;
  }

  return l;
}
int main() {
  cin >> n >> p[0] >> p[1] >> m;
  for (int j = 0; j < 2; j++) {
    for (int i = 0; i < n; i++) {
      for (int k = 0; k < n; k++) {
        scanf("%d", &mat[j][i][k]);
      }
    }
  }
  for (int i = 0; i < m; i++) {
    int t;
    cin >> t;
    cards.push_back(t);
  }
  int a = bins(0);
  int b = bins(1);
  if (a == b) {
    cout << "DRAW" << endl;
  } else if (a < b) {
    cout << "USAGI" << endl;
  } else {
    cout << "NEKO" << endl;
  }
}

Error: TLE

Fixed submission:

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

int mat[2][510][510];

vector<int> cards;

int n, p[2], m;
int f(int l, int id) {
  set<int> u;
  for (int i = 0; i < min<int>(cards.size(), l); i++)
    u.insert(cards[i]);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int ok = 1;
    for (int j = 0; j < n; j++)
      ok &= u.count(mat[id][i][j]);
    ans += ok;
    if (n != 1) {
      ok = 1;
      for (int j = 0; j < n; j++)
        ok &= u.count(mat[id][j][i]);
      ans += ok;
    }
  }
  if (n != 1) {
    int ok = 1;
    for (int j = 0; j < n; j++)
      ok &= u.count(mat[id][j][j]);
    ans += ok;
  }
  if (n != 1) {
    int ok = 1;
    for (int j = 0; j < n; j++)
      ok &= u.count(mat[id][n - j - 1][j]);
    ans += ok;
  }
  return ans;
}

int bins(int id) {
  int l = 0, r = m + 1;
  while (l != r) {
    int mi = (l + r) / 2;
    if (f(mi, id) >= p[id])
      r = mi;
    else
      l = mi + 1;
  }

  return l;
}
int main() {
  cin >> n >> p[0] >> p[1] >> m;
  for (int j = 0; j < 2; j++) {
    for (int i = 0; i < n; i++) {
      for (int k = 0; k < n; k++) {
        scanf("%d", &mat[j][i][k]);
      }
    }
  }
  for (int i = 0; i < m; i++) {
    int t;
    cin >> t;
    cards.push_back(t);
  }
  int a = bins(0);
  int b = bins(1);
  if (a == b) {
    cout << "DRAW" << endl;
  } else if (a < b) {
    cout << "USAGI" << endl;
  } else {
    cout << "NEKO" << endl;
  }
}
Problem p01358 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int N, U[2], M, A[2][500][500];
bool flag[2][500][500] = {{}};
map<int, vector<tuple<int, int, int>>> memo;
int main() {

  scanf("%d %d %d %d", &N, &U[0], &U[1], &M);

  for (int k = 0; k < 2; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        scanf("%d", &A[k][i][j]);
        memo[A[k][i][j]].emplace_back(k, i, j);
      }
    }
  }

  while (M--) {
    int num;
    scanf("%d", &num);
    for (auto &d : memo[num]) {
      int k, i, j;
      tie(k, i, j) = d;

      flag[k][i][j] = true;
      bool f1 = true, f2 = true;
      bool f3 = i == j;
      bool f4 = j == N - i - 1;
      for (int l = 0; l < N; l++) {
        f1 &= flag[k][i][l];
        f2 &= flag[k][l][j];
        f3 &= flag[k][l][l];
        f4 &= flag[k][l][N - l - 1];
      }
      U[k] -= f1;
      if (N > 1) {
        U[k] -= f2;
        U[k] -= f3;
        U[k] -= f4;
      }
    }
    if (U[0] <= 0 || U[1] <= 0) {
      if (U[0] <= 0 && U[1] <= 0)
        break;
      puts(U[1] <= 0 ? "NEKO" : "USAGI");
      exit(0);
    }
  }
  puts("DRAW");
}

Error: TLE

Fixed submission:

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

int N, U[2], M, A[2][500][500];
bool flag[2][500][500] = {{}};

int main() {

  scanf("%d %d %d %d", &N, &U[0], &U[1], &M);

  for (int k = 0; k < 2; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++)
        scanf("%d", &A[k][i][j]);
    }
  }

  while (M--) {
    int num;
    scanf("%d", &num);
    for (int k = 0; k < 2; k++) {
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
          if (A[k][i][j] == num) {
            flag[k][i][j] = true;
            bool f1 = true, f2 = true;
            bool f3 = i == j;
            bool f4 = j == N - i - 1;
            for (int l = 0; l < N; l++) {
              f1 &= flag[k][i][l];
              f2 &= flag[k][l][j];
              f3 &= flag[k][l][l];
              f4 &= flag[k][l][N - l - 1];
            }
            U[k] -= f1;
            if (N > 1) {
              U[k] -= f2;
              U[k] -= f3;
              U[k] -= f4;
            }
          }
        }
      }
    }
    if (U[0] <= 0 || U[1] <= 0) {
      if (U[0] <= 0 && U[1] <= 0)
        break;
      puts(U[1] <= 0 ? "NEKO" : "USAGI");
      exit(0);
    }
  }
  puts("DRAW");
}
Problem p01359 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
using ll = long long;
using P = pair<int, int>;
using namespace std;

template <class T> void vin(vector<T> &v, int n) {
  v.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
}

int main() {
  int N, Q;
  for (int N, Q; cin >> N >> Q and N and Q;) {
    vector<pair<int, pair<string, int>>> p;
    rep(i, N) {
      string e;
      int a, b;
      cin >> e >> a >> b;
      p.push_back(make_pair(b, make_pair(e, a)));
    }
    sort(p.begin(), p.end(),
         [](const pair<int, pair<string, int>> &a,
            const pair<int, pair<string, int>> &b) {
           return a.first < b.first;
         });
    rep(i, Q) {
      int d;
      cin >> d;

      auto itr =
          lower_bound(p.begin(), p.end(), make_pair(d, make_pair("a", 0)),
                      [](const pair<int, pair<string, int>> &a,
                         const pair<int, pair<string, int>> &b) {
                        return a.first < b.first;
                      });

      if (itr == p.end()) {
        cout << "Unknown" << endl;
      } else {
        auto tmp = *itr;
        if (tmp.first - tmp.second.second + 1 <= d and d <= tmp.first) {
          cout << tmp.second.first << ' ' << tmp.second.second - (tmp.first - d)
               << endl;
        } else {
          cout << "Unknown" << endl;
        }
      }
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
using ll = long long;
using P = pair<int, int>;
using namespace std;

template <class T> void vin(vector<T> &v, int n) {
  v.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
}

int main() {
  int N, Q;
  for (int N, Q; cin >> N >> Q and N and Q;) {
    vector<pair<int, pair<string, int>>> p;
    rep(i, N) {
      string e;
      int a, b;
      cin >> e >> a >> b;
      p.push_back(make_pair(b, make_pair(e, a)));
    }
    sort(p.begin(), p.end(),
         [](const pair<int, pair<string, int>> &a,
            const pair<int, pair<string, int>> &b) {
           return a.first < b.first;
         });
    rep(i, Q) {
      int d;
      cin >> d;
      auto itr =
          *lower_bound(p.begin(), p.end(), make_pair(d, make_pair("", 0)),
                       [](const pair<int, pair<string, int>> &a,
                          const pair<int, pair<string, int>> &b) {
                         return a.first < b.first;
                       });

      if (itr.first - itr.second.second + 1 <= d and d <= itr.first) {
        cout << itr.second.first << ' ' << itr.second.second - (itr.first - d)
             << endl;
      } else {
        cout << "Unknown" << endl;
      }
    }
  }
  return 0;
}
Problem p01359 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;

struct Era {
  string name;
  int start; // 0
  int end;
};

int main() {
  while (1) {
    int n, q;
    cin >> n >> q;
    if (n == 0)
      break;

    vector<Era> data(n);
    for (int i = 0; i < n; i++) {
      int eby;
      cin >> data[i].name >> eby >> data[i].end;
      data[i].start = data[i].end - eby;
    }

    for (int i = 0; i < q; i++) {
      bool found = false;
      int year;
      cin >> year;
      for (int j = 0; j < n; j++) {
        if (data[j].start < year && year <= data[j].end) {
          cout << data[j].name << " " << year - data[j].start << endl;
          found = true;
          break;
        }
      }
      if (!found) {
        cout << "Unknown" << endl;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

struct Era {
  string name;
  int start; // 0
  int end;
};

int main() {
  while (1) {
    int n, q;
    cin >> n >> q;
    if (n == 0)
      break;

    vector<Era> data(n);
    for (int i = 0; i < n; i++) {
      string name;
      int eby, wy;
      cin >> name >> eby >> wy;
      data[i].name = name;
      data[i].start = wy - eby;
      data[i].end = wy;
    }

    for (int i = 0; i < q; i++) {
      bool found = false;
      int year;
      cin >> year;
      for (int j = 0; j < n; j++) {
        if (data[i].start < year && year <= data[i].end) {
          cout << data[i].name << " " << year - data[i].start << endl;
          found = true;
          break;
        }
      }
      if (!found) {
        cout << "Unknown" << endl;
      }
    }
  }
  return 0;
}