alexjercan/bugnet huggingface dataset:
Problem p02575 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()

typedef long long ll;

const int N = 3e5;
const ll INF = 1e9;
int l[N], r[N];

struct SegmentTree {
  ll t[4 * N], add[4 * N];

  void change(int v, ll val) {
    val = min(val, INF);
    t[v] = val;
    add[v] = val;
  }

  void push(int v, int tl, int tm, int tr) {
    if (add[v] != 0) {
      change(v * 2, add[v]);
      change(v * 2 + 1, add[v] + tm + 1 - tl);
      add[v] = 0;
    }
  }

  ll get(int v, int tl, int tr, int pos) {
    if (tl == tr) {
      return t[v];
    }
    int tm = (tl + tr) >> 1;
    push(v, tl, tm, tr);
    if (pos <= tm)
      return get(v * 2, tl, tm, pos);
    else
      return get(v * 2 + 1, tm + 1, tr, pos);
  }

  void update(int v, int tl, int tr, int l, int r, ll val, int myl, int myr) {
    if (l > r) {
      return;
    }
    if (tl == l && tr == r) {
      change(v, val + (tl - myl));
      return;
    }
    int tm = (tl + tr) >> 1;
    push(v, tl, tm, tr);
    update(v * 2, tl, tm, l, min(r, tm), val, myl, myr);
    update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, val, myl, myr);
    t[v] = min(t[v * 2], t[v * 2 + 1]);
  }
} st;

void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> l[i] >> r[i];
    st.update(1, 1, m, l[i], r[i], INF, l[i], r[i]);
    if (1 <= l[i] - 1) {
      ll v = st.get(1, 1, m, l[i] - 1);
      st.update(1, 1, m, l[i], r[i], v + 1, l[i], r[i]);
    }
    ll v = st.t[1];
    if (v != INF)
      cout << i + v << "\\n";
    else
      cout << "-1\\n";
  }
}

// CHECK LIMITS (n <= 10^5)
// CHECK CORNER CASES (n == 1)

int main() {
  ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
  cout.setf(ios::fixed), cout.precision(20);
  // cout << 1.0 * clock() / CLOCKS_PER_SEC << endl;
  solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()

typedef long long ll;

const int N = 3e5;

int n, m;
int l[N], r[N], ans[N];

void solver(int y) {
  for (int i = 1; i <= n; i++) {
    if (l[i] <= y && y <= r[i]) {
      y = r[i] + 1;
    }
    if (y <= m) {
      ans[i] = min(i + y - 1, ans[i]);
    }
  }
}

void solve() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    ans[i] = 1e9;
    cin >> l[i] >> r[i];
  }
  for (int i = 1; i <= m; i++) {
    solver(i);
  }
  for (int i = 1; i <= n; i++) {
    cout << (ans[i] == 1e9 ? -1 : ans[i]) << "\\n";
  }
}

// CHECK LIMITS (n <= 10^5)
// CHECK CORNER CASES (n == 1)

