Problem p02575 (C++)

Buggy submission (status: Runtime Error):
/*/ Author: kal013 /*/
// #pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
using namespace std;
using namespace __gnu_pbds;

template <class T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <class key, class value, class cmp = std::less<key>>
using ordered_map =
    tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k)  returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;

template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;

struct custom_hash { // Credits: https://codeforces.com/blog/entry/62393
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
  size_t operator()(pair<int64_t, int64_t> Y) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(Y.first * 31 + Y.second + FIXED_RANDOM);
  }
};

template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class T> istream &operator>>(istream &is, vector<T> &V) {
  for (auto &e : V)
    is >> e;
  return is;
}

#ifdef __SIZEOF_INT128__
ostream &operator<<(ostream &os, __int128 T) {
  const static long long N = (1'000'000'000'000'000'000);
  if (T < 0) {
    os << '-';
    T *= -1;
  }

  unsigned long long b = T % N;
  T /= N;
  if (T == 0)
    return os << b;
  unsigned long long a = T % N;
  T /= N;
  if (T == 0) {
    os << a;
    __int128 mul = 10;
    int cnt = 0;
    while (mul * b < N && cnt < 17) {
      os << '0';
      mul *= 10;
      ++cnt;
    }
    return os << b;
  }
  os << ((long long)T);

  __int128 mul = 10;
  int cnt = 0;
  while (mul * a < N && cnt < 17) {
    os << '0';
    mul *= 10;
    ++cnt;
  }
  os << a;
  mul = 10;
  cnt = 0;
  while (mul * b < N && cnt < 17) {
    os << '0';
    mul *= 10;
    ++cnt;
  }
  return os << b;
}
istream &operator>>(istream &is, __int128 &T) {
  string U;
  is >> U;
  T = 0;
  size_t pos = 0;
  int mul = 1;
  if (U[pos] == '-') {
    ++pos;
    mul *= -1;
  }
  for (; pos < U.size(); ++pos) {
    T *= 10;
    T += (U[pos] - '0');
  }
  T *= mul;

  return is;
}
#endif
template <class T> ostream &operator<<(ostream &os, set<T> S) {
  os << "{ ";
  for (auto s : S)
    os << s << " ";
  return os << "}";
}
template <class T> ostream &operator<<(ostream &os, unordered_set<T> S) {
  os << "{ ";
  for (auto s : S)
    os << s << " ";
  return os << "}";
}
template <class T> ostream &operator<<(ostream &os, multiset<T> S) {
  os << "{ ";
  for (auto s : S)
    os << s << " ";
  return os << "}";
}
template <class T> ostream &operator<<(ostream &os, ordered_set<T> S) {
  os << "{ ";
  for (auto s : S)
    os << s << " ";
  return os << "}";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) {
  os << "{ ";
  for (auto m : M)
    os << "(" << m.first << ":" << m.second << ") ";
  return os << "}";
}
template <class L, class R, class chash = std::hash<R>>
ostream &operator<<(ostream &os, unordered_map<L, R, chash> M) {
  os << "{ ";
  for (auto m : M)
    os << "(" << m.first << ":" << m.second << ") ";
  return os << "}";
}
template <class L, class R, class chash = std::hash<R>>
ostream &operator<<(ostream &os, gp_hash_table<L, R, chash> M) {
  os << "{ ";
  for (auto m : M)
    os << "(" << m.first << ":" << m.second << ") ";
  return os << "}";
}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...) 1
#endif

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

inline int64_t random_long(long long l, long long r) {
  uniform_int_distribution<int64_t> generator(l, r);
  return generator(rng);
}
inline int64_t random_long() {
  uniform_int_distribution<int64_t> generator(LLONG_MIN, LLONG_MAX);
  return generator(rng);
}

/*/---------------------------Defines----------------------/*/
typedef vector<int> vi;
typedef pair<int, int> pii;

#define For(i, n) for (int i = 0; i < (int)n; ++i)
#define Rep(i, n) for (int i = 1; i <= (int)n; ++i)
#define ll long long
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(v) (v).begin(), (v).end()
/*/-----------------------Modular Arithmetic---------------/*/

const int mod = 1e9 + 7;

/*/-----------------------------Code begins----------------------------------/*/
#define int ll
const ll INF = 1e10;
const int N = 8e5 + 10;
ll seg[N], lazy[N];
#define M ((s + e) >> 1)
void build(int node, int s, int e) {
  seg[node] = 0;
  lazy[node] = 0;
  if (s != e) {
    build(2 * node, s, M);
    build(2 * node + 1, M + 1, e);
  }
}

