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

#include <bits/stdc++.h>
using namespace std;

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const int maxn = 1e6 + 5;
const int inf = 1e9;

int n, m;
int L[maxn], R[maxn];

bool isect(int l, int r, int a, int b) { return max(l, a) <= min(r, b); }

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

  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> L[i] >> R[i];
  }

  map<int, int> a;
  multiset<int> ms;
  for (int i = 1; i <= m; i++) {
    a[i] = 0;
    ms.insert(0);
  }
  a[0] = inf;
  ms.insert(inf);
  a[m + 1] = inf;
  ms.insert(inf);

  auto print = [&]() {
    for (auto p : a) {
      cout << p.first << ": " << p.second << endl;
    }
    cout << endl;
  };
  // print();

  assert(ms.size() == a.size());
  // a[i]: min # of left moves to get to column i

  auto update = [&](int l, int r) {
    vector<pair<int, int>> upds;
    auto iter = std::prev(a.lower_bound(l));
    set<int> rm;

    // update l
    upds.push_back({l, (l - iter->first) + iter->second});
    if (a.count(l))
      rm.insert(l);

    // update r
    if (r + 1 <= m) {
      auto iter = std::prev(a.lower_bound(r + 1));
      int res = r + 1 - iter->first + iter->second;
      if (a.count(r + 1)) {
        rm.insert(r + 1);
        res = min(res, a[r + 1]);
      }
      upds.push_back({r + 1, res});
    }

    iter = a.lower_bound(l);
    while (iter != a.end() && iter->first <= r) {
      rm.insert(iter->first);
      ++iter;
    }

    for (int k : rm) {
      ms.erase(ms.find(a[k]));
    }

    iter = a.lower_bound(l);
    while (iter != a.end() && iter->first <= r) {
      iter = a.erase(iter);
    }

    for (auto p : upds) {
      a[p.first] = p.second;
      ms.insert(p.second);
    }

    // cout<<l<<" "<<r<<endl;
    // watch(ms.size());
    // watch(a.size());

    assert(ms.size() == a.size());
  };

  function<int()> qry = [&]() {
    assert(!ms.empty());
    return *ms.begin();
    // int res = inf;
    // for (auto p: a) {
    //     res = min(res, p.second);
    // }
    // return res;
  };

  for (int i = 1; i <= n; i++) {
    int l = L[i];
    int r = R[i];
    update(l, r);
    int res = qry() + i;
    if (res > inf / 2)
      res = -1;
    cout << res << "\\n";
    // cout<<l<<" "<<r<<endl;
    // print();
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const int maxn = 1e6 + 5;
const int inf = 1e9;

int n, m;
int L[maxn], R[maxn];

bool isect(int l, int r, int a, int b) { return max(l, a) <= min(r, b); }

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

  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> L[i] >> R[i];
  }

  map<int, int> a;
  for (int i = 1; i <= m; i++) {
    a[i] = 0;
  }
  a[0] = inf;
  a[m + 1] = inf;

  auto print = [&]() {
    for (auto p : a) {
      cout << p.first << ": " << p.second << endl;
    }
    cout << endl;
  };
  // print();

  // a[i]: min # of left moves to get to column i

  auto update = [&](int l, int r) {
    vector<pair<int, int>> upds;
    auto iter = std::prev(a.lower_bound(l));

    // update l
    upds.push_back({l, (l - iter->first) + iter->second});

    // update r
    if (r + 1 <= m) {
      auto iter = std::prev(a.lower_bound(r + 1));
      int res = r + 1 - iter->first + iter->second;
      if (a.count(r + 1)) {
        res = min(res, a[r + 1]);
      }
      upds.push_back({r + 1, res});
    }

    iter = a.lower_bound(l);
    while (iter != a.end() && iter->first <= r) {
      iter = a.erase(iter);
    }

    for (auto p : upds) {
      a[p.first] = p.second;
    }
  };

  function<int()> qry = [&]() {
    int res = inf;
    for (auto p : a) {
      res = min(res, p.second);
    }
    return res;
  };

  for (int i = 1; i <= n; i++) {
    int l = L[i];
    int r = R[i];
    update(l, r);
    int res = qry() + i;
    if (res > inf / 2)
      res = -1;
    cout << res << "\\n";
    // cout<<l<<" "<<r<<endl;
    // print();
  }

  return 0;
}