Problem p02620 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
using ll = long long;

ll scoring(vector<int> &schedule);
int D;
vector<int> decline(26);
vector<vector<int>> satis(365, vector<int>(26));

void solve_greedy(vector<int> &schedule) {
  for (int d = 0; d < D; d++) {
    int sel = 0;
    for (int i = 0; i < 26; i++) {
      if (satis[d][i] > satis[d][sel]) {
        sel = i;
      }
    }
    schedule[d] = sel;
  }
}

ll scoring(vector<int> &schedule) {
  vector<int> lastHeld(26, 0);
  ll sat = 0;
  for (int d = 1; d <= D; d++) {
    int contest = schedule[d - 1];
    lastHeld[contest] = d;
    for (int i = 0; i < 26; i++) {
      if (i == contest) {
        sat += satis[d - 1][i];
      }
      sat -= decline[i] * (d - lastHeld[i]);
    }
    // cout<<sat<<endl;
  }
  return sat;
}

ll calcDiff(vector<int> &schedule, vector<int> &d, vector<int> &q, int M) {
  vector<set<int>> sched(26, set<int>());
  for (int i = 0; i < D; i++) {
    sched[schedule[i]].insert(i);
  }
  // cout<<"A"<<endl;
  for (int i = 0; i < 26; i++) {
    sched[i].insert(-1);
    sched[i].insert(D);
  }
  ll result = scoring(schedule);
  // cout<<"a"<<endl;
  for (int i = 0; i < M; i++) {
    result -= satis[d[i]][schedule[d[i]]];
    result += satis[d[i]][q[i]];
    auto lastHeldNewP = sched[q[i]].lower_bound(d[i]);
    lastHeldNewP--;
    int lastHeldNew = *lastHeldNewP;
    int nextHeldNew = *sched[q[i]].lower_bound(d[i] + 1);
    auto lastHeldOldP = (sched[schedule[d[i]]].lower_bound(d[i]));
    lastHeldOldP--;
    int lastHeldOld = *(lastHeldOldP);
    int nextHeldOld = *sched[schedule[d[i]]].lower_bound(d[i] + 1);
    result += decline[q[i]] * (nextHeldNew - d[i]) * (d[i] - lastHeldNew);
    result -=
        decline[schedule[d[i]]] * (nextHeldOld - d[i]) * (d[i] - lastHeldOld);

    // cout<<q[i]<<" " <<lastHeldNew<<" "<<nextHeldNew<<" "<<d[i]<<endl;
    // cout<<schedule[d[i]]<<" "<<lastHeldOld<<" "<<nextHeldOld<<"
    // "<<d[i]<<endl;

    sched[q[i]].insert(d[i]);
    sched[schedule[d[i]]].erase(d[i]);
    schedule[d[i]] = q[i];
    cout << result << endl;
    // cout<<scoring(schedule)<<endl;
  }
}

int main() {
  cin >> D;
  for (int i = 0; i < 26; i++) {
    int c;
    cin >> c;
    decline[i] = c;
  }
  for (int d = 0; d < D; d++) {
    for (int i = 0; i < 26; i++) {
      int s;
      cin >> s;
      satis[d][i] = s;
    }
  }
  vector<int> schedule(D, 0);
  // solve_greedy(schedule);
  // for(int i = 0;i < schedule.size();i++){
  //     cout<<schedule[i]+1<<endl;
  // }
  // scoring(schedule);
  for (int i = 0; i < D; i++) {
    cin >> schedule[i];
    schedule[i]--;
  }
  int M;
  cin >> M;
  vector<int> d(M), q(M);
  for (int i = 0; i < M; i++) {
    cin >> d[i] >> q[i];
    d[i]--;
    q[i]--;
  }
  // cout<<"A"<<endl;
  calcDiff(schedule, d, q, M);
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
using ll = long long;

ll scoring(vector<int> &schedule);
int D;
vector<int> decline(26);
vector<vector<int>> satis(365, vector<int>(26));

void solve_greedy(vector<int> &schedule) {
  for (int d = 0; d < D; d++) {
    int sel = 0;
    for (int i = 0; i < 26; i++) {
      if (satis[d][i] > satis[d][sel]) {
        sel = i;
      }
    }
    schedule[d] = sel;
  }
}

ll scoring(vector<int> &schedule) {
  vector<int> lastHeld(26, 0);
  ll sat = 0;
  for (int d = 1; d <= D; d++) {
    int contest = schedule[d - 1];
    lastHeld[contest] = d;
    for (int i = 0; i < 26; i++) {
      if (i == contest) {
        sat += satis[d - 1][i];
      }
      sat -= decline[i] * (d - lastHeld[i]);
    }
    // cout<<sat<<endl;
  }
  return sat;
}

ll calcDiff(vector<int> &schedule, vector<int> &d, vector<int> &q, int M) {
  vector<set<int>> sched(26, set<int>());
  for (int i = 0; i < D; i++) {
    sched[schedule[i]].insert(i);
  }
  // cout<<"A"<<endl;
  for (int i = 0; i < 26; i++) {
    sched[i].insert(-1);
    sched[i].insert(D);
  }
  ll result = scoring(schedule);
  // cout<<"a"<<endl;
  for (int i = 0; i < M; i++) {
    result -= satis[d[i]][schedule[d[i]]];
    result += satis[d[i]][q[i]];
    auto lastHeldNewP = sched[q[i]].lower_bound(d[i]);
    lastHeldNewP--;
    int lastHeldNew = *lastHeldNewP;
    int nextHeldNew = *sched[q[i]].lower_bound(d[i] + 1);
    auto lastHeldOldP = (sched[schedule[d[i]]].lower_bound(d[i]));
    lastHeldOldP--;
    int lastHeldOld = *(lastHeldOldP);
    int nextHeldOld = *sched[schedule[d[i]]].lower_bound(d[i] + 1);
    result += decline[q[i]] * (nextHeldNew - d[i]) * (d[i] - lastHeldNew);
    result -=
        decline[schedule[d[i]]] * (nextHeldOld - d[i]) * (d[i] - lastHeldOld);

    // cout<<q[i]<<" " <<lastHeldNew<<" "<<nextHeldNew<<" "<<d[i]<<endl;
    // cout<<schedule[d[i]]<<" "<<lastHeldOld<<" "<<nextHeldOld<<"
    // "<<d[i]<<endl;

    sched[q[i]].insert(d[i]);
    sched[schedule[d[i]]].erase(d[i]);
    schedule[d[i]] = q[i];
    cout << result << endl;
    // cout<<scoring(schedule)<<endl;
  }
  return result;
}

int main() {
  cin >> D;
  for (int i = 0; i < 26; i++) {
    int c;
    cin >> c;
    decline[i] = c;
  }
  for (int d = 0; d < D; d++) {
    for (int i = 0; i < 26; i++) {
      int s;
      cin >> s;
      satis[d][i] = s;
    }
  }
  vector<int> schedule(D, 0);
  // solve_greedy(schedule);
  // for(int i = 0;i < schedule.size();i++){
  //     cout<<schedule[i]+1<<endl;
  // }
  // scoring(schedule);
  for (int i = 0; i < D; i++) {
    cin >> schedule[i];
    schedule[i]--;
  }
  int M;
  cin >> M;
  vector<int> d(M), q(M);
  for (int i = 0; i < M; i++) {
    cin >> d[i] >> q[i];
    d[i]--;
    q[i]--;
  }
  // cout<<"A"<<endl;
  calcDiff(schedule, d, q, M);
}