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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int INF = 1e9 + 239;

namespace Solver {
int n;
vector<int> a;
multiset<int> vals;
set<pair<int, int>> S;

void init(int n_) {
  n = n_;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    a[i] = 0;
    vals.insert(a[i]);
    S.insert({i, a[i]});
  }
}

void trim(int pos) {
  if (a[pos] == -1) {
    int pnt = prev(S.lower_bound({pos, -1}))->first;
    a[pos] = a[pnt] + (pos - pnt);
    vals.insert(a[pos]);
    S.insert({pos, a[pos]});
  }
}

int get(int pos) {
  trim(pos);
  return a[pos];
}

void set(int l, int r, int v) {
  if (l > 0)
    trim(l - 1);
  if (r + 1 < n)
    trim(r + 1);
  while (true) {
    auto it = S.lower_bound({l, -1});
    if (it == S.end() || it->first > r) {
      break;
    } else {
      vals.erase(vals.find(a[it->first]));
      a[it->first] = -1;
      S.erase(it);
    }
  }
  a[l] = v;
  vals.insert(a[l]);
  S.insert({l, a[l]});
}

int mn() { return *vals.begin(); }
} // namespace Solver

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int h, w;
  cin >> h >> w;
  Solver::init(w);
  vector<int> a(h);
  vector<int> b(h);
  for (int i = 0; i < h; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  for (int i = 1; i <= h; i++) {
    int val = INF;
    if (a[i - 1] - 1 >= 0)
      val = Solver::get(a[i - 1] - 1);
    Solver::set(a[i - 1], b[i - 1], val + 1);
    if (Solver::mn() >= INF) {
      cout << -1 << '\\n';
    } else {
      cout << Solver::mn() + i << '\\n';
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int INF = 1e9 + 239;

namespace Solver {
int n;
vector<int> a;

void init(int n_) {
  n = n_;
  a.resize(n);
}

void trim(int pos) {
  int pnt = pos;
  while (a[pnt] == -1) {
    pnt--;
  }
  a[pos] = a[pnt] + (pos - pnt);
}

int get(int pos) {
  trim(pos);
  return a[pos];
}

void set(int l, int r, int v) {
  if (l > 0)
    trim(l - 1);
  if (r + 1 < n)
    trim(r + 1);
  a[l] = v;
  for (int i = l + 1; i <= r; i++) {
    a[i] = -1;
  }
}

int mn() {
  int ans = INF;
  for (int i = 0; i < n; i++) {
    if (a[i] != -1)
      ans = min(ans, a[i]);
  }
  return ans;
}
} // namespace Solver

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int h, w;
  cin >> h >> w;
  Solver::init(w);
  vector<int> a(h);
  vector<int> b(h);
  for (int i = 0; i < h; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  for (int i = 1; i <= h; i++) {
    int val = INF;
    if (a[i - 1] - 1 >= 0)
      val = Solver::get(a[i - 1] - 1);
    Solver::set(a[i - 1], b[i - 1], val + 1);
    if (Solver::mn() >= INF) {
      cout << -1 << '\\n';
    } else {
      cout << Solver::mn() + i << '\\n';
    }
  }
}