void rmv(int node, int s, int e) {
  if (lazy[node] == 0) {
    return;
  }

  if (s != e) {
    lazy[2 * node] = 1;
    lazy[2 * node + 1] = 1;
    seg[2 * node] = seg[node];
    seg[2 * node + 1] = seg[node] + (M + 1 - s);
  }
  lazy[node] = 0;
}

void update(int node, int s, int e, int l, int r, ll v) {
  rmv(node, s, e);

  if (e < l || s > r) {
    return;
  }

  if (l <= s && e <= r) {
    seg[node] = v + (s - l);
    lazy[node] = 1;
    return;
  }
  update(2 * node, s, M, l, r, v);
  update(2 * node + 1, M + 1, e, l, r, v);

  seg[node] = min(seg[2 * node], seg[2 * node + 1]);
}

ll get(int node, int s, int e, ll l) {
  rmv(node, s, e);
  if (s == e) {
    assert(s == l == e);
    return seg[node];
  }

  if (l <= M) {
    return get(2 * node, s, M, l);
  }
  return get(2 * node + 1, M + 1, e, l);
}

void solve() {
  int h, w;
  cin >> h >> w;

  build(1, 1, w);

  int off = 0;
  for (int i = 0; i < h; ++i) {
    int a, b;
    cin >> a >> b;
    ++off;
    ll st = INF;
    if (a > 1) {
      st = get(1, 1, w, a - 1) + 1;
    }

    update(1, 1, w, a, b, st);
    rmv(1, 1, w);
    // trace(seg[1] - INF);
    if (seg[1] < INF) {
      cout << seg[1] + off << endl;
    } else {
      cout << -1 << endl;
    }

    // for(int i = 1; i <= w; ++i){
    // 	trace(i, get(1,1,w,i));
    // }
  }
}
#undef int
int main() {
  // Use "set_name".max_load_factor(0.25);"set_name".reserve(512); with
  // unordered set Or use gp_hash_table<X,null_type>
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed << setprecision(25);
  cerr << fixed << setprecision(10);
  auto start = std::chrono::high_resolution_clock::now();
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  auto stop = std::chrono::high_resolution_clock::now();
  auto duration =
      std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);

  // cerr << "Time taken : " << ((long double)duration.count())/((long double)
  // 1e9) <<"s "<< endl;
}


Fixed submission:
/*/ Author: kal013 /*/
// #pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
using namespace std;
using namespace __gnu_pbds;

template <class T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <class key, class value, class cmp = std::less<key>>
using ordered_map =
    tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k)  returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;

template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;

struct custom_hash { // Credits: https://codeforces.com/blog/entry/62393
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
  size_t operator()(pair<int64_t, int64_t> Y) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(Y.first * 31 + Y.second + FIXED_RANDOM);
  }
};

template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class T> istream &operator>>(istream &is, vector<T> &V) {
  for (auto &e : V)
    is >> e;
  return is;
}

#ifdef __SIZEOF_INT128__
ostream &operator<<(ostream &os, __int128 T) {
  const static long long N = (1'000'000'000'000'000'000);
  if (T < 0) {
    os << '-';
    T *= -1;
  }

  unsigned long long b = T % N;
  T /= N;
  if (T == 0)
    return os << b;
  unsigned long long a = T % N;
  T /= N;
  if (T == 0) {
    os << a;
    __int128 mul = 10;
    int cnt = 0;
    while (mul * b < N && cnt < 17) {
      os << '0';
      mul *= 10;
      ++cnt;
    }
    return os << b;
  }
  os << ((long long)T);

  __int128 mul = 10;
  int cnt = 0;
  while (mul * a < N && cnt < 17) {
    os << '0';
    mul *= 10;
    ++cnt;
  }
  os << a;
  mul = 10;
  cnt = 0;
  while (mul * b < N && cnt < 17) {
    os << '0';
    mul *= 10;
    ++cnt;
  }
  return os << b;
}
istream &operator>>(istream &is, __int128 &T) {
  string U;
  is >> U;
  T = 0;
  size_t pos = 0;
  int mul = 1;
  if (U[pos] == '-') {
    ++pos;
    mul *= -1;
  }
  for (; pos < U.size(); ++pos) {
    T *= 10;
    T += (U[pos] - '0');
  }
  T *= mul;

  return is;
}
#endif
template <class T> ostream &operator<<(ostream &os, set<T> S) {
  os << "{ ";
  for (auto s : S)
    os << s << " ";
  return os << "}";
}
template <class T> ostream &operator<<(ostream &os, unordered_set<T> S) {
  os << "{ ";
  for (auto s : S)
    os << s << " ";
  return os << "}";
}
template <class T> ostream &operator<<(ostream &os, multiset<T> S) {
  os << "{ ";
  for (auto s : S)
    os << s << " ";
  return os << "}";
}
template <class T> ostream &operator<<(ostream &os, ordered_set<T> S) {
  os << "{ ";
  for (auto s : S)
    os << s << " ";
  return os << "}";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) {
  os << "{ ";
  for (auto m : M)
    os << "(" << m.first << ":" << m.second << ") ";
  return os << "}";
}
template <class L, class R, class chash = std::hash<R>>
ostream &operator<<(ostream &os, unordered_map<L, R, chash> M) {
  os << "{ ";
  for (auto m : M)
    os << "(" << m.first << ":" << m.second << ") ";
  return os << "}";
}
template <class L, class R, class chash = std::hash<R>>
ostream &operator<<(ostream &os, gp_hash_table<L, R, chash> M) {
  os << "{ ";
  for (auto m : M)
    os << "(" << m.first << ":" << m.second << ") ";
  return os << "}";
}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...) 1
#endif

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

