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

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author cherrypi59
 */

#pragma GCC optimize("O2")
#pragma GCC optimize("tree-vectorize")
#pragma GCC target("sse4")

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FFOR(i, a, b) FOR(i, a, b + 1)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FOR(i, 1, n + 1)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define LEN(x) (int)(x).size()
#define DUMP(x) cerr << __LINE__ << ' ' << #x << "=" << (x) << endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair

using namespace std;
using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vector<T>>;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr lint ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; }

class CIncrementalScoring {
public:
  int D, score = 0;
  vc<int> C, T, last;
  vvc<int> S;
  vector<set<int>> G;

  void init() {
    REP(i, D) {
      last[T[i]] = i;
      score += S[T[i]][i];
      G[T[i]].insert(i);
      REP(j, 26) score -= C[j] * (i - last[j]);
    }
  }

  void update(int d, int x) {
    int old = T[d];
    score += S[x][d] - S[old][d];
    {
      auto oldItr = G[old].find(d);
      int pre = *prev(oldItr);
      int nxt = *next(oldItr);
      score -= C[old] * (nxt - d) * (d - pre);
      G[old].erase(oldItr);
      G[x].insert(d);
      auto xItr = G[x].find(d);
      pre = *prev(xItr);
      nxt = *next(xItr);
      score += C[x] * (nxt - d) * (d - pre);
    }
    T[d] = x;
  }

  void solve(std::istream &in, std::ostream &out) {
    ios_base::sync_with_stdio(false);
    in.tie(nullptr), out.tie(nullptr);
#ifdef DEBUG
    int64_t seed = chrono::duration_cast<chrono::milliseconds>(
                       chrono::system_clock::now().time_since_epoch())
                       .count();
    mt19937_64 rnd(seed);
    D = 365;
#else
    in >> D;
#endif
    C.resize(26);
    T.resize(D);
    last.resize(26, -1);
    S.resize(26, vc<int>(D));
    G.resize(26, {-1, D});
    for (auto &c : C) {
#ifdef DEBUG
      c = rnd() % 100 + 1;
#else
      in >> c;
#endif
    }
    REP(i, D) REP(j, 26) {
#ifdef DEBUG
      S[j][i] = rnd() % 20001;
#else
      in >> S[j][i];
#endif
    }
    REP(i, D) {
#ifdef DEBUG
      T[i] = rnd() % 26 + 1;
#else
      in >> T[i];
#endif
      T[i]--;
    }
    init();
    int M;
#ifdef DEBUG
    M = 100;
#else
    in >> M;
#endif
    while (M--) {
      int d, q;
#ifdef DEBUG
      d = rnd() % D + 1;
      q = rnd() % 26 + 1;
#else
      in >> d >> q;
#endif
      d--, q--;
      update(d, q);
      out << score << '\\n';
    }
  }
};

int main() {
  CIncrementalScoring solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Error: 0

Fixed submission:

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author cherrypi59
 */

#pragma GCC optimize("O2")
#pragma GCC optimize("tree-vectorize")
#pragma GCC target("sse4")

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FFOR(i, a, b) FOR(i, a, b + 1)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FOR(i, 1, n + 1)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define LEN(x) (int)(x).size()
#define DUMP(x) cerr << __LINE__ << ' ' << #x << "=" << (x) << endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair

using namespace std;
using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vector<T>>;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr lint ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; }

class CIncrementalScoring {
public:
  int D, score = 0;
  vc<int> C, T, last;
  vvc<int> S;
  vector<set<int>> G;

  void init() {
    REP(i, D) {
      last[T[i]] = i;
      score += S[T[i]][i];
      G[T[i]].insert(i);
      REP(j, 26) score -= C[j] * (i - last[j]);
#ifdef DEBUG
      DUMP(score);
#endif
    }
  }

  void update(int d, int x) {
    int old = T[d];
    score += S[x][d] - S[old][d];
    {
      auto oldItr = G[old].find(d);
      int pre = *prev(oldItr);
      int nxt = *next(oldItr);
      score -= C[old] * (nxt - d) * (d - pre);
      G[old].erase(oldItr);
      G[x].insert(d);
      auto xItr = G[x].find(d);
      pre = *prev(xItr);
      nxt = *next(xItr);
      score += C[x] * (nxt - d) * (d - pre);
    }
    T[d] = x;
  }

  void solve(std::istream &in, std::ostream &out) {
    ios_base::sync_with_stdio(false);
    in.tie(nullptr), out.tie(nullptr);
    in >> D;
    C.resize(26);
    T.resize(26);
    last.resize(26, -1);
    S.resize(26, vc<int>(D));
    G.resize(26, {-1, D});
    for (auto &c : C)
      in >> c;
    REP(i, D) REP(j, 26) in >> S[j][i];
    REP(i, D) in >> T[i], T[i]--;
    init();
    int M;
    in >> M;
    while (M--) {
      int d, q;
      in >> d >> q;
      d--, q--;
      update(d, q);
      out << score << '\\n';
    }
  }
};

int main() {
  CIncrementalScoring solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}