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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct P {
  int id, score;
  P(int id, int score) : id(id), score(score) {}
};

bool operator<(const P &p1, const P &p2) {
  if (p1.score == p2.score)
    return p1.id > p2.id;
  return p1.score < p2.score;
}
bool operator>(const P &p1, const P &p2) {
  if (p1.score == p2.score)
    return p1.id < p2.id;
  return p1.score > p2.score;
}

struct Data {
  int d, t, x;
  Data(int d, int t, int x) : d(d), t(t), x(x) {}
};

bool compare(const Data &d1, const Data &d2) { return d1.t < d2.t; }

int main() {
  vector<Data> v;

  int N, R, L;
  cin >> N >> R >> L;
  for (int i = 0; i < R; ++i) {
    int d, t, x;
    cin >> d >> t >> x;
    v.push_back(Data(d - 1, t, x));
  }
  sort(v.begin(), v.end(), compare);

  priority_queue<P> Q;
  for (int i = 0; i < N; ++i) {
    Q.push(P(i, 0));
  }

  vector<int> score(N, 0);
  vector<int> broadTime(N, 0);

  int bt = 0;
  int maxNo = 0;
  for (int i = 0; i < v.size(); ++i) {
    int t = v[i].t;
    int pos = v[i].d; // テ」ツδ?」ツδシテ」ツδ?ァツ閉ェテ・ツ渉キ
    int x = v[i].x; // テ・ツ環?ァツつケ or テヲツクツ崚ァツつケ テ・ツ按?

    P top = Q.top();

    // テ・ツ環?ァツョツ?
    broadTime[top.id] += t - bt;
    score[pos] += x;
    if (x > 0) { // テ・ツ環?ァツつケ
      if (pos != top.id) {
        P newData = P(pos, score[pos]);
        Q.push(newData);
        //				cout << ":" << newData.id << " " <<
        //newData.score << endl;
      }
    }

    while (1) {
      P Top = Q.top();
      if (Top.score == score[Top.id])
        break;

      Q.pop();
      Q.push(P(Top.id, score[Top.id]));
    }
    /*
                    queue<P> Q2;
                    while ( !Q.empty() ) {
                            P data = Q.top(); Q.pop(); Q2.push(data);
                            cout << ":" << data.id << " " << data.score << endl;
                    }
                    while ( !Q2.empty() ) {
                            P data = Q2.front(); Q2.pop(); Q.push(data);
                    }
                    cout << endl;
    */
    //		cout << Q.top().score << endl;

    //		cout << ":" << top.id << endl;

    bt = t;
  }
  broadTime[Q.top().id] += L - bt;

  int ansNo = 0, ansT = -1;
  for (int i = 0; i < broadTime.size(); ++i) {
    if (broadTime[i] > ansT) {
      ansT = broadTime[i];
      ansNo = i;
    }
    //		cout << ":" << broadTime[i] << endl;
  }
  cout << (ansNo + 1) << endl;

  for (int i = 0; i < score.size(); ++i) {
    //		cout << score[i] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct P {
  int id, score;
  P(int id, int score) : id(id), score(score) {}
};

bool operator<(const P &p1, const P &p2) {
  if (p1.score == p2.score)
    return p1.id > p2.id;
  return p1.score < p2.score;
}
bool operator>(const P &p1, const P &p2) {
  if (p1.score == p2.score)
    return p1.id < p2.id;
  return p1.score > p2.score;
}

struct Data {
  int d, t, x;
  Data(int d, int t, int x) : d(d), t(t), x(x) {}
};

bool compare(const Data &d1, const Data &d2) { return d1.t < d2.t; }

int main() {
  vector<Data> v;

  int N, R, L;
  cin >> N >> R >> L;
  for (int i = 0; i < R; ++i) {
    int d, t, x;
    cin >> d >> t >> x;
    v.push_back(Data(d - 1, t, x));
  }
  sort(v.begin(), v.end(), compare);

  priority_queue<P> Q;
  for (int i = 0; i < N; ++i) {
    Q.push(P(i, 0));
  }

  vector<int> score(N, 0);
  vector<int> broadTime(N, 0);

  int bt = 0;
  int maxNo = 0;
  for (int i = 0; i < v.size(); ++i) {
    int t = v[i].t;
    int pos = v[i].d; // テ」ツδ?」ツδシテ」ツδ?ァツ閉ェテ・ツ渉キ
    int x = v[i].x; // テ・ツ環?ァツつケ or テヲツクツ崚ァツつケ テ・ツ按?

    P top = Q.top();

    // テ・ツ環?ァツョツ?
    broadTime[top.id] += t - bt;
    score[pos] += x;
    if (x > 0) { // テ・ツ環?ァツつケ
      if (pos != top.id) {
        queue<P> Q2;
        while (1) {
          P data = Q.top();
          Q.pop();
          if (data.id == pos) {
            data.score = score[pos];
            Q.push(data);
            break;
          }
          Q2.push(data);
        }
        while (!Q2.empty()) {
          Q.push(Q2.front());
          Q2.pop();
        }
      }
    }

    while (1) {
      P Top = Q.top();
      if (Top.score == score[Top.id])
        break;

      Q.pop();
      Q.push(P(Top.id, score[Top.id]));
    }

    //		cout << ":" << top.id << endl;

    bt = t;
  }
  broadTime[Q.top().id] += L - bt;

  int ansNo = 0, ansT = -1;
  for (int i = 0; i < broadTime.size(); ++i) {
    if (broadTime[i] > ansT) {
      ansT = broadTime[i];
      ansNo = i;
    }
    //		cout << ":" << broadTime[i] << endl;
  }
  cout << (ansNo + 1) << endl;

  for (int i = 0; i < score.size(); ++i) {
    //		cout << score[i] << endl;
  }
}