int main() {
  ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
  cout.setf(ios::fixed), cout.precision(20);
  // cout << 1.0 * clock() / CLOCKS_PER_SEC << endl;
  solve();
}
Problem p02575 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ls (p << 1)
#define rs (p << 1 | 1)
#define mid (l + r) / 2
#define rep(i, l, r) for (int i = l; i <= r; ++i)
typedef long long ll;
const int N = 80000000 + 5;
struct tree {
  int min, tag;
} t[N];
int n, m, a, b, fir;
int read() {
  char c;
  int x = 0, f = 1;
  c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * f;
}
void lazy(int p, int k) { t[p].min = t[p].tag = k; }
void down(int p, int l, int r) {
  if (!t[p].tag)
    return;
  lazy(ls, t[p].tag), lazy(rs, t[p].tag + mid - l + 1);
  t[p].tag = 0;
}
void update(int p, int l, int r, int x, int y, int k) {
  down(p, l, r);
  if (l >= x && r <= y) {
    lazy(p, k);
    return;
  }
  if (mid >= x)
    update(ls, l, mid, x, y, k);
  if (mid < y)
    update(rs, mid + 1, r, x, y, k + max(0, mid - max(l, x) + 1));
  t[p].min = min(t[ls].min, t[rs].min);
}
int query(int p, int l, int r, int x, int y) {
  if (l >= x && r <= y)
    return t[p].min;
  down(p, l, r);
  if (mid >= x)
    return query(ls, l, mid, x, y);
  else
    return query(rs, mid + 1, r, x, y);
}
int main() {
  n = read(), m = read();
  rep(i, 1, n) {
    a = read(), b = read();
    fir = (a == 1 ? m + 1 : query(1, 1, m, a - 1, a - 1) + 1);
    update(1, 1, m, a, b, fir);
    printf("%d\\n", t[1].min >= m + 1 ? -1 : t[1].min + i);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ls (p << 1)
#define rs (p << 1 | 1)
#define mid (l + r) / 2
#define rep(i, l, r) for (int i = l; i <= r; ++i)
typedef long long ll;
const int N = 800000 + 5;
struct tree {
  int min, tag;
} t[N];
int n, m, a, b, fir;
int read() {
  char c;
  int x = 0, f = 1;
  c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * f;
}
void lazy(int p, int k) { t[p].min = t[p].tag = k; }
void down(int p, int l, int r) {
  if (!t[p].tag)
    return;
  lazy(ls, t[p].tag), lazy(rs, t[p].tag + mid - l + 1);
  t[p].tag = 0;
}
void update(int p, int l, int r, int x, int y, int k) {
  down(p, l, r);
  if (l >= x && r <= y) {
    lazy(p, k);
    return;
  }
  if (mid >= x)
    update(ls, l, mid, x, y, k);
  if (mid < y)
    update(rs, mid + 1, r, x, y, k + max(0, mid - max(l, x) + 1));
  t[p].min = min(t[ls].min, t[rs].min);
}
int query(int p, int l, int r, int x, int y) {
  if (l >= x && r <= y)
    return t[p].min;
  down(p, l, r);
  if (mid >= x)
    return query(ls, l, mid, x, y);
  else
    return query(rs, mid + 1, r, x, y);
}
int main() {
  n = read(), m = read();
  rep(i, 1, n) {
    a = read(), b = read();
    fir = (a == 1 ? m + 1 : query(1, 1, m, a - 1, a - 1) + 1);
    update(1, 1, m, a, b, fir);
    printf("%d\\n", t[1].min >= m + 1 ? -1 : t[1].min + i);
  }
  return 0;
}
Problem p02575 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int INF = 1 << 30;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int H, W;
  cin >> H >> W;
  vector<int> A(H), B(H);
  for (int i = 0; i < H; i++) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
  }

  map<int, int> path;
  multiset<int> dist;
  for (int i = 0; i < W; i++) {
    path[i] = i;
    dist.insert(0);
  }

  for (int i = 0; i < H; i++) {
    int idx = -1;
    for (auto itr = path.lower_bound(A[i]);
         itr != path.end() && itr->first <= B[i] + 1; itr = path.erase(itr)) {
      chmax(idx, itr->second);
      auto ditr = dist.find(itr->first - itr->second);
      dist.erase(ditr);
    }
    if (idx != -1 && B[i] + 1 < W) {
      path[B[i] + 1] = idx;
      dist.insert(B[i] + 1 - idx);
    }
    if (dist.size() == 0) {
      cout << "-1\\n";
    } else {
      cout << *dist.begin() + i + 1 << '\\n';
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

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

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int H, W;
  cin >> H >> W;
  vector<int> A(H), B(H);
  for (int i = 0; i < H; i++) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
  }

  vector<int> ans(H, H + W + 5);
  for (int i = 0; i < W; i++) {
    int current = i;
    int cnt = 0;
    for (int i = 0; i < H; i++) {
      if (current >= W)
        break;
      if (current < A[i] || B[i] < current)
        chmin(ans[i], ++cnt);
      else {
        cnt += B[i] - current + 1;
        current = B[i] + 1;
        if (current < W)
          chmin(ans[i], ++cnt);
      }
    }
  }

  for (auto num : ans) {
    if (num == H + W + 5)
      cout << "-1\\n";
    else
      cout << num << '\\n';
  }

  return 0;
}
Problem p02575 (C++) — buggy submission, verdict: Runtime Error

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

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

ll gcd(ll a, ll b) {
  for (; b; a %= b, swap(a, b))
    ;
  return a;
}

const int INF = 987654321;

int h, w;
int a[200001];
int b[200001];

set<pii> st;
multiset<int> ms;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  cin >> h >> w;
  for (int i = 0; i < h; i++)
    cin >> a[i] >> b[i];

  for (int i = 1; i <= w; i++) {
    st.insert({i, i});
    ms.insert(0);
  }

  for (int i = 0; i < h; i++) {
    while (true) {
      auto it = st.lower_bound({a[i], 0});
      if (it == st.end())
        break;
      if (it->first > b[i])
        break;

      int s = it->second, e = it->first;

      st.erase(it);
      ms.erase(ms.find(e - s));

      if (b[i] != w) {
        st.insert({b[i] + 1, s});
        ms.insert(b[i] + 1 - s);

        while (true) {
          auto it = st.lower_bound({b[i] + 1, 0});
          auto jt = next(it);
          if (jt == st.end() || jt->first != b[i] + 1)
            break;

          int s = it->second, e = it->first;

          st.erase(it);
          ms.erase(ms.find(e - s));
        }
      }
    }

    if (ms.empty())
      cout << "-1\\n";
    else
      cout << (*ms.begin()) + i + 1 << '\\n';
  }
}

