Problem p02620 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
// s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
// vector<vector<ll>> dp(n,vector<ll>(n))
// exist x map o set
int D;
ll c[26];
ll s[365][26];
void input() {
  scanf("%d", &D);
  for (int i = 0; i < 26; i++)
    cin >> c[i];
  for (int i = 0; i < D; i++)
    for (int j = 0; j < 26; j++)
      cin >> s[i][j];
}
signed main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  input();
  vector<int> last(26, 0);
  vector<ll> ans(D);
  set<ll> day[26];
  rep(i, 26) day[i].insert(0);
  ll score = 0;
  rep(i, D) {
    cin >> ans[i];
    ans[i]--;
    last[ans[i]] = i + 1;
    score += s[i][ans[i]];
    rep(j, 26) { score -= c[j] * (i + 1 - last[j]); }
    day[ans[i]].insert(i + 1);
  }
  rep(i, 26) day[i].insert(D + 1);

  int m;
  cin >> m;
  while (m--) {
    ll d, q;
    cin >> d >> q;
    q--;
    ll p = ans[d - 1];
    ans[d - 1] = q;
    score += (s[d - 1][q] - s[d - 1][p]);

    ll prev = 0, nxt = D + 1;
    for (ll u : day[p]) {
      if (u < d)
        chmax(prev, u);
      else if (u > d)
        chmin(nxt, u);
    }
    score -= c[p] * (d - prev) * (nxt - d);
    day[p].erase(d);

    prev = 0, nxt = D + 1;
    for (ll u : day[q]) {
      if (u < d)
        chmax(prev, u);
      else if (u > d)
        chmin(nxt, u);
    }
    score += c[q] * (d - prev) * (nxt - d);
    day[q].insert(d);

    cout << score << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
// s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
// vector<vector<ll>> dp(n,vector<ll>(n))
// exist x map o set
int D;
ll c[26];
ll s[365][26];
void input() {
  cin >> D;
  for (int i = 0; i < 26; i++)
    cin >> c[i];
  for (int i = 0; i < D; i++)
    for (int j = 0; j < 26; j++)
      cin >> s[i][j];
}
signed main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  input();
  vector<int> last(26, 0);
  vector<ll> ans(D);
  set<ll> day[26];
  rep(i, 26) day[i].insert(0);
  ll score = 0;
  rep(i, D) {
    cin >> ans[i];
    ans[i]--;
    last[ans[i]] = i + 1;
    score += s[i][ans[i]];
    rep(j, 26) { score -= c[j] * (i + 1 - last[j]); }
    day[ans[i]].insert(i + 1);
  }
  rep(i, 26) day[i].insert(D + 1);

  int m;
  cin >> m;
  while (m--) {
    ll d, q;
    cin >> d >> q;
    q--;
    ll p = ans[d - 1];
    ans[d - 1] = q;
    score += (s[d - 1][q] - s[d - 1][p]);

    ll prev = 0, nxt = D + 1;
    for (ll u : day[p]) {
      if (u < d)
        chmax(prev, u);
      else if (u > d)
        chmin(nxt, u);
    }
    score -= c[p] * (d - prev) * (nxt - d);
    day[p].erase(d);

    prev = 0, nxt = D + 1;
    for (ll u : day[q]) {
      if (u < d)
        chmax(prev, u);
      else if (u > d)
        chmin(nxt, u);
    }
    score += c[q] * (d - prev) * (nxt - d);
    day[q].insert(d);

    cout << score << endl;
  }
}