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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll allscore(int i, int d, vector<vector<int>> s, vector<vector<int>> t,
            vector<int> c) {
  ll res = 0;
  int fury = 0;
  for (int j = 0; j < d; j++) {
    if (t.at(i).at(j) == 1) {
      res += s.at(j).at(i);
      fury = 0;
    } else {
      fury++;
      res -= fury * c.at(i);
    }
  }
  return res;
}
ll fastscore(vector<int> bl, int date, int fury) {
  int l = -1;
  int r = bl.size();
  for (int i = date - 1; i >= 0; i--) {
    if (bl.at(i) == 1) {
      l = i;
      break;
    }
  }
  for (int i = date + 1; i < r; i++) {
    if (bl.at(i) == 1) {
      r = i;
      break;
    }
  }
  ll ans = (r - date) * (date - l) * fury;
  return ans;
}
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> td(d);
  vector<vector<int>> t(26, vector<int>(d, 0));
  for (int i = 0; i < d; i++) {
    int x;
    cin >> x;
    x--;
    td.at(i) = x;
    t.at(x).at(i)++;
  }

  int m;
  cin >> m;
  vector<pair<int, int>> dq(m);
  for (int i = 0; i < m; i++) {
    cin >> dq.at(i).first >> dq.at(i).second;
    dq.at(i).first--;
    dq.at(i).second--;
  }

  // Q.B
  vector<int> sum(26, 0);
  for (int i = 0; i < 26; i++) {
    sum.at(i) = allscore(i, d, s, t, c);
  }
  ll ans = 0;
  for (int i = 0; i < 26; i++)
    ans += sum.at(i);
  // Q.C
  for (int i = 0; i < m; i++) {
    int day = dq.at(i).first;
    int change = dq.at(i).second;
    int before = td.at(day);
    // 変更前と変更後について再計算、sumに反映
    t.at(before).at(day) = 0;
    t.at(change).at(day) = 1;
    td.at(day) = change;
    ll befs = fastscore(t.at(before), day, c.at(before)) + s.at(day).at(before);
    ll chgs = fastscore(t.at(change), day, c.at(change)) + s.at(day).at(change);
    ans = ans - befs + chgs;
    sum.at(before) -= befs;
    sum.at(change) += chgs;
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll score(int i, int d, vector<vector<int>> s, vector<vector<int>> t,
         vector<int> c) {
  ll res = 0;
  int fury = 0;
  for (int j = 0; j < d; j++) {
    if (t.at(i).at(j) == 1) {
      res += s.at(j).at(i);
      fury = 0;
    } else {
      fury++;
      res -= fury * c.at(i);
    }
  }
  return res;
}

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> td(d);
  vector<vector<int>> t(26, vector<int>(d, 0));
  for (int i = 0; i < d; i++) {
    int x;
    cin >> x;
    x--;
    td.at(i) = x;
    t.at(x).at(i)++;
  }

  int m;
  cin >> m;
  vector<pair<int, int>> dq(m);
  for (int i = 0; i < m; i++) {
    cin >> dq.at(i).first >> dq.at(i).second;
    dq.at(i).first--;
    dq.at(i).second--;
  }

  // Q.B
  vector<int> sum(26, 0);
  for (int i = 0; i < 26; i++) {
    sum.at(i) = score(i, d, s, t, c);
  }
  ll ans = 0;
  for (int i = 0; i < 26; i++)
    ans += sum.at(i);
  // Q.C
  for (int i = 0; i < m; i++) {
    int day = dq.at(i).first;
    int change = dq.at(i).second;
    int before = td.at(day);
    // 変更前と変更後について再計算、sumに反映
    t.at(before).at(day) = 0;
    t.at(change).at(day) = 1;
    td.at(day) = change;
    ll befs = score(before, d, s, t, c);
    ll chgs = score(change, d, s, t, c);
    ans = ans - sum.at(before) - sum.at(change) + befs + chgs;
    sum.at(before) = befs;
    sum.at(change) = chgs;
    cout << ans << endl;
  }
}