Error: TLE

Fixed submission:

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

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

ll gcd(ll a, ll b) {
  for (; b; a %= b, swap(a, b))
    ;
  return a;
}

const int INF = 987654321;

int h, w;
int a[200001];
int b[200001];

set<pii> st;
multiset<int> ms;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  cin >> h >> w;
  for (int i = 0; i < h; i++)
    cin >> a[i] >> b[i];

  for (int i = 1; i <= w; i++) {
    st.insert({i, i});
    ms.insert(0);
  }

  for (int i = 0; i < h; i++) {
    while (true) {
      auto it = st.lower_bound({a[i], 0});
      if (it == st.end())
        break;
      if (it->first > b[i])
        break;

      int s = it->second, e = it->first;

      st.erase(it);
      ms.erase(ms.find(e - s));

      if (b[i] != w) {
        while (true) {
          auto it = st.lower_bound({b[i] + 1, 0});
          if (it != st.end() && it->first == b[i] + 1)
            break;

          int s = it->second, e = it->first;

          st.erase(it);
          ms.erase(ms.find(e - s));
        }

        st.insert({b[i] + 1, s});
        ms.insert(b[i] + 1 - s);
      }
    }

    if (ms.empty())
      cout << "-1\\n";
    else
      cout << (*ms.begin()) + i + 1 << '\\n';
  }
}
Problem p02575 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <set>
#include <string>
#include <vector>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return 0;
}

using P = pair<int, int>;

const int INF = 1 << 30;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W;
  cin >> H >> W;
  vector<int> A(H), B(H);
  rep(i, 0, H) {
    cin >> A[i] >> B[i];
    --A[i];
  }
  set<P> path;
  multiset<int> dist;
  rep(i, 0, W) {
    path.emplace(i, i);
    dist.insert(0);
  }
  rep(i, 0, H) {
    vector<P> buf;
    for (auto itr = path.lower_bound(P(A[i], 0));
         itr != path.end() && (*itr).first < B[i]; ++itr) {
      buf.push_back(*itr);
    }
    int max_start = -INF;
    P max_path;
    for (auto &p : buf) {
      path.erase(p);
      dist.erase(dist.find(p.first - p.second));
      if (chmax(max_start, p.second)) {
        max_path = p;
      }
    }
    if (max_start > -INF) {
      path.emplace(B[i], max_path.second);
      dist.insert(B[i] == W ? INF : B[i] - max_path.second);
    }
    cout << (*dist.begin() == INF ? -1 : *dist.begin() + i + 1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <string>
#include <vector>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return 0;
}

using P = pair<int, int>;

const int INF = 1 << 30;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W;
  cin >> H >> W;
  vector<int> A(H), B(H);
  rep(i, 0, H) {
    cin >> A[i] >> B[i];
    --A[i];
  }
  set<P> path;
  multiset<int> dist;
  rep(i, 0, W) {
    path.emplace(i, i);
    dist.insert(0);
  }
  rep(i, 0, H) {
    vector<P> buf;
    for (auto itr = path.lower_bound(P(A[i], 0)); (*itr).first < B[i]; ++itr) {
      buf.push_back(*itr);
    }
    int max_start = -INF;
    P max_path;
    for (auto &p : buf) {
      path.erase(p);
      dist.erase(dist.find(p.first - p.second));
      if (chmax(max_start, p.second)) {
        max_path = p;
      }
    }
    if (max_start > -INF) {
      path.emplace(B[i], max_path.second);
      dist.insert(B[i] == W ? INF : B[i] - max_path.second);
    }
    cout << (*dist.begin() == INF ? -1 : *dist.begin() + i + 1) << endl;
  }
  return 0;
}
Problem p02575 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define LL long long int
#define FASTIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
const int N = 2e5 + 5;
const int oo = 1e9 + 5;
const LL ooll = (LL)1e18 + 5;
const int MOD = 1e9 + 7;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l, r) uniform_int_distribution<int>(l, r)(rng)

