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

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

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

static const double EPS = 1e-8;

static const int tx[] = {0, 1, 0, -1};
static const int ty[] = {-1, 0, 1, 0};

int main() {
  int total_students;
  int total_queries;
  while (~scanf("%d %d", &total_students, &total_queries)) {
    vector<int> student_scores;
    vector<int> sorted_student_scores;
    for (int student_i = 0; student_i < total_students; student_i++) {
      int score;
      scanf("%d", &score);
      student_scores.push_back(score);
      sorted_student_scores.push_back(score);
      ;
    }

    sort(sorted_student_scores.begin(), sorted_student_scores.end());

    set<int> leaders;
    for (int query_i = 0; query_i < total_queries; query_i++) {
      string operation;
      int num;
      cin >> operation >> num;
      if (operation == "ADD") {
        leaders.insert(num - 1);
      } else if (operation == "REMOVE") {
        leaders.erase(lower_bound(leaders.begin(), leaders.end(), num - 1));
      } else if (operation == "CHECK") {
        vector<int> leader_scores;

        for (set<int>::iterator leader_it = leaders.begin();
             leader_it != leaders.end(); leader_it++) {
          leader_scores.push_back(student_scores[*leader_it]);
        }
        sort(leader_scores.begin(), leader_scores.end());

        int rhs = INF;
        int lhs = 0;

        for (int round = 0; round < 50; round++) {
          int mid = lhs + (rhs - lhs) / 2;
          int prev = 0;
          int fail_sum = 0;
          for (int leader_i = 0; leader_i < leader_scores.size(); leader_i++) {
            int score = leader_scores[leader_i];
            int current =
                lower_bound(sorted_student_scores.begin(),
                            sorted_student_scores.end(), score - mid) -
                sorted_student_scores.begin();
            int fail = max(0, current - prev);
            fail_sum += fail;
            prev = upper_bound(sorted_student_scores.begin(),
                               sorted_student_scores.end(), score) -
                   sorted_student_scores.begin();
          }
          fail_sum += max(total_students - prev, 0);

          if (fail_sum > num) {
            lhs = mid;
          } else {
            rhs = mid;
          }
        }

        if (rhs < INF) {
          printf("%d\\n", rhs);
        } else {
          printf("NA\\n");
        }
      }
    }
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

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

static const double EPS = 1e-8;

static const int tx[] = {0, 1, 0, -1};
static const int ty[] = {-1, 0, 1, 0};

int main() {
  int total_students;
  int total_queries;
  while (~scanf("%d %d", &total_students, &total_queries)) {
    vector<int> scores;
    for (int student_i = 0; student_i < total_students; student_i++) {
      int score;
      scanf("%d", &score);
      scores.push_back(score);
    }
    vector<int> leaders;
    for (int query_i = 0; query_i < total_queries; query_i++) {
      string operation;
      int num;
      cin >> operation >> num;
      if (operation == "ADD") {
        leaders.push_back(num - 1);
        sort(leaders.begin(), leaders.end());
      } else if (operation == "REMOVE") {
        leaders.erase(lower_bound(leaders.begin(), leaders.end(), num - 1));
        sort(leaders.begin(), leaders.end());
      } else if (operation == "CHECK") {
        vector<int> leader_scores;

        for (int leader_i = 0; leader_i < leaders.size(); leader_i++) {
          leader_scores.push_back(scores[leaders[leader_i]]);
        }
        sort(leader_scores.begin(), leader_scores.end());

        priority_queue<int> diff_log;
        for (int score_i = 0; score_i < scores.size(); score_i++) {
          if (!binary_search(leaders.begin(), leaders.end(), score_i)) {
            int score = scores[score_i];
            int pos =
                lower_bound(leader_scores.begin(), leader_scores.end(), score) -
                leader_scores.begin();
            if (pos == leader_scores.size()) {
              diff_log.push(INF);
            } else if (pos < leader_scores.size()) {
              int diff = leader_scores[pos] - score;
              if (diff >= 0) {
                diff_log.push(diff);
              } else {
                diff_log.push(INF);
              }
            }
          }
        }
        while (num > 0 && !diff_log.empty()) {
          diff_log.pop();
          num--;
        }
        if (!diff_log.empty()) {
          if (diff_log.top() == INF) {
            printf("NA\\n");
          } else {
            printf("%d\\n", diff_log.top());
          }
        } else {
          printf("%d\\n", 0);
        }
      }
    }
  }
}