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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

ll D, c[33], s[444][33], t[444];
set<ll> s1[33], s2[33];

vector<ll> score(ll t[]) {
  ll last[33] = {}, i, j, sc = 0;
  vector<ll> v;
  v.pb(0);
  for (i = 1; i <= D; i++) {
    sc += s[i][t[i]];
    last[t[i]] = i;
    for (j = 1; j <= 26; j++)
      sc -= c[j] * (i - last[j]);
    v.pb(sc);
  }
  return v;
}

ll cost(ll k, ll dis) { return k * dis * (dis - 1) / 2; }

int main() {
  ll i, j;
  cin >> D;
  for (i = 1; i <= 26; i++)
    cin >> c[i];
  for (i = 1; i <= D; i++)
    for (j = 1; j <= 26; j++)
      cin >> s[i][j];
  // for (i = 1; i <= D;i++){
  //     ll ma = -1e9, p = -1;
  //     for (j = 1; j <= 26; j++){
  //         if(c[j]*(i-last[j])+s[i][j]>ma){
  //             ma = c[j] * (i - last[j]) + s[i][j];
  //             p = j;
  //         }
  //     }
  //     t[i] = p;
  //     last[p] = i;
  // }
  for (i = 1; i <= 26; i++) {
    s1[i].insert(D + 1);
    s2[i].insert(0);
  }
  for (i = 1; i <= D; i++) {
    cin >> t[i];
    s1[t[i]].insert(i);
    s2[t[i]].insert(-i);
  }
  ll sc = score(t)[D];
  ll M;
  cin >> M;
  ll d, q;
  for (i = 0; i < M; i++) {
    cin >> d >> q;
    ll bq = t[d];
    ll b = *s1[bq].upper_bound(d);
    ll a = *s2[bq].upper_bound(-d);
    sc -= s[d][bq];
    sc += s[d][q];
    ll l = d + a, r = b - d;
    sc += cost(c[bq], l);
    sc += cost(c[bq], r);
    sc -= cost(c[bq], l + r);
    s1[bq].erase(d);
    s2[bq].erase(-d);
    b = *s1[q].upper_bound(d);
    a = *s2[q].upper_bound(-d);
    l = d + a, r = b - d;
    sc -= cost(c[q], l);
    sc -= cost(c[q], r);
    sc += cost(c[q], l + r);
    s1[q].insert(d);
    s2[q].insert(-d);
    t[d] = q;
    cout << sc << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

ll D, c[33], s[444][33], last[33] = {};
set<ll> s1[33], s2[33];

vector<ll> score(vector<ll> t) {
  ll last[33] = {}, i, j, sc = 0;
  vector<ll> v;
  v.pb(0);
  for (i = 1; i <= D; i++) {
    sc += s[i][t[i]];
    last[t[i]] = i;
    for (j = 1; j <= 26; j++)
      sc -= c[j] * (i - last[j]);
    v.pb(sc);
  }
  return v;
}

ll cost(ll k, ll dis) { return k * dis * (dis - 1) / 2; }

int main() {
  ll i, j;
  cin >> D;
  for (i = 1; i <= 26; i++)
    cin >> c[i];
  for (i = 1; i <= D; i++)
    for (j = 1; j <= 26; j++)
      cin >> s[i][j];
  // for (i = 1; i <= D;i++){
  //     ll ma = -1e9, p = -1;
  //     for (j = 1; j <= 26; j++){
  //         if(c[j]*(i-last[j])+s[i][j]>ma){
  //             ma = c[j] * (i - last[j]) + s[i][j];
  //             p = j;
  //         }
  //     }
  //     cout << p << endl;
  //     last[p] = i;
  // }
  vector<ll> v;
  v.pb(0);
  for (i = 1; i <= 26; i++) {
    s1[i].insert(D + 1);
    s2[i].insert(0);
  }
  ll t[33];
  for (i = 1; i <= D; i++) {
    cin >> t[i];
    v.pb(t[i]);
    s1[t[i]].insert(i);
    s2[t[i]].insert(-i);
  }
  ll sc = score(v)[D];
  ll m;
  cin >> m;
  ll d, q;
  for (i = 0; i < m; i++) {
    cin >> d >> q;
    ll bq = t[d];
    ll b = *s1[bq].upper_bound(d);
    ll a = *s2[bq].upper_bound(-d);
    sc -= s[d][bq];
    sc += s[d][q];
    ll l = d + a, r = b - d;
    sc += cost(c[bq], l);
    sc += cost(c[bq], r);
    sc -= cost(c[bq], l + r);
    s1[bq].erase(d);
    s2[bq].erase(-d);
    b = *s1[q].upper_bound(d);
    a = *s2[q].upper_bound(-d);
    l = d + a, r = b - d;
    sc -= cost(c[q], l);
    sc -= cost(c[q], r);
    sc += cost(c[q], l + r);
    s1[q].insert(d);
    s2[q].insert(-d);
    t[d] = q;
    cout << sc << endl;
  }
}