Problem p02620 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  int d;
  cin >> d;

  vector<int> c;
  for (int i = 0; i < 26; i++) {
    int tmp;
    cin >> tmp;
    c.push_back(tmp);
  }

  vector<vector<int>> s(d, vector<int>(26, 0));
  for (int i = 0; i < d; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s[i].at(j);
    }
  }

  vector<int> t;
  for (int i = 0; i < d; i++) {
    int tmp;
    cin >> tmp;
    t.push_back(tmp);
  }

  int m;
  cin >> m;

  vector<int> d_;
  vector<int> q;
  for (int i = 0; i < m; i++) {
    int tmp1, tmp2;
    cin >> tmp1 >> tmp2;
    d_.push_back(tmp1);
    q.push_back(tmp2);
  }
  /*
  vector<int> last(26,0);
  int ans = 0;
  int i = 0;
  while(i<d){
      int dec = 0;
      ans += s[i][t[i]-1];
      last[t[i]-1] = i+1;
      for(int j=0; j<26; j++){
          dec += c[j] * (i+1-last[j]);
      }
      ans -= dec;
      i++;
      cout << ans << endl;
  }
  */
  vector<int> last_(26, 0);
  int total;
  for (int k = 0; k < d; k++) {
    // int tmp3 = t[d_[k]-1];
    t[d_[k] - 1] = q[k];
    /*
    for(int l=0;l<d;l++){
        cout << t[l] << " ";
    }
    cout << endl;
    */
    // cout << t[d_[j]-1] << endl;
    // cout << tmp3 << endl;
    int i_ = 0;
    long long ans_ = 0;
    for (int a = 0; a < 26; a++) {
      last_[a] = 0;
    }
    while (i_ < d) {
      long long dec_ = 0;
      // cout << t[i_] << endl;
      ans_ += s[i_][t[i_] - 1];
      last_[t[i_] - 1] = i_ + 1;
      for (int j = 0; j < 26; j++) {
        dec_ += c[j] * (i_ + 1 - last_[j]);
      }
      ans_ -= dec_;
      i_++;
    }
    cout << ans_ << endl;
    // cout << tmp3 << endl;
    // total = max(ans,ans_);
    /*
    if(total == ans){
        t[d_[j]-1] = tmp3;
    }
    */
    // cout << total << endl;
  }

  /*
  //デバッグ処理
  for(int i=0; i<26; i++){
      cout << c[i] << " ";
  }
  cout << endl;
  for(int i=0; i<d; i++){
      for(int j=0; j<26; j++){
          cout << s[i][j] << " ";
      }
      cout << endl;
  }
  for(int i=0; i<d; i++){
      cout << t[i] << " ";
  }
  */
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  int d;
  cin >> d;

  vector<int> c;
  for (int i = 0; i < 26; i++) {
    int tmp;
    cin >> tmp;
    c.push_back(tmp);
  }

  vector<vector<int>> s(d, vector<int>(26, 0));
  for (int i = 0; i < d; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s[i].at(j);
    }
  }

  vector<int> t;
  for (int i = 0; i < d; i++) {
    int tmp;
    cin >> tmp;
    t.push_back(tmp);
  }

  int m;
  cin >> m;

  vector<int> d_;
  vector<int> q;
  for (int i = 0; i < m; i++) {
    int tmp1, tmp2;
    cin >> tmp1 >> tmp2;
    d_.push_back(tmp1);
    q.push_back(tmp2);
  }
  /*
  vector<int> last(26,0);
  int ans = 0;
  int i = 0;
  while(i<d){
      int dec = 0;
      ans += s[i][t[i]-1];
      last[t[i]-1] = i+1;
      for(int j=0; j<26; j++){
          dec += c[j] * (i+1-last[j]);
      }
      ans -= dec;
      i++;
      cout << ans << endl;
  }
  */
  vector<int> last_(26, 0);
  int total;
  for (int k = 0; k < m; k++) {
    // int tmp3 = t[d_[k]-1];
    t[d_[k] - 1] = q[k];
    /*
    for(int l=0;l<d;l++){
        cout << t[l] << " ";
    }
    cout << endl;
    */
    // cout << t[d_[j]-1] << endl;
    // cout << tmp3 << endl;
    int i_ = 0;
    long long ans_ = 0;
    for (int a = 0; a < 26; a++) {
      last_[a] = 0;
    }
    while (i_ < d) {
      long long dec_ = 0;
      // cout << t[i_] << endl;
      ans_ += s[i_][t[i_] - 1];
      last_[t[i_] - 1] = i_ + 1;
      for (int j = 0; j < 26; j++) {
        dec_ += c[j] * (i_ + 1 - last_[j]);
      }
      ans_ -= dec_;
      i_++;
    }
    cout << ans_ << endl;
    // cout << tmp3 << endl;
    // total = max(ans,ans_);
    /*
    if(total == ans){
        t[d_[j]-1] = tmp3;
    }
    */
    // cout << total << endl;
  }

  /*
  //デバッグ処理
  for(int i=0; i<26; i++){
      cout << c[i] << " ";
  }
  cout << endl;
  for(int i=0; i<d; i++){
      for(int j=0; j<26; j++){
          cout << s[i][j] << " ";
      }
      cout << endl;
  }
  for(int i=0; i<d; i++){
      cout << t[i] << " ";
  }
  */
  return 0;
}