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

#include <algorithm>
#include <cassert>
#include <iostream>
using namespace std;

int main() {

  int N, R, L;
  int INF = 1e9;

  cin >> N >> R >> L;

  pair<int, int> score[200005];
  int time[100005];
  for (int i = 1; i < 200005; i++)
    score[i] = make_pair(INF, -1);
  int id[200005] = {};
  for (int i = 1; i <= N; i++) {
    score[i] = make_pair(0, i);
    id[i] = i;
    time[i] = 0;
  }

  int mae = 0;

  int d, t, x;
  for (int i = 0; i < R; i++) {
    cin >> d >> t >> x;

    int site1 = id[d];
    assert(site1 >= 1);

    time[score[1].second] += t - mae;
    score[site1].first -= x;
    mae = t;

    if (x > 0) {
      while (site1 > 1) {
        if (score[site1] < score[site1 / 2]) {
          swap(score[site1], score[site1 / 2]);
          id[score[site1].second] = site1;
          id[score[site1 / 2].second] = site1 / 2;
          site1 = site1 / 2;
        } else
          break;
      }
    } else {
      while (1) {
        if (score[2 * site1] < score[site1] ||
            score[2 * site1 + 1] < score[site1]) {
          int flg = 0;
          if (score[2 * site1] > score[2 * site1 + 1])
            flg = 1;
          swap(score[site1], score[2 * site1 + flg]);
          id[score[site1].second] = site1;
          id[score[2 * site1 + flg].second] = 2 * site1 + flg;
          site1 = 2 * site1 + flg;
        } else
          break;
      }
    }
    /*    for(int j=1;j<=N;j++) cout<<score[j].second;
          cout << endl;
          for(int j=1;j<=N;j++) cout <<score[j].first <<" ";
          cout << endl;
          for(int j=1;j<=N;j++) cout <<time[j] << " ";
          cout << endl;
    */
  }

  time[score[1].second] += L - mae;

  int maxtime = -1;
  int ans;
  for (int i = 1; i <= N; i++)
    if (maxtime < time[i])
      maxtime = time[i], ans = i;

  // for(int i=1;i<=N;i++) cout << time[i] <<" ";
  // cout << endl;

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
using namespace std;

int main() {

  int N, R, L;
  int INF = 1e9;

  cin >> N >> R >> L;

  pair<int, int> score[200005];
  int time[100005];
  for (int i = 1; i < 200005; i++)
    score[i] = make_pair(INF, -1);

  for (int i = 1; i <= N; i++) {
    score[i] = make_pair(0, i);
    time[i] = 0;
  }

  int mae = 0;

  int d, t, x;
  for (int i = 0; i < R; i++) {
    cin >> d >> t >> x;

    int site1 = -1;
    for (int j = 1; j <= N; j++) {
      if (score[j].second == d) {
        site1 = j;
        break;
      }
    }
    assert(site1 >= 1);

    time[score[1].second] += t - mae;
    score[site1].first -= x;
    mae = t;

    if (x > 0) {
      while (site1 > 1) {
        if (score[site1] < score[site1 / 2]) {
          swap(score[site1], score[site1 / 2]);
          site1 = site1 / 2;
        } else
          break;
      }
    } else {
      while (1) {
        if (score[2 * site1] < score[site1] ||
            score[2 * site1 + 1] < score[site1]) {
          int flg = 0;
          if (score[2 * site1] > score[2 * site1 + 1])
            flg = 1;
          swap(score[site1], score[2 * site1 + flg]);
          site1 = 2 * site1 + flg;
        } else
          break;
      }
    }
    /*    for(int j=1;j<=N;j++) cout<<score[j].second;
    cout << endl;
    for(int j=1;j<=N;j++) cout <<score[j].first <<" ";
    cout << endl;
    for(int j=1;j<=N;j++) cout <<time[j] << " ";
    cout << endl;
    */
  }

  time[score[1].second] += L - mae;
  /*
  for(int j=1;j<=N;j++) cout <<time[j] << " ";
   cout << endl;
  */

  int maxtime = -1;
  int ans;
  for (int i = 1; i <= N; i++)
    if (maxtime < time[i])
      maxtime = time[i], ans = i;

  cout << ans << endl;

  return 0;
}