Problem p02620 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using int64 = long long int;

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

const int DAY = 365;
const int CONT = 26;

struct Solver {
  int D, C[CONT], S[DAY][CONT], ans[DAY];
  ll score;
  set<int> schedules[CONT];

  Solver() { fill(ans, ans + DAY, -1); }

  void read_input() {
    cin >> D;
    for (int i = 0; i < CONT; i++) {
      cin >> C[i];
    }

    for (int i = 0; i < D; i++) {
      for (int j = 0; j < CONT; j++) {
        cin >> S[i][j];
      }
    }

    for (int i = 0; i < CONT; i++) {
      schedules[i].emplace(-1);
      schedules[i].emplace(D);
    }
  }

  ll calc_score(bool store = true) {
    ll res = 0;
    int last[CONT] = {};
    for (int i = 0; i < D; i++) {
      res += S[i][ans[i]];
      last[ans[i]] = i + 1;
      for (int j = 0; j < CONT; j++) {
        res -= C[j] * (i + 1 - last[j]);
      }
    }
    if (store)
      score = res;
    return max(0LL, res);
  }

  void update(int day, int contest) {
    int pre_contest = ans[day];
    ans[day] = contest;

    // 削除
    {
      score -= S[day][pre_contest];
      auto ptr = schedules[pre_contest].lower_bound(day);
      ll d0 = day - *prev(ptr);
      ll d1 = *next(ptr) - *prev(ptr);
      ll d2 = *next(ptr) - day;
      score += d0 * (d0 - 1) / 2 * C[pre_contest];
      score -= d1 * (d1 - 1) / 2 * C[pre_contest];
      score += d2 * (d2 - 1) / 2 * C[pre_contest];
      schedules[pre_contest].erase(day);
    }

    // 追加
    {
      score += S[day][contest];
      schedules[contest].emplace(day);
      auto ptr = schedules[contest].lower_bound(day);
      ll d0 = day - *prev(ptr);
      ll d1 = *next(ptr) - *prev(ptr);
      ll d2 = *next(ptr) - day;
      score -= d0 * (d0 - 1) / 2 * C[contest];
      score += d1 * (d1 - 1) / 2 * C[contest];
      score -= d2 * (d2 - 1) / 2 * C[contest];
    }
  }
};

int main() {
  Solver solver;
  solver.read_input();
  for (int i = 0; i < solver.D; i++) {
    int t;
    cin >> t;
    solver.ans[i] = --t;
    solver.schedules[t].emplace(i);
  }
  solver.calc_score();

  int Q;
  cin >> Q;
  while (Q--) {
    int d, q;
    cin >> d >> q;
    d--;
    q--;
    solver.update(d, q);
    cout << solver.score << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int D;
  cin >> D;
  vector<int> c(27, 0);
  for (int i = 1; i < 27; i++)
    cin >> c[i];
  vector<vector<int>> s(D + 1, vector<int>(27, 0));
  for (int d = 1; d < D + 1; d++) {
    for (int i = 1; i < 27; i++)
      cin >> s[d][i];
  }

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

  int M;
  cin >> M;
  vector<int> d(M + 1, 0), q(M + 1, 0);
  for (int i = 1; i < M + 1; i++)
    cin >> d[i] >> q[i];

  for (int m = 1; m < M + 1; m++) {

    int dd = d[m];
    int qq = q[m];

    t[dd] = qq;

    map<int, int> mp;
    int ans = 0;
    for (int d = 1; d < D + 1; d++) {
      ans += s[d][t[d]];
      // cout << ans << endl;
      mp[t[d]] = d;
      for (int i = 1; i < 27; i++)
        ans -= c[i] * (d - mp[i]);
      // cout << ans << endl;
    }
    cout << ans << endl;
  }
}