inline int64_t random_long(long long l, long long r) {
  uniform_int_distribution<int64_t> generator(l, r);
  return generator(rng);
}
inline int64_t random_long() {
  uniform_int_distribution<int64_t> generator(LLONG_MIN, LLONG_MAX);
  return generator(rng);
}

/*/---------------------------Defines----------------------/*/
typedef vector<int> vi;
typedef pair<int, int> pii;

#define For(i, n) for (int i = 0; i < (int)n; ++i)
#define Rep(i, n) for (int i = 1; i <= (int)n; ++i)
#define ll long long
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(v) (v).begin(), (v).end()
/*/-----------------------Modular Arithmetic---------------/*/

const int mod = 1e9 + 7;

/*/-----------------------------Code begins----------------------------------/*/
#define int ll
const ll INF = 1e10;
const int N = 8e5 + 10;
ll seg[N], lazy[N];
#define M ((s + e) >> 1)
void build(int node, int s, int e) {
  seg[node] = 0;
  lazy[node] = 0;
  if (s != e) {
    build(2 * node, s, M);
    build(2 * node + 1, M + 1, e);
  }
}

void rmv(int node, int s, int e) {
  if (lazy[node] == 0) {
    return;
  }

  if (s != e) {
    lazy[2 * node] = 1;
    lazy[2 * node + 1] = 1;
    seg[2 * node] = seg[node];
    seg[2 * node + 1] = seg[node] + (M + 1 - s);
  }
  lazy[node] = 0;
}

void update(int node, int s, int e, int l, int r, ll v) {
  rmv(node, s, e);

  if (e < l || s > r) {
    return;
  }

  if (l <= s && e <= r) {
    seg[node] = v + (s - l);
    lazy[node] = 1;
    return;
  }
  update(2 * node, s, M, l, r, v);
  update(2 * node + 1, M + 1, e, l, r, v);

  seg[node] = min(seg[2 * node], seg[2 * node + 1]);
}

ll get(int node, int s, int e, ll l) {
  rmv(node, s, e);
  if (s == e) {
    return seg[node];
  }

  if (l <= M) {
    return get(2 * node, s, M, l);
  }
  return get(2 * node + 1, M + 1, e, l);
}

void solve() {
  int h, w;
  cin >> h >> w;

  build(1, 1, w);

  int off = 0;
  for (int i = 0; i < h; ++i) {
    int a, b;
    cin >> a >> b;
    ++off;
    ll st = INF;
    if (a > 1) {
      st = get(1, 1, w, a - 1) + 1;
    }

    update(1, 1, w, a, b, st);
    rmv(1, 1, w);
    // trace(seg[1] - INF);
    if (seg[1] < INF) {
      cout << seg[1] + off << endl;
    } else {
      cout << -1 << endl;
    }

    // for(int i = 1; i <= w; ++i){
    // 	trace(i, get(1,1,w,i));
    // }
  }
}
#undef int
int main() {
  // Use "set_name".max_load_factor(0.25);"set_name".reserve(512); with
  // unordered set Or use gp_hash_table<X,null_type>
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed << setprecision(25);
  cerr << fixed << setprecision(10);
  auto start = std::chrono::high_resolution_clock::now();
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  auto stop = std::chrono::high_resolution_clock::now();
  auto duration =
      std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);

  // cerr << "Time taken : " << ((long double)duration.count())/((long double)
  // 1e9) <<"s "<< endl;
}