alexjercan/bugnet huggingface dataset:
Problem p02620 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef long long ll;

int D, M, d, q;
int c[30];
int s[400][30];
int type[400];
int last[29];
ll satis_level[400];

int calc_score() {
  rep(i, 0, 400) satis_level[i] = 0;
  rep(j, 1, 27) last[j] = 0;
  ll down_satis;
  rep(i, 1, D + 1) {
    down_satis = 0;
    last[type[i]] = i;
    satis_level[i] += s[i][type[i]];
    rep(j, 1, 27) down_satis += c[j] * (i - last[j]);
    satis_level[i] -= down_satis;
    satis_level[i + 1] += satis_level[i];
  }
  return 0;
}

int main() {
  cin >> D;
  rep(i, 1, 27) cin >> c[i];
  rep(i, 1, D + 1) {
    rep(j, 1, 27) { cin >> s[i][j]; }
  }

  rep(j, 1, D + 1) cin >> type[j];
  cin >> M;

  rep(query, 1, M + 1) {
    cin >> d >> q;
    type[d] = q;
    calc_score();
    cout << satis_level[D] << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef long long ll;

int D, M, d, q;
int c[30];
int s[400][30];
int type[400];
int last[400][30];
ll satis_level[400];

int calc_score() {
  rep(i, 0, 400) satis_level[i] = 0;
  rep(i, 1, D + 1) rep(j, 1, 27) last[i][j] = 0;
  ll down_satis;
  rep(i, 1, D + 1) {
    down_satis = 0;
    rep(k, 1, 400) last[k][type[i]] = i;
    satis_level[i] += s[i][type[i]];
    rep(j, 1, 27) down_satis += c[j] * (i - last[i][j]);
    satis_level[i] -= down_satis;
    satis_level[i + 1] += satis_level[i];
  }
  return 0;
}

int main() {
  cin >> D;
  rep(i, 1, 27) cin >> c[i];
  rep(i, 1, D + 1) {
    rep(j, 1, 27) {
      cin >> s[i][j];
      last[i][j] = 0;
    }
  }
  rep(j, 1, D + 1) cin >> type[j];
  cin >> M;

  rep(query, 1, M + 1) {
    cin >> d >> q;
    type[d] = q;
    calc_score();
    cout << satis_level[D] << '\\n';
  }
  return 0;
}
Problem p02620 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

int main() {
  int D;
  std::cin >> D;
  std::vector<int> C(26);
  for (int i = 0; i < 26; i++) {
    std::cin >> C[i];
  }
  std::vector<std::vector<int>> S(D, std::vector<int>(26));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < 26; j++) {
      std::cin >> S[i][j];
    }
  }
  std::vector<int> T(D);
  for (int i = 0; i < D; i++) {
    std::cin >> T[i];
  }

  int M;
  std::cin >> M;
  std::vector<int> D_query(M);
  std::vector<int> Q_query(M);
  for (int i = 0; i < M; i++) {
    std::cin >> D_query[i] >> Q_query[i];
  }

  std::vector<int> V(M);
  for (int q = 0; q < M; q++) {
    T[D_query[q] - 1] = Q_query[q];
    long long ans = 0;
    std::vector<int> last(26, 0);
    for (int i = 0; i < D; i++) {
      ans += S[i][T[i] - 1];
      last[T[i] - 1] = i + 1;
      for (int j = 0; j < 26; j++) {
        ans -= (i + 1 - last[j]) * C[j];
      }
    }
    V[q] = ans;
  }

  for (int i = 0; i < M; i++) {
    std::cout << V[i] << std::endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

int main() {
  int D;
  std::cin >> D;
  std::vector<int> C(26);
  for (int i = 0; i < 26; i++) {
    std::cin >> C[i];
  }
  std::vector<std::vector<int>> S(D, std::vector<int>(26));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < 26; j++) {
      std::cin >> S[i][j];
    }
  }
  std::vector<int> T(D);
  for (int i = 0; i < D; i++) {
    std::cin >> T[i];
  }

  int M;
  std::cin >> M;
  std::vector<int> D_query(M);
  std::vector<int> Q_query(M);
  for (int i = 0; i < M; i++) {
    std::cin >> D_query[i] >> Q_query[i];
  }

  std::vector<int> V(D);
  for (int q = 0; q < M; q++) {
    T[D_query[q] - 1] = Q_query[q];
    long long ans = 0;
    std::vector<int> last(26, 0);
    for (int i = 0; i < D; i++) {
      ans += S[i][T[i] - 1];
      last[T[i] - 1] = i + 1;
      for (int j = 0; j < 26; j++) {
        ans -= (i + 1 - last[j]) * C[j];
      }
    }
    V[q] = ans;
  }

  for (int i = 0; i < M; i++) {
    std::cout << V[i] << std::endl;
  }

  return 0;
}
Problem p02620 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int main() {
  int d;
  cin >> d;
  const int n = 26;
  vector<int> c(n);
  vector<vector<int>> s(d, vector<int>(n));
  rep(i, n) cin >> c[i];
  rep(i, d) rep(j, n) cin >> s[i][j];
  vector<int> t(d), last(n);
  rep(i, d) {
    cin >> t[i];
    t[i]--;
  }
  int m;
  cin >> m;
  vector<int> qd(m), qq(m);
  rep(i, m) {
    cin >> qd[i] >> qq[i];
    qd[i]--;
    qq[i]--;
  }
  rep(Q, m) {
    // 点数のシミュレーション
    t[qd[Q]] = qq[Q];
    vector<int> nlast(n);
    ll now = 0;
    rep(i, d) {
      now += s[i][t[i]];
      nlast[t[i]] = i + 1;
      rep(j, n) { now -= c[j] * (i + 1 - nlast[j]); }
    }
    cout << now << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int main() {
  int d;
  cin >> d;
  const int n = 26;
  vector<int> c(n);
  vector<vector<int>> s(d, vector<int>(n));
  rep(i, n) cin >> c[i];
  rep(i, d) rep(j, n) cin >> s[i][j];
  vector<int> t(d), last(n);
  rep(i, d) {
    cin >> t[i];
    t[i]--;
  }
  int m;
  cin >> m;
  vector<int> qd(m), qq(m);
  rep(i, m) {
    cin >> qd[i] >> qq[i];
    qd[i]--;
    qq[i]--;
  }
  // ll ans = 0;
  // rep(i, d) {
  // 	ans += s[i][t[i]];
  // 	last[t[i]] = i+1;
  // 	rep(j, n) {
  // 		ans -= c[j] * (i+1-last[j]);
  // 	}
  // 	// cout << ans << endl;
  // }
  rep(Q, m) {
    // 点数のシミュレーション
    t[qd[Q]] = qq[Q];
    vector<int> nlast(n);
    ll now = 0;
    rep(i, d) {
      now += s[i][t[i]];
      nlast[t[i] - 1] = i + 1;
      rep(j, n) { now -= c[j] * (i + 1 - nlast[j - 1]); }
    }

    cout << now << endl;
  }
  return 0;
}
Problem p02620 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define cinf(n, x)                                                             \\
  for (int i = 0; i < (n); i++)                                                \\
    cin >> x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define LB(a, x) lb(all(a), x) - a.begin()
#define UB(a, x) ub(all(a), x) - a.begin()
#define mod 1000000007
#define FS fixed << setprecision(15)
using namespace std;
typedef long long ll;
const double pi = 3.141592653589793;
template <class T> using V = vector<T>;
using Graph = vector<vector<int>>;
using P = pair<ll, ll>;
typedef unsigned long long ull;
typedef long double ldouble;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const ll INF = 1e18;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll D;
  cin >> D;
  V<ll> c(26);

  cinf(26, c);
  V<P> p(26);
  V<ll> t(D);
  set<P> st;
  ll sum = 0;
  /*rep(i,26){
      p[i]=make_pair(c[i],i);
  }
  sort(all(p));*/
  ll s[D][26];
  rep(i, D) { rep(j, 26) cin >> s[i][j]; }
  rep(i, D) {
    cin >> t[i];
    t[i]--;
  }
  V<ll> ans(D);

  ll M;
  cin >> M;
  while (M--) {
    ll d, q;
    cin >> d >> q;
    d--;
    q--;
    t[d] = q;
    ll now = 0;
    V<ll> lst(26, 1);
    rep(i, D) {

      ll cnt = 0;
      rep(j, 26) {
        if (j != t[i]) {
          cnt += c[j] * lst[j];
        }
      }
      now += s[i][t[i]] - cnt;

      lst[t[i]] = 1;
      rep(j, 26) {
        if (j != t[i])
          lst[j]++;
      }
    }
    cout << now << '\\n';
  }
}


Error: TLE

Fixed submission:

#define rep(i, n) for (int i = 0; i < n; i++)
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using P = pair<int, int>;

int calc_sat(int D, int N, vector<int> c, vector<vector<int>> s,
             vector<int> t) {
  vector<int> last(N, -1);
  int sat = 0;
  rep(d, D) {
    last[t[d]] = d;
    sat += s[d][t[d]];
    rep(i, N) { sat -= c[i] * (d - last[i]); }
  }
  return sat;
}

int main() {
  int D;
  cin >> D;
  const int N = 26;
  vector<int> c(N);
  rep(i, N) cin >> c[i];
  vector<vector<int>> s(D, vector<int>(N));
  rep(i, D) rep(j, N) cin >> s[i][j];
  vector<int> t(D);
  rep(i, D) {
    cin >> t[i];
    t[i]--;
  }

  int M;
  cin >> M;
  vector<int> d(M), q(M);
  rep(i, M) {
    cin >> d[i] >> q[i];
    d[i]--;
    q[i]--;
  }

  rep(i, M) {
    t[d[i]] = q[i];
    int sat = calc_sat(D, N, c, s, t);
    printf("%d\\n", sat);
  }

  return 0;
}
Problem p02620 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int D;
int c[26];
int l[367][26];
int s[366][26];
int t[366];
int m, p, q, d, k;
int md[100000];
int mq[100000];
int ans;
int main() {
  scanf("%d", &D);
  for (int i = 0; i < 26; i++) {
    scanf("%d", c + i);
  }
  for (int i = 1; i <= D; i++) {
    for (int j = 0; j < 26; j++) {
      scanf("%d", s[i] + j);
    }
  }
  for (int i = 1; i <= D; i++) {
    scanf("%d", t + i);
    t[i]--;
  }
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d", md + i, mq + i);
    mq[i]--;
  }
  for (int i = 1; i <= D; i++) {
    ans += s[i][t[i]];
    for (int j = 0; j < 26; j++) {
      l[i][j] = l[i - 1][j] + 1;
    }
    l[i][t[i]] = 0;
    for (int j = 0; j < 26; j++) {
      ans -= c[j] * l[i][j];
    }
  }
  for (int i = 0; i < m; i++) {
    q = mq[i];
    d = md[i];
    p = t[d];
    t[d] = q;
    ans -= s[d][p];
    ans += s[d][q];
    k = d;
    while (l[k][q] && k <= D) {
      ans -= c[q] * (k - d - l[k][q]);
      l[k][q] = k - d;
      k++;
    }
    k = d;
    do {
      ans -= c[p] * (l[k - 1][p] + 1 - l[k][p]);
      l[k][p] = l[k - 1][p] + 1;
      k++;
    } while (l[k][p] && k <= D);
    printf("%d\\n", ans);
  }
}

