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

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

class State {
private:
  int D;
  vector<int> c;
  vector<vector<int>> s;
  vector<int> t;
  int64_t score;
  vector<vector<int>> ds;

  int sum(int x) { return x * (x + 1) / 2; }

public:
  State(int D, vector<int> c, vector<vector<int>> s, vector<int> t)
      : D(D), c(c), s(s), t(t) {
    score = 0;
    ds = vector<vector<int>>(26);
    vector<int> last(26, -1);
    for (int d = 0; d < D; d++) {
      last.at(t.at(d)) = d;
      score += s.at(d).at(t.at(d));
      for (int i = 0; i < 26; i++) {
        if (i == t.at(d))
          continue;
        score -= c.at(i) * (d - last.at(i));
      }
      ds.at(t.at(d)).push_back(d);
    }
  }

  int64_t getScore() { return score; }

  void change(int d, int td_new) {
    int td_old = t.at(d);

    {
      int idx = 0;
      while (idx < (int)ds.at(td_old).size() && ds.at(td_old).at(idx) != d)
        idx++;
      int d_prev = (idx - 1 < 0) ? -1 : ds.at(td_old).at(idx - 1);
      int d_next = (idx + 1 >= (int)ds.at(td_old).size())
                       ? D
                       : ds.at(td_old).at(idx + 1);
      score -= s.at(d).at(td_old);
      score += sum(d - 1 - d_prev) * c.at(td_old);
      score += sum(d_next - 1 - d) * c.at(td_old);
      score -= sum(d_next - 1 - d_prev) * c.at(td_old);
      ds.at(td_old).erase(ds.at(td_old).begin() + idx);
    }
    {
      int idx = 0;
      while (idx < (int)ds.at(td_new).size() && ds.at(td_new).at(idx) < d)
        idx++;
      int d_prev = (idx - 1 < 0) ? -1 : ds.at(td_new).at(idx - 1);
      int d_next =
          (idx >= (int)ds.at(td_new).size()) ? D : ds.at(td_new).at(idx);
      score += s.at(d).at(td_new);
      score -= sum(d - 1 - d_prev) * c.at(td_new);
      score -= sum(d_next - 1 - d) * c.at(td_new);
      score += sum(d_next - 1 - d_prev) * c.at(td_new);
      ds.at(td_new).insert(ds.at(td_new).begin() + idx, d);
    }

    t.at(d) = td_new;
  }
};

void solve(int D, vector<int> c, vector<vector<int>> s, vector<int> t, int M,
           vector<int> d, vector<int> q) {
  State state = State(D, c, s, t);
  for (int i = 0; i < M; i++) {
    state.change(d.at(i), q.at(i));
    cout << state.getScore() << endl;
  }
}

int main() {
  int D;
  cin >> D;
  vector<int> c(26);
  for (int i = 0; i < 26; i++) {
    cin >> c.at(i);
  }
  vector<vector<int>> s(D, vector<int>(26));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s.at(i).at(j);
    }
  }
  vector<int> t(D);
  for (int i = 0; i < D; i++) {
    cin >> t.at(i);
    t.at(i)--;
  }
  int M;
  cin >> M;
  vector<int> d(M);
  vector<int> q(M);
  for (int i = 0; i < M; i++) {
    cin >> d.at(i);
    cin >> q.at(i);
    d.at(i)--;
    q.at(i)--;
  }
  solve(D, move(c), move(s), move(t), M, move(d), move(q));
  return 0;
}


Error: TLE

Fixed submission:

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

int calculate(int D, vector<int> c, vector<vector<int>> s, vector<int> t) {
  vector<int> last(26, -1);
  int res = 0;
  for (int d = 0; d < D; d++) {
    last.at(t.at(d)) = d;
    res += s.at(d).at(t.at(d));
    for (int i = 0; i < 26; i++) {
      if (i == t.at(d))
        continue;
      res -= c.at(i) * (d - last.at(i));
    }
  }
  return res;
}

void solve(int D, vector<int> c, vector<vector<int>> s, vector<int> t, int M,
           vector<int> d, vector<int> q) {
  for (int i = 0; i < M; i++) {
    t.at(d.at(i)) = q.at(i);
    int res = calculate(D, c, s, t);
    cout << res << endl;
  }
}

int main() {
  int D;
  cin >> D;
  vector<int> c(26);
  for (int i = 0; i < 26; i++) {
    cin >> c.at(i);
  }
  vector<vector<int>> s(D, vector<int>(26));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s.at(i).at(j);
    }
  }
  vector<int> t(D);
  for (int i = 0; i < D; i++) {
    cin >> t.at(i);
    t.at(i)--;
  }
  int M;
  cin >> M;
  vector<int> d(M);
  vector<int> q(M);
  for (int i = 0; i < M; i++) {
    cin >> d.at(i);
    cin >> q.at(i);
    d.at(i)--;
    q.at(i)--;
  }
  solve(D, move(c), move(s), move(t), M, move(d), move(q));
  return 0;
}