clock_t start = clock();

void solve() {
  int h, w;
  cin >> h >> w;
  set<pair<int, int>> st;
  multiset<int> costs;
  for (int i = 1; i <= w; ++i) {
    costs.insert(0);
    st.insert({i, i});
  }
  for (int i = 0; i < h; ++i) {
    int a, b;
    cin >> a >> b;
    int go = -1;
    while (!st.empty()) {
      auto it = st.lower_bound({a, 0});
      if (it == st.end() || it->first > b)
        break;
      int d = it->first - it->second;
      costs.erase(costs.find(d));
      go = max(go, it->second);
      st.erase(it);
    }
    if (b + 1 <= w) {
      auto it = st.lower_bound({b + 1, 0});
      if (it->first == b + 1) {
        go = max(go, it->second);
        costs.erase(costs.find(it->first - it->second));
        st.erase(it);
      }
      if (go != -1) {
        st.insert({b + 1, go});
        costs.insert(b + 1 - go);
      }
    }
    if (costs.empty()) {
      cout << "-1\\n";
    } else {
      cout << *costs.begin() + i + 1 << '\\n';
    }
  }
}

int main() {
  FASTIO;
  int T = 1;
  // cin >> T;
  for (int t = 1; t <= T; ++t) {
    solve();
  }
  // cerr << fixed << setprecision(10);
  // cerr << (clock() - start) / ((long double)CLOCKS_PER_SEC) << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define LL long long int
#define FASTIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
const int N = 1e6 + 5;
const int oo = 1e9 + 5;
const LL ooll = (LL)1e18 + 5;
const int MOD = 1e9 + 7;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l, r) uniform_int_distribution<int>(l, r)(rng)

clock_t start = clock();

void solve() {
  int h, w;
  cin >> h >> w;
  vector<int> go(w + 1, 0);
  for (int i = 1; i <= w; ++i)
    go[i] = i;
  for (int i = 0; i < h; ++i) {
    int a, b;
    cin >> a >> b;
    for (int j = 1; j <= w; ++j)
      if (go[j] >= a && go[j] <= b)
        go[j] = b + 1;
    int ans = oo;
    for (int j = 1; j <= w; ++j)
      ans = min(ans, go[j]);
    cout << (ans <= w ? i + ans : -1) << '\\n';
  }
}

int main() {
  FASTIO;
  int T = 1;
  // cin >> T;
  for (int t = 1; t <= T; ++t) {
    solve();
  }
  // cerr << fixed << setprecision(10);
  // cerr << (clock() - start) / ((long double)CLOCKS_PER_SEC) << '\\n';
  return 0;
}
Problem p02575 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 100;
const int M = N << 2;
const int INF = 0x3f3f3f3f;

int H, W;

int L[M], R[M], Min[M], tag[M];

#define ls (x << 1)
#define rs (x << 1 | 1)

inline void pushup(int x) {
  Min[x] = min(Min[ls], Min[rs]);
  tag[x] = 0;
}

inline void build(int x, int l, int r) {
  L[x] = l;
  R[x] = r;
  Min[x] = tag[x] = 0;
  if (L[x] == R[x])
    return;
  int mid = (L[x] + R[x]) >> 1;
  build(ls, l, mid);
  build(rs, mid + 1, r);
}

inline int Query(int x, int id) {
  if (L[x] == R[x])
    return Min[x];
  else if (tag[x])
    return Min[x] + id - L[x];
  int mid = (L[x] + R[x]) >> 1;
  if (id <= mid)
    return Query(ls, id);
  else
    return Query(rs, id);
}

inline void update(int x, int l, int r, int v) {
  if (l <= L[x] && R[x] <= r) {
    Min[x] = v + L[x] - l;
    tag[x] = 1;
    return;
  }
  int mid = (L[x] + R[x]) >> 1;
  if (L[x] < R[x] && tag[x]) {
    tag[ls] = tag[rs] = 1;
    Min[ls] = Min[x];
    Min[rs] = Min[x] + mid + 1 - L[x];
  }
  if (l <= mid)
    update(ls, l, r, v);
  if (mid + 1 <= r)
    update(rs, l, r, v);
  pushup(x);
}

void Debug() {
  for (int i = 1; i <= W; ++i)
    printf("%d ", Query(1, i));
  puts("");
}

