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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int D;
  cin >> D;
  vector<int> c(26, 0);
  rep(i, 26) { cin >> c[i]; } // end rep
  vector<vector<int>> s(D, vector<int>(26, 0));
  rep(i, D) {
    rep(j, 26) { cin >> s[i][j]; } // end rep
  }                                // end rep

  vector<int> t(D + 1, 0);
  rep(i, D) {
    cin >> t[i + 1];
    t[i + 1]--;
  } // end rep

  vector<set<int>> st(26);
  rep(i, 26) {
    st[i].insert(0);
    st[i].insert(D + 1);
  } // end rep
  vector<int> sat(D + 1, 0);
  vector<int> last(26, 0);

  for (int d = 1; d <= D; d++) {
    st[t[d]].insert(d);
    sat[d] = sat[d - 1] + s[d - 1][t[d]];
    last[t[d]] = d;
    rep(j, 26) { sat[d] -= (d - last[j]) * c[j]; } // end rep
  }                                                // end for

  int M;
  cin >> M;
  vector<int> ans(M + 1, 0);
  ans[0] = sat[D];

  rep(i, M) {
    int d, q;
    cin >> d >> q;
    q--;
    int p = t[d];
    st[p].erase(d);

    auto itrp = st[p].lower_bound(d), itrq = st[q].lower_bound(d);
    int rp = *itrp, rq = *itrq;
    itrp--;
    itrq--;
    int lp = *itrp, lq = *itrq;

    int diff = s[d - 1][q] - s[d - 1][p];
    diff -= (d - lp) * (rp - d) * c[p];
    diff += (d - lq) * (rq - d) * c[q];
    ans[i + 1] = ans[i] + diff;

    t[d] = q;
    st[q].insert(d);
  } // end rep

  for (int i = 1; i <= M; i++) {
    cout << ans[i] << endl;
  } // end for

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

bool last[365][26];

int calcScoreIndiviual(int j, int D, vector<int> c, vector<vector<int>> s) {
  int sat = 0;
  int last_indiviual = 0;
  rep(d, D) {
    if (last[d][j]) {
      last_indiviual = d + 1;
      sat += s[d][j];
    } else {
      sat -= (d + 1 - last_indiviual) * c[j];
    } // end rep
  }   // end rep
  return sat;
}

int main() {
  memset(last, false, sizeof(last));

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int D;
  cin >> D;
  vector<int> c(26, 0);
  rep(i, 26) { cin >> c[i]; } // end rep
  vector<vector<int>> s(D, vector<int>(26, 0));
  rep(i, D) {
    rep(j, 26) { cin >> s[i][j]; } // end rep
  }                                // end rep

  vector<int> t(D, 0);
  rep(i, D) {
    cin >> t[i];
    t[i]--;
    last[i][t[i]] = true;
  } // end rep

  int score = 0;
  rep(j, 26) { score += calcScoreIndiviual(j, D, c, s); }
  //	cout << score << endl;

  int M;
  cin >> M;
  rep(i, M) {
    int d_in, q_in;
    cin >> d_in >> q_in;
    d_in--;
    q_in--;
    int prevIndex = -1;
    rep(j, 26) {
      if (last[d_in][j]) {
        prevIndex = j;
        break;
      } // end if
    }   // end rep
    score -= calcScoreIndiviual(prevIndex, D, c, s);
    score -= calcScoreIndiviual(q_in, D, c, s);
    last[d_in][prevIndex] = false;
    last[d_in][q_in] = true;
    score += calcScoreIndiviual(prevIndex, D, c, s);
    score += calcScoreIndiviual(q_in, D, c, s);
    cout << score << endl;
  } // end rep

  return 0;
}