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

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

int main() {
  int D;
  cin >> D;
  vector<int> c(27, 0);
  rep(j, 26) cin >> c[j + 1];
  int s[D + 1][27];
  rep(i, D) rep(j, 26) cin >> s[i + 1][j + 1];
  vector<int> t(D + 1, 0);
  rep(i, D) cin >> t[i + 1];

  int M;
  cin >> M;
  vector<int> d(M + 1, 0);
  vector<int> q(M + 1, 0);
  rep(i, M) cin >> d[i + 1] >> q[i + 1];

  ll ans = 0;
  vector<int> lastday(27, 0);

  for (int day = 1; day <= D; day++) {
    ans += s[day][t[day]];
    lastday[t[day]] = day;
    for (int j = 1; j <= 26; j++) {
      ans -= c[j] * (day - lastday[j]);
    }
  }

  for (int que = 1; que <= M; que++) {
    ans -= s[d[que]][t[d[que]]];
    ans += s[d[que]][q[que]];

    int before = 0;
    for (int pos = d[que] - 1; pos >= 1; pos--) {
      if (t[pos] == t[d[que]]) {
        before = pos;
        break;
      }
    }
    int after = D + 1;
    for (int pos = d[que] + 1; pos <= D; pos++) {
      if (t[pos] == t[d[que]]) {
        after = pos;
        break;
      }
    }
    ans += c[t[d[que]]] * (after - d[que]) * (before - d[que]);

    before = 0;
    for (int pos = d[que] - 1; pos >= 1; pos--) {
      if (t[pos] == q[que]) {
        before = pos;
        break;
      }
    }
    after = D + 1;
    for (int pos = d[que] + 1; pos <= D; pos++) {
      if (t[pos] == q[que]) {
        after = pos;
        break;
      }
    }
    ans -= c[q[que]] * (after - d[que]) * (before - d[que]);

    t[d[que]] = q[que];
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int D;
  cin >> D;
  vector<int> c(27, 0);
  rep(j, 26) cin >> c[j + 1];
  int s[D + 1][27];
  rep(i, D) rep(j, 26) cin >> s[i + 1][j + 1];
  vector<int> t(27, 0);
  rep(i, D) cin >> t[i + 1];

  int M;
  cin >> M;
  vector<int> d(M + 1, 0);
  vector<int> q(M + 1, 0);
  rep(i, M) cin >> d[i + 1] >> q[i + 1];

  ll ans = 0;
  vector<int> lastday(27, 0);

  for (int day = 1; day <= D; day++) {
    ans += s[day][t[day]];
    lastday[t[day]] = day;
    for (int j = 1; j <= 26; j++) {
      ans -= c[j] * (day - lastday[j]);
    }
    // cout << ans << endl;
  }
  // cout << "start : " << ans << endl;

  for (int que = 1; que <= M; que++) {
    ans -= s[d[que]][t[d[que]]];
    // cout << d[que] << " " << t[d[que]] << " " << s[d[que]][t[d[que]]] <<
    // endl; cout << "s-old : " << ans << endl;
    ans += s[d[que]][q[que]];
    // cout << "s+new : " << ans << endl;

    int before = 0;
    for (int pos = d[que] - 1; pos >= 1; pos--) {
      if (t[pos] == t[d[que]]) {
        before = pos;
        break;
      }
    }
    int after = D + 1;
    for (int pos = d[que] + 1; pos <= D; pos++) {
      if (t[pos] == t[d[que]]) {
        after = pos;
        break;
      }
    }
    ans += c[t[d[que]]] * (after - d[que]) * (before - d[que]);

    before = 0;
    for (int pos = d[que] - 1; pos >= 1; pos--) {
      if (t[pos] == q[que]) {
        before = pos;
        break;
      }
    }
    after = D + 1;
    for (int pos = d[que] + 1; pos <= D; pos++) {
      if (t[pos] == q[que]) {
        after = pos;
        break;
      }
    }
    ans -= c[q[que]] * (after - d[que]) * (before - d[que]);

    t[d[que]] = q[que];
    cout << ans << endl;
  }

  return 0;
}