Error: 0

Fixed submission:

#include <stdio.h>
int D;
int c[26];
int l[26][367];
int s[366][26];
int t[366];
int m, p, q, d, k;
int md[100000];
int mq[100000];
int ans;
int main() {
  scanf("%d", &D);
  for (int i = 0; i < 26; i++) {
    scanf("%d", c + i);
  }
  for (int i = 1; i <= D; i++) {
    for (int j = 0; j < 26; j++) {
      scanf("%d", s[i] + j);
    }
  }
  for (int i = 1; i <= D; i++) {
    scanf("%d", t + i);
    t[i]--;
  }
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d", md + i, mq + i);
    mq[i]--;
  }
  for (int i = 1; i <= D; i++) {
    ans += s[i][t[i]];
    for (int j = 0; j < 26; j++) {
      l[i][j] = l[i - 1][j] + 1;
    }
    l[i][t[i]] = 0;
    for (int j = 0; j < 26; j++) {
      ans -= c[j] * l[i][j];
    }
  }
  for (int i = 0; i < m; i++) {
    q = mq[i];
    d = md[i];
    p = t[d];
    t[d] = q;
    k = d;
    ans -= s[d][p];
    ans += s[d][q];
    while (l[k][q]) {
      ans -= c[q] * (k - d - l[k][q]);
      l[k][q] = k - d;
      k++;
    }
    k = d;
    do {
      ans -= c[p] * (l[k - 1][p] + 1 - l[k][p]);
      l[k][p] = l[k - 1][p] + 1;
      k++;
    } while (l[k][p]);
    printf("%d\\n", ans);
  }
}
Problem p02620 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;

