Problem p03295 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
const int mod = 1000000007, MAX = 1 << 18, INF = 1 << 30;

bool compare(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  if (a.first.second == b.first.second)
    return a.first.first > b.first.first;
  return a.first.second > b.first.second;
}

int main() {

  int N, M;
  cin >> N >> M;
  vector<int> dp(N + 1, INF);
  vector<pair<pair<int, int>, int>> P(M);
  for (int i = 0; i < M; i++) {
    cin >> P[i].first.first >> P[i].first.second;
    P[i].second = i + 1;
  }

  sort(all(P), compare);

  for (int i = 0; i < N; i++) {
    auto it = upper_bound(all(dp), -P[i].first.second);
    *it = min(*it, -P[i].first.first);
  }
  int ans = 0;

  for (int i = 0; i < N; i++) {
    if (dp[i] == INF) {
      ans = i;
      break;
    } else {
      dp[i] = -dp[i];
    }
  }

  reverse(all(P));

  // for(int i=0;i<N;i++) cout<<dp[i]<<endl;

  cout << ans << endl;

  /*if(ans==1){
      cout<<1<<endl;
      return 0;
  }
  int nowsee=0,start=-1;
  priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>>
  PQ;

  bool room=false;

  for(int i=ans-2;i>=-1;i--){
      if(room) cout<<" ";
      if(i==-1){
          while(nowsee<N){
              PQ.push({P[nowsee].second,{P[nowsee].first.first,P[nowsee].first.second}});
              nowsee++;
          }
          if(PQ.empty()) return 1;
          while(1){
              pair<int,pair<int,int>> p=PQ.top();PQ.pop();
              if(p.second.first>=start){
                  cout<<p.first;
                  room=true;
                  start=p.second.second;
                  break;
              }
          }
      }else{
          while(nowsee<N&&P[nowsee].first.second<=dp[i]){
              PQ.push({P[nowsee].second,{P[nowsee].first.first,P[nowsee].first.second}});
              nowsee++;
          }
          if(PQ.empty()) return 1;
          while(1){
              pair<int,pair<int,int>> p=PQ.top();PQ.pop();
              if(p.second.first>=start){
                  cout<<p.first;
                  room=true;
                  start=p.second.second;
                  break;
              }
          }
      }
  }
  cout<<endl;*/
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
const int mod = 1000000007, MAX = 1 << 18, INF = 1 << 30;

bool compare(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  if (a.first.second == b.first.second)
    return a.first.first > b.first.first;
  return a.first.second > b.first.second;
}

int main() {

  int N, M;
  cin >> N >> M;
  vector<int> dp(N + 1, INF);
  vector<pair<pair<int, int>, int>> P(M);
  for (int i = 0; i < M; i++) {
    cin >> P[i].first.first >> P[i].first.second;
    P[i].second = i + 1;
  }

  sort(all(P), compare);

  for (int i = 0; i < M; i++) {
    auto it = upper_bound(all(dp), -P[i].first.second);
    *it = min(*it, -P[i].first.first);
  }
  int ans = 0;

  for (int i = 0; i < N; i++) {
    if (dp[i] == INF) {
      ans = i;
      break;
    } else {
      dp[i] = -dp[i];
    }
  }

  reverse(all(P));

  // for(int i=0;i<N;i++) cout<<dp[i]<<endl;

  cout << ans << endl;

  /*if(ans==1){
      cout<<1<<endl;
      return 0;
  }
  int nowsee=0,start=-1;
  priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>>
  PQ;

  bool room=false;

  for(int i=ans-2;i>=-1;i--){
      if(room) cout<<" ";
      if(i==-1){
          while(nowsee<N){
              PQ.push({P[nowsee].second,{P[nowsee].first.first,P[nowsee].first.second}});
              nowsee++;
          }
          if(PQ.empty()) return 1;
          while(1){
              pair<int,pair<int,int>> p=PQ.top();PQ.pop();
              if(p.second.first>=start){
                  cout<<p.first;
                  room=true;
                  start=p.second.second;
                  break;
              }
          }
      }else{
          while(nowsee<N&&P[nowsee].first.second<=dp[i]){
              PQ.push({P[nowsee].second,{P[nowsee].first.first,P[nowsee].first.second}});
              nowsee++;
          }
          if(PQ.empty()) return 1;
          while(1){
              pair<int,pair<int,int>> p=PQ.top();PQ.pop();
              if(p.second.first>=start){
                  cout<<p.first;
                  room=true;
                  start=p.second.second;
                  break;
              }
          }
      }
  }
  cout<<endl;*/
}