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

#include <algorithm>
#include <cmath>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
// #include <bits/stdc++.h>
using namespace std;

int main() {
  int D;
  cin >> D;
  vector<long int> c(26);
  vector<vector<long int>> s(D, vector<long int>(26));
  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];
  }
  vector<int> t(D, -1);
  vector<vector<int>> memo(26);
  vector<int> memo2(26, -1);
  vector<long int> ans;
  long int ans2 = 0;
  for (int i = 0; i < 26; i++)
    memo[i].push_back(-1);
  for (int i = 0; i < D; i++) {
    cin >> t[i];
    t[i]--;
    memo[t[i]].push_back(i);
    memo2[t[i]] = i;
    ans2 += s[i][t[i]];
    for (int j = 0; j < 26; j++) {
      ans2 -= c[j] * (i - memo2[j]);
    }
  }
  for (int i = 0; i < 26; i++)
    memo[i].push_back(D);
  ans.push_back(ans2);

  int M;
  cin >> M;
  int k = 0;
  vector<int> d(M), q(M);
  for (int i = 0; i < M; i++) {
    cin >> d[i] >> q[i];
    d[i]--;
    q[i]--;
    auto it = lower_bound(memo[t[d[i]]].begin(), memo[t[d[i]]].end(), d[i]);
    auto it1 = it - 1;
    auto it2 = it + 1;
    long int x = ans[k] - (d[i] - *it1) * (*it2 - d[i]) * c[t[d[i]]];
    memo[t[d[i]]].erase(it);
    auto ite = lower_bound(memo[q[i]].begin(), memo[q[i]].end(), d[i]);
    auto ite1 = ite - 1;
    x += (*ite - d[i]) * (d[i] - *ite1) * c[q[i]];
    memo[q[i]].insert(ite, d[i]);
    x += s[d[i]][q[i]] - s[d[i]][t[d[i]]];
    k++;
    ans.push_back(x);
    t[d[i]] = q[i];
  }

  for (size_t i = 1; i < ans.size(); i++)
    cout << ans[i] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
// #include <bits/stdc++.h>
using namespace std;

int main() {
  int D;
  cin >> D;
  vector<long int> c(26);
  vector<vector<long int>> s(D, vector<long int>(26));
  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];
  }
  vector<int> t(D, -1);
  vector<vector<int>> memo(26);
  vector<int> memo2(26, -1);
  vector<long int> ans;
  long int ans2 = 0;
  for (int i = 0; i < 26; i++)
    memo[i].push_back(-1);
  for (int i = 0; i < D; i++) {
    cin >> t[i];
    t[i]--;
    memo[t[i]].push_back(i);
    memo2[t[i]] = i;
    ans2 += s[i][t[i]];
    for (int j = 0; j < 26; j++) {
      ans2 -= c[j] * (i - memo2[j]);
    }
  }
  for (int i = 0; i < 26; i++)
    memo[i].push_back(D);
  ans.push_back(ans2);

  int M;
  cin >> M;
  int k = 0;
  vector<int> d(M), q(M);
  for (int i = 0; i < M; i++) {
    cin >> d[i] >> q[i];
    d[i]--;
    q[i]--;
    auto it = lower_bound(memo[t[d[i]]].begin(), memo[t[d[i]]].end(), d[i]);
    auto it1 = it - 1;
    auto it2 = it + 1;
    long int x = ans[k] - (d[i] - *it1) * (*it2 - d[i]) * c[t[d[i]]];
    memo[t[d[i]]].erase(it);
    auto ite = lower_bound(memo[q[i]].begin(), memo[q[i]].end(), d[i]);
    auto ite1 = ite - 1;
    x += (*ite - d[i]) * (d[i] - *ite1) * c[q[i]];
    memo[q[i]].insert(ite, q[i]);
    x += s[d[i]][q[i]] - s[d[i]][t[d[i]]];
    k++;
    ans.push_back(x);
    t[d[i]] = q[i];
  }

  for (size_t i = 1; i < ans.size(); i++)
    cout << ans[i] << endl;

  return 0;
}