int main() {
  int D;
  cin >> D;
  vector<int> C(26);
  vector<vector<int>> S(D, vector<int>(26));
  REP(i, 0, 26) cin >> C[i];
  REP(i, 0, D) REP(j, 0, 26) cin >> S[i][j];

  vector<int> T(D);
  REP(i, 0, D) cin >> T[i];

  vector<vector<int>> last(26, vector<int>(D, 0));
  vector<ll> ret(26, 0);
  REP(i, 0, D) {
    ret[T[i] - 1] += S[i][T[i] - 1];
    last[T[i] - 1][i] = i + 1;
    REP(j, 0, 26) {
      if (i > 0 && !last[j][i])
        last[j][i] = last[j][i - 1];
      ret[j] -= C[j] * (i + 1 - last[j][i]);
    }
  }

  int M;
  cin >> M;
  REP(i, 0, M) {
    int d, q;
    cin >> d >> q;
    d--;

    int prev = T[d] - 1, next = q - 1;
    T[d] = q;
    last[prev].assign(D, 0);
    last[next].assign(D, 0);
    ret[prev] = 0;
    ret[next] = 0;
    REP(i, 0, D) {
      if (T[i] == prev + 1 || T[i] == next + 1) {
        ret[T[i] - 1] += S[i][T[i] - 1];
        last[T[i] - 1][i] = i + 1;
      }
      if (i > 0) {
        if (!last[prev][i])
          last[prev][i] = last[prev][i - 1];
        if (!last[next][i])
          last[next][i] = last[next][i - 1];
      }
      ret[prev] -= C[prev] * (i + 1 - last[prev][i]);
      ret[next] -= C[next] * (i + 1 - last[next][i]);
    }
    cout << accumulate(ALL(ret), 0LL) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;

int main() {
  int D;
  cin >> D;
  vector<int> C(26);
  vector<vector<int>> S(D, vector<int>(26));
  REP(i, 0, 26) cin >> C[i];
  REP(i, 0, D) REP(j, 0, 26) cin >> S[i][j];

  vector<int> T(D);
  REP(i, 0, D) cin >> T[i];

  ll ret = 0;
  vector<vector<int>> last(26, vector<int>(D, 0));
  REP(i, 0, D) {
    ret += S[i][T[i] - 1];
    last[T[i] - 1][i] = i + 1;
    REP(j, 0, 26) {
      if (i > 0 && !last[j][i])
        last[j][i] = last[j][i - 1];
      ret -= C[j] * (i + 1 - last[j][i]);
    }
  }

  int M;
  cin >> M;
  REP(i, 0, M) {
    int d, q;
    cin >> d >> q;
    d--;
    T[d] = q;
    ll ret = 0;
    vector<vector<int>> last(26, vector<int>(D, 0));
    REP(i, 0, D) {
      ret += S[i][T[i] - 1];
      last[T[i] - 1][i] = i + 1;
      REP(j, 0, 26) {
        if (i > 0 && !last[j][i])
          last[j][i] = last[j][i - 1];
        ret -= C[j] * (i + 1 - last[j][i]);
      }
    }
    cout << ret << endl;
  }
  return 0;
}
Problem p02620 (C++) — buggy submission, verdict: Time Limit Exceeded

// C - Incremental Scoring
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int N = 26;
int D;

vector<int> c(N + 1);
vector<vector<int>> s;
vector<int> t;

ll scoring() {
  vector<int> last(N + 1, 0);
  ll score = 0;
  for (int d = 1; d <= D; d++) {

    score += s[d][t[d]];
    last[t[d]] = d;

    int penalty = 0;
    for (int i = 1; i <= N; i++) {
      penalty += c[i] * (d - last[i]);
    }

    score -= penalty;
  }
  return score;
}

ll exchange(int d, int q) {
  t[d] = q;
  return scoring();
}

int main() {

  cin >> D;

  s.resize(D + 1);
  for (int i = 0; i <= D; i++) {
    s[i].resize(N + 1);
  }
  t.resize(D + 1);

  for (int i = 1; i <= N; i++) {
    cin >> c[i];
  }

  for (int i = 1; i <= D; i++) {
    for (int j = 1; j <= N; j++) {
      cin >> s[i][j];
    }
  }
  for (int i = 1; i <= D; i++) {
    cin >> t[i];
  }

  // cout<<scoring()<<endl;

  // 解交換
  int M;
  cin >> M;

  vector<int> ex_d(M);
  vector<int> q(M);
  for (int i = 0; i < M; i++) {
    cin >> ex_d[i] >> q[i];
  }
  for (int i = 0; i < M; i++) {
    /*
    for(int i=1; i<D+1; i++){
        cout<<t[i]<<" ";
    }
    cout<<endl;
    */
    cout << exchange(ex_d[i], q[i]) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll N = 26;
ll D = 365;

ll scoring(vector<ll> c, vector<vector<ll>> s, vector<ll> t, vector<ll> last) {
  ll score = 0;
  for (int d = 0; d < D; d++) {
    score += s[d][t[d]];
    last[t[d]] = d + 1;

    // int penalty = 0;
    for (int i = 0; i < N; i++) {
      score -= c[i] * ((d + 1) - last[i]);
    }
    // cout<<"ペナルティ : "<<penalty<<endl;
    // score -= penalty;
  }
  return score;
}

int main() {
  cin >> D;

  vector<ll> c(N);
  vector<vector<ll>> s(D, vector<ll>(N));
  vector<ll> t(D);
  vector<ll> last(N, 0);

  for (int i = 0; i < N; i++) {
    cin >> c[i];
  }

  for (int i = 0; i < D; i++) {
    for (int j = 0; j < N; j++) {
      cin >> s[i][j];
    }
  }
  for (int i = 0; i < D; i++) {
    cin >> t[i];
    t[i]--;
  }

  int M;
  cin >> M;
  vector<ll> d(M);
  vector<ll> q(M);
  for (int i = 0; i < M; i++) {
    cin >> d[i] >> q[i];
    d[i]--;
    q[i]--;
    t[d[i]] = q[i];
    cout << scoring(c, s, t, last) << endl;
  }

  return 0;
}
Problem p02620 (C++) — buggy submission, verdict: Runtime Error

#include <cassert>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

int D;
vector<int> C;
vector<vector<int>> S;

struct State {
  vector<int> T;
  vector<set<int>> X;
  long long score;
  State() : T(D, -1), X(26, set<int>{-1, D}) {
    score = 0;
    for (int i = 0; i < 26; i++)
      score -= (long long)C[i] * D * (D + 1) / 2;
  }

  void add(int d, int t) {
    assert(T[d] == -1);
    assert(X[t].count(d) == 0);

    auto p1 = X[t].lower_bound(d);
    auto p0 = p1;
    p0--;
    score += (long long)C[t] * (d - *p0) * (*p1 - d);
    score += S[d][t];
    X[t].insert(d);
    T[d] = t;
  }

  void remove(int d) {
    assert(T[d] != -1);
    assert(X[T[d]].count(d) > 0);

    int t = T[d];
    auto p = X[t].find(d);
    auto p0 = p;
    p0--;
    auto p1 = p;
    p1++;
    score -= (long long)C[t] * (d - *p0) * (*p1 - d);
    score -= S[d][t];
    X[t].erase(d);
    T[d] = -1;
  }
};

int main() {
  cin >> D;
  C = vector<int>(26);
  for (int &c : C)
    cin >> c;
  S = vector<vector<int>>(D, vector<int>(26));
  for (auto &s : S)
    for (auto &x : s)
      cin >> x;

  State state;
  for (int i = 0; i < D; i++) {
    int t;
    cin >> t;
    t--;
    state.add(i, t);
  }
  int M;
  cin >> M;
  for (int i = 0; i < M; i++) {
    int d;
    int q;
    cin >> d >> q;
    d--, q--;
    state.remove(d);
    state.add(d, q);
    cout << state.score << endl;
  }
}


Error: 0

Fixed submission:

#include <cassert>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

int D;
vector<int> C;
vector<vector<int>> S;

struct State {
  vector<int> T;
  vector<set<int>> X;
  long long score;
  State() : T(26, -1), X(26, set<int>{-1, D}) {
    score = 0;
    for (int i = 0; i < 26; i++)
      score -= (long long)C[i] * D * (D + 1) / 2;
  }

  void add(int d, int t) {
    assert(T[d] == -1);
    assert(X[t].count(d) == 0);

    auto p1 = X[t].lower_bound(d);
    auto p0 = p1;
    p0--;
    score += (long long)C[t] * (d - *p0) * (*p1 - d);
    score += S[d][t];
    X[t].insert(d);
    T[d] = t;
  }

  void remove(int d) {
    assert(T[d] != -1);
    assert(X[T[d]].count(d) > 0);

    int t = T[d];
    auto p = X[t].find(d);
    auto p0 = p;
    p0--;
    auto p1 = p;
    p1++;
    score -= (long long)C[t] * (d - *p0) * (*p1 - d);
    score -= S[d][t];
    X[t].erase(d);
    T[d] = -1;
  }
};

int main() {
  cin >> D;
  C = vector<int>(26);
  for (int &c : C)
    cin >> c;
  S = vector<vector<int>>(D, vector<int>(26));
  for (auto &s : S)
    for (auto &x : s)
      cin >> x;

  State state;
  for (int i = 0; i < D; i++) {
    int t;
    cin >> t;
    t--;
    state.add(i, t);
  }
  int M;
  cin >> M;
  for (int i = 0; i < M; i++) {
    int d;
    int q;
    cin >> d >> q;
    d--, q--;
    state.remove(d);
    state.add(d, q);
    cout << state.score << endl;
  }
}
Problem p02620 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

int c[27];
int s[366][27];
int t[366];
int last[27];

int main(void) {
  int D;
  cin >> D;

  for (int i = 1; i <= 26; i++) {
    cin >> c[i];
  }

  for (int i = 1; i <= D; i++) {
    for (int j = 1; j <= 26; j++) {
      cin >> s[i][j];
    }
  }

  int v = 0;
  for (int i = 1; i <= D; i++) {
    cin >> t[i];
    last[t[i]] = i;
    v += s[i][t[i]];

    for (int j = 1; j <= 26; j++)
      v -= c[j] * (i - last[j]);
    // cout<<v<<endl;
  }

  int M;
  cin >> M;
  int d, q;
  for (int i = 0; i < M; i++) {
    v = 0;
    fill(last, last + 27, 0);
    cin >> d >> q;
    t[d] = q;
    for (int j = 1; j <= D; j++) {
      last[t[j]] = j;
      v += s[j][t[j]];

      for (int k = 1; k <= 26; k++)
        v -= c[k] * (j - last[k]);
    }
    cout << v << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int c[27];
int s[366][27];
int t[366];
int sol[366];

int last_day(int d, int type) {
  int lday = 0;
  for (int i = 1; i <= d; i++) {
    if (t[i] == type)
      lday = i;
  }

  return lday;
}

int dissatisfied(int d) {
  int dis = 0;
  for (int j = 1; j <= 26; j++) {
    dis += c[j] * (d - last_day(d, j));
  }
  return dis;
}

int solve(int d) {
  int res = 0;
  for (int i = 1; i <= d; i++) {
    res = res + s[i][t[i]] - dissatisfied(i);
    sol[i] = res;
  }
  return res;
}

int main(void) {
  int D;
  cin >> D;

  for (int i = 1; i <= 26; i++) {
    cin >> c[i];
  }

  for (int i = 1; i <= D; i++) {
    for (int j = 1; j <= 26; j++) {
      cin >> s[i][j];
    }
  }

  for (int i = 1; i <= D; i++) {
    cin >> t[i];
  }

  int M;
  cin >> M;
  int d, q;
  for (int i = 0; i < M; i++) {
    cin >> d >> q;
    t[d] = q;
    cout << solve(D) << endl;
  }

  return 0;
}
Problem p02620 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int d;
  int c[27];
  int s[400][27];
  int t[400];
  static int last[27]; // 最後の開催日
  int ans = 0;
  int kotae[100005];

  cin >> d;
  rep(i, 26) cin >> c[i + 1];
  rep(i, d) {
    rep(j, 26) { cin >> s[i + 1][j + 1]; }
  }

  rep(i, d) { cin >> t[i + 1]; }

  int m;
  cin >> m;
  rep(k, m) {
    int a, b;
    cin >> a >> b;
    t[a] = b;
    // 初期化
    ans = 0;
    rep(i, 26) last[i + 1] = 0;
    for (int i = 1; i <= d; i++) {
      ans += s[i][t[i]];
      last[t[i]] = i;
      for (int j = 1; j <= 26; j++)
        ans -= (i - last[j]) * c[j];
    }
    kotae[k] = ans;
  }

  rep(i, m) cout << kotae[i] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int d;
  int c[27];
  int s[400][27];
  int t[400];
  static int last[27]; // 最後の開催日
  int ans = 0;
  int kotae[500];

  cin >> d;
  rep(i, 26) cin >> c[i + 1];
  rep(i, d) {
    rep(j, 26) { cin >> s[i + 1][j + 1]; }
  }

  rep(i, d) { cin >> t[i + 1]; }

  int m;
  cin >> m;
  rep(k, m) {
    int a, b;
    cin >> a >> b;
    t[a] = b;
    // 初期化
    ans = 0;
    rep(i, 26) last[i + 1] = 0;
    for (int i = 1; i <= d; i++) {
      ans += s[i][t[i]];
      last[t[i]] = i;
      for (int j = 1; j <= 26; j++)
        ans -= (i - last[j]) * c[j];
    }
    kotae[k] = ans;
  }

  rep(i, m) cout << kotae[i] << endl;
  return 0;
}