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

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
#define M 998244353
#define INF 1e16
typedef long long ll;
typedef pair<ll, ll> P;

ll A(ll x) {
  if (x >= 0)
    return x;
  else
    return -x;
}

ll gcd(ll a, ll b) {
  if (b > a) {
    ll tmp = b;
    b = a;
    a = tmp;
  }
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}

ll c[30];
ll t[400];
ll s[400][30];
ll last[400][30];

int main(void) {
  int D;
  cin >> D;
  for (int i = 1; i <= 26; i++) {
    cin >> c[i];
    last[1][i] = 0;
    last[0][i] = 0;
  }
  for (int i = 1; i <= D; i++) {
    for (int j = 1; j <= 26; j++) {
      cin >> s[i][j];
    }
  }
  ll ans = 0;
  for (ll d = 1; d <= D; d++) {
    cin >> t[d];
    for (int i = 1; i <= 26; i++) {
      last[d][i] = last[d - 1][i];
      if (i == t[d])
        last[d][t[d]] = d;
    }
    ans += s[d][t[d]];
    for (ll j = 1; j <= 26; j++) {
      ans -= c[j] * (d - last[d][j]);
    }
  }
  ll m;
  cin >> m;
  /*for(int i=1;i<=26;i++){
      for(int j=1;j<=D;j++){
          cout<<last[j][i]<<" ";
      }
      cout<<endl;
  }*/
  for (int query = 0; query < m; query++) {
    ll d, q;
    cin >> d >> q;
    ans += s[d][q] - s[d][t[d]];
    ll old1 = last[d][t[d]];
    ll new1 = last[d - 1][t[d]];
    ll old2 = last[d][q];
    ll new2 = d;
    for (ll i = d; i <= D; i++) {
      ans += c[t[d]] * (i - last[i][t[d]]);
      if (last[i][t[d]] == old1)
        last[i][t[d]] = new1;
      ans -= c[t[d]] * (i - last[i][t[d]]);
    }
    for (ll i = d; i <= D; i++) {
      ans += c[q] * (i - last[i][q]);
      if (last[i][q] == old2)
        last[i][q] = new2;
      ans -= c[q] * (i - last[i][q]);
    }
    // cout<<query+1<<endl;
    /*for(int i=1;i<=26;i++){
    for(int j=1;j<=D;j++){
        cout<<last[j][i]<<" ";
    }
    cout<<endl;
}*/
    t[d] = q;
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
#define M 998244353
#define INF 1e16
typedef long long ll;
typedef pair<ll, ll> P;

ll A(ll x) {
  if (x >= 0)
    return x;
  else
    return -x;
}

ll gcd(ll a, ll b) {
  if (b > a) {
    ll tmp = b;
    b = a;
    a = tmp;
  }
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}

ll c[27];
ll t[27];
ll s[400][27];
ll last[400][27];
ll score[400][27];

int main(void) {
  int D;
  cin >> D;
  for (int i = 1; i <= 26; i++) {
    cin >> c[i];
    last[0][i] = 0;
  }
  for (int i = 1; i <= D; i++) {
    for (int j = 1; j <= 26; j++) {
      cin >> s[i][j];
    }
  }
  ll ans = 0;
  for (ll d = 1; d <= D; d++) {
    cin >> t[d];
    for (int i = 1; i <= 26; i++) {
      last[d][i] = last[d - 1][i];
      if (i == t[d])
        last[d][t[d]] = d;
    }
    ans += s[d][t[d]];
    for (ll j = 1; j <= 26; j++) {
      ans -= c[j] * (d - last[d][j]);
    }
  }
  ll m;
  cin >> m;
  for (int query = 0; query < m; query++) {
    ll d, q;
    cin >> d >> q;
    ans += s[d][q] - s[d][t[d]];
    ll old1 = last[d][t[d]];
    ll new1 = last[d - 1][t[d]];
    ll old2 = last[d][q];
    ll new2 = d;
    for (ll i = d; i <= D; i++) {
      ans += c[t[d]] * (i - last[i][t[d]]);
      if (last[i][t[d]] == old1)
        last[i][t[d]] = new1;
      ans -= c[t[d]] * (i - last[i][t[d]]);
    }
    for (ll i = d; i <= D; i++) {
      ans += c[q] * (i - last[i][q]);
      if (last[i][q] == old2)
        last[i][q] = new2;
      ans -= c[q] * (i - last[i][q]);
    }
    t[d] = q;
    cout << ans << endl;
  }
  return 0;
}