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

// #define _DEBUG
#include "bits/stdc++.h"
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define debug_1(x1) cout << #x1 << ": " << x1 << endl
#define debug_2(x1, x2)                                                        \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define debug_3(x1, x2, x3)                                                    \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << endl
#define debug_4(x1, x2, x3, x4)                                                \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << endl
#define debug_5(x1, x2, x3, x4, x5)                                            \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#ifdef _DEBUG
#define debug(...)                                                             \\
  CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~))        \\
  (__VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(index, num) for (int index = 0; index < (int)num; index++)
#define rep1(index, num) for (int index = 1; index <= (int)num; index++)
#define brep(index, num) for (int index = (int)num - 1; index >= 0; index--)
#define brep1(index, num) for (int index = (int)num; index > 0; index--)
#define scan(argument) cin >> argument
#define prin(argument) cout << argument << endl
#define kaigyo cout << endl
#define eps 1e-7
#define mp(a1, a2) make_pair(a1, a2)
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl = (ll)1e+18 + 1;
int INF = 1e+9 + 1;
ll D, c[27], s[366][27];
ll t[366];
set<ll> tst[26];
int main() {
  srand(time(NULL));
  std::chrono::system_clock::time_point start =
      std::chrono::system_clock::now();
  int nowtime = 0;
  int TIMEMAX = 1950;
  nowtime = std::chrono::duration_cast<std::chrono::milliseconds>(
                std::chrono::system_clock::now() - start)
                .count();

  scan(D);
  rep(i, 26) scan(c[i]);
  rep(i, D) {
    rep(j, 26) { scan(s[i][j]); }
  }
  ll pt = 0;
  rep(i, 26) {
    tst[i].insert(-1);
    tst[i].insert(D);
  }
  rep(i, D) {
    scan(t[i]);
    t[i]--;
    tst[t[i]].insert(i);
    pt += s[i][t[i]];
  }
  rep(i, 26) {
    for (auto itr = tst[i].begin(); itr != tst[i].end(); itr++) {
      if (itr == tst[i].begin())
        continue;
      auto itr2 = itr;
      itr2--;
      pt -= ((*itr) - (*itr2)) * ((*itr) - (*itr2) - 1) * c[i] / 2;
    }
  }
  int M;
  scan(M);
  rep(k, M) {
    ll d, q;
    scan(d >> q);
    d--;
    q--;
    ll befq = t[d];
    t[d] = q;
    pt += s[d][q] - s[d][befq];
    auto itr = tst[befq].lower_bound(d);
    auto itrb = itr;
    itrb--;
    auto itrn = itr;
    itrn++;
    pt -= ((*itrn) - (*itr)) * ((*itr) - (*itrb)) * c[befq];
    tst[befq].erase(itr);
    tst[q].insert(d);
    auto nitr = tst[q].lower_bound(d);
    auto nitrb = nitr;
    nitrb--;
    auto nitrn = nitr;
    nitrn++;
    pt += ((*nitrn) - (*nitr)) * ((*nitr) - (*nitrb)) * c[q];
    debug(befq, *itrb, *itr, *itrn,
          ((*itrn) - (*itr)) * ((*itr) - (*itrb)) * c[befq]);
    debug(q, *nitrb, *nitr, *nitrn,
          ((*itrn) - (*itr)) * ((*itr) - (*itrb)) * c[q]);

    prin(pt);
  }
  return 0;
}


Error: -11

Fixed submission:

// #define _DEBUG
#include "bits/stdc++.h"
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define debug_1(x1) cout << #x1 << ": " << x1 << endl
#define debug_2(x1, x2)                                                        \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define debug_3(x1, x2, x3)                                                    \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << endl
#define debug_4(x1, x2, x3, x4)                                                \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << endl
#define debug_5(x1, x2, x3, x4, x5)                                            \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#ifdef _DEBUG
#define debug(...)                                                             \\
  CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~))        \\
  (__VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(index, num) for (int index = 0; index < (int)num; index++)
#define rep1(index, num) for (int index = 1; index <= (int)num; index++)
#define brep(index, num) for (int index = (int)num - 1; index >= 0; index--)
#define brep1(index, num) for (int index = (int)num; index > 0; index--)
#define scan(argument) cin >> argument
#define prin(argument) cout << argument << endl
#define kaigyo cout << endl
#define eps 1e-7
#define mp(a1, a2) make_pair(a1, a2)
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl = (ll)1e+18 + 1;
int INF = 1e+9 + 1;
ll D, c[27], s[366][27];
ll t[366];
set<ll> tst[26];
int main() {
  srand(time(NULL));
  std::chrono::system_clock::time_point start =
      std::chrono::system_clock::now();
  int nowtime = 0;
  int TIMEMAX = 1950;
  nowtime = std::chrono::duration_cast<std::chrono::milliseconds>(
                std::chrono::system_clock::now() - start)
                .count();

  scan(D);
  /*	rep(i,26) scan(c[i]);
          rep(i,D){
                  rep(j,26){
                          scan(s[i][j]);
                  }
          }*/
  rep(i, 26) c[i] = 1;
  rep(i, D) rep(j, 26) s[i][j] = 0;
  ll pt = 0;
  rep(i, 26) {
    tst[i].insert(-1);
    tst[i].insert(D);
  }
  rep(i, D) {
    scan(t[i]);
    t[i]--;
    tst[t[i]].insert(i);
    pt += s[i][t[i]];
  }
  rep(i, 26) {
    for (auto itr = tst[i].begin(); itr != tst[i].end(); itr++) {
      if (itr == tst[i].begin())
        continue;
      auto itr2 = itr;
      itr2--;
      pt -= ((*itr) - (*itr2)) * ((*itr) - (*itr2) - 1) * c[i] / 2;
    }
  }
  int M;
  scan(M);
  rep(k, M) {
    ll d, q;
    scan(d >> q);
    d--;
    q--;
    ll befq = t[d];
    t[d] = q;
    pt += s[d][q] - s[d][befq];
    auto itr = tst[befq].lower_bound(d);
    auto itrb = itr;
    itrb--;
    auto itrn = itr;
    itrn++;
    pt -= ((*itrn) - (*itr)) * ((*itr) - (*itrb)) * c[befq];
    tst[befq].erase(itr);
    tst[q].insert(d);
    auto nitr = tst[q].lower_bound(d);
    auto nitrb = nitr;
    nitrb--;
    auto nitrn = nitr;
    nitrn++;
    pt += ((*nitrn) - (*nitr)) * ((*nitr) - (*nitrb)) * c[q];
    debug(befq, *itrb, *itr, *itrn,
          ((*itrn) - (*itr)) * ((*itr) - (*itrb)) * c[befq]);
    debug(q, *nitrb, *nitr, *nitrn,
          ((*itrn) - (*itr)) * ((*itr) - (*itrb)) * c[q]);

    prin(pt);
  }
  return 0;
}