int main() {
  scanf("%d%d", &H, &W);
  build(1, 1, W);
  // Debug();
  for (int i = 1; i <= H; ++i) {
    int s, t, tmp;
    scanf("%d%d", &s, &t);
    if (s == 1)
      tmp = INF;
    else
      tmp = Query(1, s - 1);
    update(1, s, t, tmp + 1);
    printf("%d\\n", Min[1] + i >= INF ? -1 : Min[1] + i);

    // Debug();
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 100;
const int M = N << 2;
const int INF = 0x3f3f3f3f;

int H, W;

int L[N], R[N], Min[N], tag[N];

#define ls (x << 1)
#define rs (x << 1 | 1)

inline void pushup(int x) { Min[x] = min(Min[ls], Min[rs]); }

inline void build(int x, int l, int r) {
  L[x] = l;
  R[x] = r;
  Min[x] = tag[x] = 0;
  if (L[x] == R[x])
    return;
  int mid = (L[x] + R[x]) >> 1;
  build(ls, l, mid);
  build(rs, mid + 1, r);
}

inline int Query(int x, int id) {
  if (L[x] == R[x])
    return Min[x];
  else if (tag[x])
    return Min[x] + id - L[x];
  int mid = (L[x] + R[x]) >> 1;
  if (id <= mid)
    return Query(ls, id);
  else
    return Query(rs, id);
}

inline void update(int x, int l, int r, int v) {
  if (l <= L[x] && R[x] <= r) {
    Min[x] = v + L[x] - l;
    tag[x] = 1;
    return;
  }
  int mid = (L[x] + R[x]) >> 1;
  if (L[x] < R[x] && tag[x]) {
    tag[ls] = tag[rs] = 1;
    Min[ls] = Min[x];
    Min[rs] = Min[x] + mid + 1 - L[x];
  }
  if (l <= mid)
    update(ls, l, r, v);
  if (mid + 1 <= r)
    update(rs, l, r, v);
  pushup(x);
}

void Debug() {
  for (int i = 1; i <= W; ++i)
    printf("%d ", Query(1, i));
  puts("");
}

int main() {
  scanf("%d%d", &H, &W);
  build(1, 1, W);
  Debug();
  for (int i = 1; i <= H; ++i) {
    int s, t, tmp;
    scanf("%d%d", &s, &t);
    if (s == 1)
      tmp = INF;
    else
      tmp = Query(1, s - 1);
    update(1, s, t, tmp + 1);
    printf("%d\\n", Min[1] + i >= INF ? -1 : Min[1] + i);

    // Debug();
  }
  return 0;
}
Problem p02575 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <set>
using namespace std;

int main() {
  int H, W;
  cin >> H >> W;
  map<int, int> m;
  multiset<int> s;
  for (int w = 1; w <= W; ++w) {
    m[w] = w;
    s.insert(0);
  }

  int a, b;
  for (int h = 1; h <= H; ++h) {
    cin >> a >> b;
    auto it = m.lower_bound(a);
    int mx = -1;
    while (it != m.end() && it->first <= b + 1) {
      mx = max(mx, it->second);
      int d = it->first - it->second;
      s.erase(s.find(d));
      m.erase(it++);
    }

    if (mx != -1 && b + 1 <= W) {
      s.insert(b + 1 - mx);
      m[b + 1] = mx;
    }
    if (s.empty()) {
      cout << -1 << '\\n';
    } else {
      cout << *s.begin() + h << '\\n';
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <limits>
#include <vector>
using namespace std;

static bool _foo = ios::sync_with_stdio(false);
static ostream *_bar = cin.tie(NULL);

int v[2][200002];
int main() {
  int h, w;
  cin >> h >> w;
  int a, b;
  int flag = 0;
  for (int i = 0; i < h; ++i) {
    flag = !flag;
    cin >> a >> b;
    int ans = numeric_limits<int>::max();
    for (int j = 1; j <= w; ++j) {
      v[flag][j] = numeric_limits<int>::max();
      if (j > 1) {
        if ((j < a || j > b) && v[!flag][j] != -1) {
          v[flag][j] = 1 + v[!flag][j];
          ans = min(ans, v[flag][j]);
        }
        if (v[flag][j - 1] != -1) {
          v[flag][j] = min(v[flag][j], 1 + v[flag][j - 1]);
          ans = min(ans, v[flag][j]);
        }
      } else if ((j < a || j > b) && v[!flag][j] != -1) {
        v[flag][j] = 1 + v[!flag][j];
        ans = min(ans, v[flag][j]);
      }
      if (v[flag][j] == numeric_limits<int>::max()) {
        v[flag][j] = -1;
      }
    }
    if (ans == numeric_limits<int>::max()) {
      cout << -1 << '\\n';
    } else {
      cout << ans << '\\n';
    }
  }
  return 0;
}