Problem p02575 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <vector>

#define f first
#define s second
#define ll long long
#define ld double
#define pb push_back
#define all(x) x.begin(), x.end()
#define mp make_pair
#define y0 eto
#define y1 mezhdy
#define y2 nami
#define left extermination
#define right dismemberment

using namespace std;

double start_moment = 0;
double get_runtime() { return 1.0 * clock() / CLOCKS_PER_SEC; }
void reset_timer() { start_moment = get_runtime(); }
double timer_time() { return get_runtime() - start_moment; }
void runtime() { cout << fixed << setprecision(5) << get_runtime() << '\n'; }

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <class T> void read(vector<T> &a, ll n) {
  T x;
  a.clear();
  for (ll i = 0; i < n; i++) {
    cin >> x;
    a.pb(x);
  }
}
template <class T> void write(vector<T> &a) {
  for (T x : a)
    cout << x << ' ';
  cout << '\n';
}

const int N = 200005;

int w, h;
int val[N];
set<int>::iterator it;
set<int> pos;
set<pii> st;
vector<int> to_er;

int main() {
  //    ios_base::sync_with_stdio(0);
  //    freopen("INPUT.txt","r",stdin);
  //    freopen("OUTPUT.txt", "w", stdout);

  scanf("%d %d", &h, &w);

  for (int i = 1; i <= w; i++) {
    val[i] = 1;
    pos.insert(i);
    st.insert(mp(val[i], i));
  }

  for (int j = 0; j < h; j++) {
    int a, b;
    scanf("%d %d", &a, &b);

    int mn = 1000000000;
    it = lower_bound(all(pos), a);

    to_er.clear();
    while (it != pos.end() && *it <= b) {
      to_er.pb(*it);
      it++;
    }

    for (int i = 0; i < to_er.size(); i++) {
      int vv = val[to_er[i]];
      val[to_er[i]] = -1;
      if (b != w)
        mn = min(vv + (b + 1) - to_er[i], mn);
      st.erase(mp(vv, to_er[i]));
      pos.erase(to_er[i]);
    }

    if (mn != 1000000000) {
      if (val[b + 1] == -1) {
        val[b + 1] = mn;
        st.insert(mp(val[b + 1], b + 1));
        pos.insert(b + 1);
      } else if (val[b + 1] > mn) {
        st.erase(mp(val[b + 1], b + 1));
        val[b + 1] = mn;
        st.insert(mp(val[b + 1], b + 1));
        pos.insert(b + 1);
      }
    }

    if (st.size()) {
      int res = (*st.begin()).f;
      printf("%d\n", res + j);
    } else
      printf("-1\n");
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <vector>

#define f first
#define s second
#define ll long long
#define ld double
#define pb push_back
#define all(x) x.begin(), x.end()
#define mp make_pair
#define y0 eto
#define y1 mezhdy
#define y2 nami
#define left extermination
#define right dismemberment

using namespace std;

double start_moment = 0;
double get_runtime() { return 1.0 * clock() / CLOCKS_PER_SEC; }
void reset_timer() { start_moment = get_runtime(); }
double timer_time() { return get_runtime() - start_moment; }
void runtime() { cout << fixed << setprecision(5) << get_runtime() << '\n'; }

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <class T> void read(vector<T> &a, ll n) {
  T x;
  a.clear();
  for (ll i = 0; i < n; i++) {
    cin >> x;
    a.pb(x);
  }
}
template <class T> void write(vector<T> &a) {
  for (T x : a)
    cout << x << ' ';
  cout << '\n';
}

const int N = 200005;

int w, h;
int val[N];
set<int>::iterator it;
set<int> pos;
set<pii> st;
vector<int> to_er;

int main() {
  //    ios_base::sync_with_stdio(0);
  //    freopen("INPUT.txt","r",stdin);
  //    freopen("OUTPUT.txt", "w", stdout);

  scanf("%d %d", &h, &w);

  for (int i = 1; i <= w; i++) {
    val[i] = 1;
    pos.insert(i);
    st.insert(mp(val[i], i));
  }

  for (int j = 0; j < h; j++) {
    int a, b;
    scanf("%d %d", &a, &b);

    int mn = 1000000000;
    it = pos.lower_bound(a);

    to_er.clear();
    while (it != pos.end() && *it <= b) {
      to_er.pb(*it);
      it++;
    }

    for (int i = 0; i < to_er.size(); i++) {
      int vv = val[to_er[i]];
      val[to_er[i]] = -1;
      if (b != w)
        mn = min(vv + (b + 1) - to_er[i], mn);
      st.erase(mp(vv, to_er[i]));
      pos.erase(to_er[i]);
    }

    if (mn != 1000000000) {
      if (val[b + 1] == -1) {
        val[b + 1] = mn;
        st.insert(mp(val[b + 1], b + 1));
        pos.insert(b + 1);
      } else if (val[b + 1] > mn) {
        st.erase(mp(val[b + 1], b + 1));
        val[b + 1] = mn;
        st.insert(mp(val[b + 1], b + 1));
        pos.insert(b + 1);
      }
    }

    if (st.size()) {
      int res = (*st.begin()).f;
      printf("%d\n", res + j);
    } else
      printf("-1\n");
  }
}