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

#include <bits/stdc++.h>
using namespace std;
#define forr(i, m, n) for (int i = (m); i <= (n); i++)
#define forv(i, m, n) for (int i = (m); i >= (n); i--)
#define forall(it, p) for (auto it = p.begin(); it != p.end(); ++it)
#define DBG 1
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;

vector<int> c;
vector<vector<int>> s;

struct State {
  vector<int> t;
  int d, score;

  State() {}

  void set(int _d, vector<int> &_t) {
    d = _d;
    // t = _t;
    t.resize(d);
    forr(i, 0, d - 1) t[i] = _t[i];
    compscore0();
  }

  void compscore0() {
    vector<int> last(26, -1);
    int sc = 0;
    forr(i, 0, d - 1) {
      int tt = t[i];
      last[tt] = i;
      forr(j, 0, 25) sc -= c[j] * (i - last[j]);
      sc += s[i][tt];
    }
    score = sc;
  }

  void add(int day, int con) {
    int bef = day - 1;
    while (bef >= 0 && t[bef] != con)
      bef--;
    int aft = day + 1;
    while (aft < d && t[aft] != con)
      aft++;
    int inc = s[day][con] + c[con] * (day - bef) * (aft - day);
    // cerr << "add inc " << inc << " s " << s[day][con] << " c " << c[con] << "
    // dy-bf " << day-bef << " af-dy " << aft-day << " sc " << score << endl;
    score += inc;
    t[day] = con;
  }

  void sub(int day) {
    int con = t[day];
    int bef = day - 1;
    while (bef >= 0 && t[bef] != con)
      bef--;
    int aft = day + 1;
    while (aft < d && t[aft] != con)
      aft++;
    int inc = s[day][con] + c[con] * (day - bef) * (aft - day);
    // cerr << "sub inc " << inc << " s " << s[day][con] << " c " << c[con] << "
    // dy-bf " << day-bef << " af-dy " << aft-day << " sc " << score << endl;
    score -= inc;
    t[day] = -1;
  }

  void chg(int day, int con) {
    // cerr << "chg day " << day << " con " << con << endl;
    sub(day);
    // cerr << "sub done" << endl;
    add(day, con);
    // cerr << "add done score " << score << endl;
  }
};

int main() {
  int d;
  cin >> d;
  // c = vector<int>(26);
  c.resize(26);
  // s = vector< vector<int> >(d, vector<int>(26));
  s.resize(d);
  forr(i, 0, d - 1) s[i].resize(26);

  forr(i, 0, 25) cin >> c[i];
  forr(i, 0, d - 1) forr(j, 0, 25) cin >> s[i][j];
  vector<int> t(d);
  forr(i, 0, d - 1) {
    cin >> t[i];
    t[i]--;
  }

  // cerr << "before st" << endl;
  State st;
  st.set(d, t);
  // cerr << "after st" << endl;

  int m;
  cin >> m;
  forr(i, 0, m - 1) {
    int dd, qq;
    cin >> dd >> qq;
    st.chg(dd - 1, qq - 1);
    cout << st.score << endl;
  }
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define forr(i, m, n) for (int i = (m); i <= (n); i++)
#define forv(i, m, n) for (int i = (m); i >= (n); i--)
#define forall(it, p) for (auto it = p.begin(); it != p.end(); ++it)
#define DBG 1
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;

vector<int> c;
vector<vector<int>> s;

struct State {
  vector<int> t;
  int d, score;

  State() { t.resize(d); }

  void set(int _d, vector<int> &_t) {
    d = _d;
    t = _t;
    compscore0();
  }

  void compscore0() {
    vector<int> last(26, -1);
    int sc = 0;
    forr(i, 0, d - 1) {
      int tt = t[i];
      last[tt] = i;
      forr(j, 0, 25) sc -= c[j] * (i - last[j]);
      sc += s[i][tt];
    }
    score = sc;
  }

  void add(int day, int con) {
    int bef = day - 1;
    while (bef >= 0 && t[bef] != con)
      bef--;
    int aft = day + 1;
    while (aft < d && t[aft] != con)
      aft++;
    int inc = s[day][con] + c[con] * (day - bef) * (aft - day);
    // cerr << "add inc " << inc << " s " << s[day][con] << " c " << c[con] << "
    // dy-bf " << day-bef << " af-dy " << aft-day << " sc " << score << endl;
    score += inc;
    t[day] = con;
  }

  void sub(int day) {
    int con = t[day];
    int bef = day - 1;
    while (bef >= 0 && t[bef] != con)
      bef--;
    int aft = day + 1;
    while (aft < d && t[aft] != con)
      aft++;
    int inc = s[day][con] + c[con] * (day - bef) * (aft - day);
    // cerr << "sub inc " << inc << " s " << s[day][con] << " c " << c[con] << "
    // dy-bf " << day-bef << " af-dy " << aft-day << " sc " << score << endl;
    score -= inc;
    t[day] = -1;
  }

  void chg(int day, int con) {
    // cerr << "chg day " << day << " con " << con << endl;
    sub(day);
    // cerr << "sub done" << endl;
    add(day, con);
    // cerr << "add done score " << score << endl;
  }
};

int main() {
  int d;
  cin >> d;
  c = vector<int>(26);
  s = vector<vector<int>>(d, vector<int>(26));

  forr(i, 0, 25) cin >> c[i];
  forr(i, 0, d - 1) forr(j, 0, 25) cin >> s[i][j];
  vector<int> t(d);
  forr(i, 0, d - 1) {
    cin >> t[i];
    t[i]--;
  }

  // cerr << "before st" << endl;
  State st;
  st.set(d, t);
  // cerr << "after st" << endl;

  int m;
  cin >> m;
  forr(i, 0, m - 1) {
    int dd, qq;
    cin >> dd >> qq;
    st.chg(dd - 1, qq - 1);
    cout << st.score << endl;
  }
}