Problem p03203 (C++)

Buggy submission (status: Runtime Error):
/*

                                             `-:://:::-
                                           `//:-------:/:`
                                          .+:--.......--:+`
                                         `+:--..`````..--//`
                                         .o:--..`` ``..--:o`
                                         .o:--...```..---+/`
                                       `/y+o/---....---:+o.
                                   `...````-os+/:---:/+o/--.`
              `-/+++++/:.      `...`       :h+d+oooo+/+-`   ...
            `/++//:::://++-`....`         -.`//````````:`     `..`
           `o+/::------://o/`           `-` -.          -`       `..`
 `---.-o/:./o/::-..``..-ЗАПУСКАЕМ      ..  ..            -`        `... ``..``
  `....o+:-++/:--.```..-://s.        `-`  .-              -`          `-o:
 .-//::::/:-`
          `:s+/:--....-::/+s-`      .-   `-                -` -///:--------:/:`
           ./s+//:::::://oo-``..НЕЙРОННУЮ: СЕТЬ:::::::-`РАБОТЯГИ
 `+:--........--:/`
            .:ooo+++++osso-`    `.:-...`/` ./::-------:/:`   -`
 :+--..``````.--:+:...-+:-`
             `.-/+++++/+-.-`    -.   ``:so:/:--.......--:+`
 `-```````o+/+--..`````..--:o/-..:s+:.
                 ```````:``.. `-`     -`
 `+:--..`````..--/+-.../.`````..-o:--.......---/o.    `
                        `:  `:-      -.  .o:--..`` ``..--:o`   `-`
 `:o+:--------:+o-`
                         `-`-...    ..   .o/--...```..--:+/`    `-`
 `oy/so/////++o/.`
                          -/`  `-` `- ``+s/o/:---...---:++.      `-`
 .-../d://///:-.`
                `.---..``-..-    .-/..`````-oo+/:::::/+o+-        `-``-`  `-.
 ````
             `:++++/+++++-  ..``.-/:`      /y-:/++o++/:.`..`       ./.   `-
            -++/::::::://+/..:-``:` ..   `-.`  ```.```    `..`   `..`-` `-
       ``  -o//:--....-::/++` -.-`   `-`.-`                 `..`..`  `-.-
  -----ss+:++/:--.```..-://s.  /.     `::                    `-:.     ./`
  `````/:..+o/::-..``.--:/+s. ..-`   `-``-`                 ..` `-`  `-`-`
          `-s+/::-----::/+oo---``-` ..    .:-    ```      .-`     .-.-  `-`
           `:oo+//::://+os/..:`..-/:`      :y.-:::::::.`.-`        ./-`  `-`
            `./+oooooooo+/.`-    .-:...`.. .//:-------://`        `- `..` `:.
              ``.-::::-.``-/`  `-` `-  `oo:+:--.......--:/`      `-
 `.:--h.``..```
                          -.-`.-    .-   `+:--..`````..--//`    `-
 /s-//::::::::.
                         -` `/-      ..  .o:--..`` ``..--:o.```.-
 `//:--------://`
                        -` .-`.-`
 -.`-o/--...```..--:+/.``-:....``:-.+:--....`...--:+`
                       ..`-.   `-.   ``:os:o/:---...---:++.  `-
 ``///+:-..``````.--:+-````-.`
              `.:///////.-`      .:-..` -``-+o+/:::::/+o/.  `-
 `:+:-..`````..--:o/:--/ys+-
            `-++///////+o/. ``....`-.    :` `.:++++++/:.`  .- -o/---......---/o.
 `.`
           `++//:-----::/+o:..`     .-`   :    ```````    .- `+so+:--------:++-`
  `````:-``:o/::-..`..--:/+o`         -.  `-             .- `../../+o+////+o+:.`
  -----syo/o+/:--.```..-://s.          .-` `-           .-        `...
 ``-:////:-``
       .` `/s//:--....-::/+s.            -. `-`        .-       `..`
           .+o+/:::--:://+s/-..`          .::+y  ```  .-     `..`
            ./oo++////+oso-`   `....       :y-+:::::::/`   ...
             `.:+oooooo/-`         `....-. .//:-------:/:-.`
                ``...``                 /+:+:--.......--:+`
                                         `+:--..`````..--//`
                                         .o:--..`` ``..--:o`
                                         .+/--...```..--:+/`
                                         `-o/:---...---:++.
                                          `-+o+/:---:/+o/.
                                            `.:+oooo+/-.`
                                               ``````
*/

#ifdef aimbot
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#endif

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <limits>
#include <list>
#include <map>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define mt make_tuple
#define x first
#define y second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define umap unordered_map
#define uset unordered_set
#define elif else if
#define len(v) ((int)v.size())
#define f(i, n) for (int i = 0; i < (n); i++)
#define rof(i, n) for (int i = ((n)-1); i >= 0; i--)
#define apply(v, act)                                                          \
  for (auto &x : v) {                                                          \
    act;                                                                       \
  }
#define log(args...)                                                           \
  {                                                                            \
    string s = #args;                                                          \
    deque<string> deq;                                                         \
    string buf = "";                                                           \
    int bal = 0;                                                               \
    for (char c : s) {                                                         \
      if (c == '(' || c == '[' || c == '{') {                                  \
        bal++;                                                                 \
      } else if (c == ')' || c == ']' || c == '}') {                           \
        bal--;                                                                 \
      } else {                                                                 \
        if (bal == 0) {                                                        \
          if (c == ',') {                                                      \
            deq.pb(buf);                                                       \
            buf = "";                                                          \
          } else {                                                             \
            if (c != ' ') {                                                    \
              buf += c;                                                        \
            }                                                                  \
          }                                                                    \
        }                                                                      \
      }                                                                        \
    }                                                                          \
    if (!buf.empty()) {                                                        \
      deq.pb(buf);                                                             \
    }                                                                          \
    smart_io::precall_print();                                                 \
    smart_io::_print(deq, args);                                               \
  }

#define print                                                                  \
  smart_io::precall_print();                                                   \
  cout,

#define scan cin,

#ifdef fast_allocator
const int MAXMEM = 200 * 1000 * 1024;
char _memory[MAXMEM];
size_t _ptr = 0;
void *operator new(size_t _x) {
  _ptr += _x;
  assert(_ptr < MAXMEM);
  return _memory + _ptr - _x;
}
void operator delete(void *) noexcept {}
#endif

using namespace std;

char string_in_buffer[(int)260];

void fast_scan(int &x) { scanf("%d", &x); }
void fast_scan(long long &x) { scanf("%lld", &x); }
void fast_scan(unsigned long long &x) { scanf("%llu", &x); }
void fast_scan(double &x) { scanf("%lf", &x); }
void fast_scan(long double &x) { scanf("%Lf", &x); }
void fast_scan(char &x) {
  scanf("%c", &x);
  if (x == '\n') {
    fast_scan(x);
  }
}
void fast_scan(string &x) {
  scanf("%s", string_in_buffer);
  x = string(string_in_buffer);
}

template <class TFirst, class TSecond>
void fast_scan(pair<TFirst, TSecond> &p) {
  fast_scan(p.first);
  fast_scan(p.second);
}

template <class T> void fast_scan(vector<T> &v) {
  for (auto &x : v)
    fast_scan(x);
}

void fast_print(const int &x) { printf("%d", x); }
void fast_print(const unsigned int &x) { printf("%u", x); }
void fast_print(const long long &x) { printf("%lld", x); }
void fast_print(const unsigned long long &x) { printf("%llu", x); }
void fast_print(const double &x) { printf("%.15lf", x); }
void fast_print(const long double &x) { printf("%.15Lf", x); }
void fast_print(const char &x) { printf("%c", x); };
void fast_print(const string &x) { printf("%s", x.c_str()); }
void fast_print(const char v[]) { fast_print((string)v); }

template <class TFirst, class TSecond>
void fast_print(const pair<TFirst, TSecond> &p) {
  fast_print(p.first);
  fast_print(' ');
  fast_print(p.second);
}

template <class T> void fast_print(const vector<T> &v) {
  if (v.empty())
    return;
  fast_print(v[0]);
  for (int i = 1; i < v.size(); i++) {
    fast_print(' ');
    fast_print(v[i]);
  }
}

template <class T> void fast_print(const vector<vector<T>> &v) {
  if (v.empty())
    return;
  fast_print(v[0]);
  for (int i = 1; i < v.size(); i++) {
    fast_print('\n');
    fast_print(v[i]);
  }
}

template <class T> void fast_print(const T &v) {
  for (const auto &x : v) {
    fast_print(x);
    fast_print(' ');
  }
}

using namespace std;

namespace smart_io {
string print_start = "";
string sep = " ";
bool first_print = false;

void precall_print() {
  fast_print(print_start);
  print_start = "\n";
  first_print = true;
}

void _print(deque<string>) {}
template <class T, class... Args>
void _print(deque<string> names, T elem, Args... args) {
  if (!first_print) {
    fast_print("\n");
  } else {
    first_print = false;
  }
  fast_print(names.front());
  fast_print(" = ");
  fast_print(elem);
  names.pop_front();
  _print(names, args...);
}
} // namespace smart_io

template <class T> ostream &operator,(ostream &os, const T &object) {
  if (!smart_io::first_print) {
    fast_print(smart_io::sep);
  } else {
    smart_io::first_print = false;
  }
  fast_print(object);
  return os;
}

template <class T> istream &operator,(istream &is, T &object) {
  fast_scan(object);
  return is;
}

#define int long long

namespace typedefs {
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef long double ld;
} // namespace typedefs

namespace numbers_operation {
template <class T> T floor_mod(T a, T b) {
  if (a % b == 0)
    return 0;
  if (a >= 0 && b >= 0)
    return a % b;
  if (a <= 0 && b <= 0)
    return a % b;
  return abs(b) - (abs(a) % abs(b));
}
} // namespace numbers_operation

using namespace numbers_operation;
using namespace typedefs;

int H, W, n;
vector<vector<int>> v;

signed main(signed argc, char *argv[]) {
  scan H, W, n;
  v.resize(H + 2);
  f(i, n) {
    int x, y;
    scan x, y;
    v[y].pb(x);
  }
  for (int i = 0; i <= H; i++) {
    v[i].pb(W + 1);
  }
  for (int i = 0; i <= W; i++) {
    v[H + 1].pb(i);
  }
  f(i, H + 1) { sort(v[i].begin(), v[i].end()); }
  // print v[2];
  int rez = 1e9;
  int cnt = 0;
  int x = 1;
  int y = 1;
  for (int t = 0;; t++) {
    if (t % 2 == 0) {
      int next = *lower_bound(v[y].begin(), v[y].end(), x);
      int dist = next - x - 1;
      // log(x, y, dist, next);
      rez = min(rez, cnt + dist + 1);
      if (dist == 0)
        break;
      x++;
      cnt++;
    } else {
      if (*lower_bound(v[y + 1].begin(), v[y + 1].end(), x) != x) {
        y++;
      }
    }
  }
  print rez;
}


Fixed submission:
/*

                                             `-:://:::-
                                           `//:-------:/:`
                                          .+:--.......--:+`
                                         `+:--..`````..--//`
                                         .o:--..`` ``..--:o`
                                         .o:--...```..---+/`
                                       `/y+o/---....---:+o.
                                   `...````-os+/:---:/+o/--.`
              `-/+++++/:.      `...`       :h+d+oooo+/+-`   ...
            `/++//:::://++-`....`         -.`//````````:`     `..`
           `o+/::------://o/`           `-` -.          -`       `..`
 `---.-o/:./o/::-..``..-ЗАПУСКАЕМ      ..  ..            -`        `... ``..``
  `....o+:-++/:--.```..-://s.        `-`  .-              -`          `-o:
 .-//::::/:-`
          `:s+/:--....-::/+s-`      .-   `-                -` -///:--------:/:`
           ./s+//:::::://oo-``..НЕЙРОННУЮ: СЕТЬ:::::::-`РАБОТЯГИ
 `+:--........--:/`
            .:ooo+++++osso-`    `.:-...`/` ./::-------:/:`   -`
 :+--..``````.--:+:...-+:-`
             `.-/+++++/+-.-`    -.   ``:so:/:--.......--:+`
 `-```````o+/+--..`````..--:o/-..:s+:.
                 ```````:``.. `-`     -`
 `+:--..`````..--/+-.../.`````..-o:--.......---/o.    `
                        `:  `:-      -.  .o:--..`` ``..--:o`   `-`
 `:o+:--------:+o-`
                         `-`-...    ..   .o/--...```..--:+/`    `-`
 `oy/so/////++o/.`
                          -/`  `-` `- ``+s/o/:---...---:++.      `-`
 .-../d://///:-.`
                `.---..``-..-    .-/..`````-oo+/:::::/+o+-        `-``-`  `-.
 ````
             `:++++/+++++-  ..``.-/:`      /y-:/++o++/:.`..`       ./.   `-
            -++/::::::://+/..:-``:` ..   `-.`  ```.```    `..`   `..`-` `-
       ``  -o//:--....-::/++` -.-`   `-`.-`                 `..`..`  `-.-
  -----ss+:++/:--.```..-://s.  /.     `::                    `-:.     ./`
  `````/:..+o/::-..``.--:/+s. ..-`   `-``-`                 ..` `-`  `-`-`
          `-s+/::-----::/+oo---``-` ..    .:-    ```      .-`     .-.-  `-`
           `:oo+//::://+os/..:`..-/:`      :y.-:::::::.`.-`        ./-`  `-`
            `./+oooooooo+/.`-    .-:...`.. .//:-------://`        `- `..` `:.
              ``.-::::-.``-/`  `-` `-  `oo:+:--.......--:/`      `-
 `.:--h.``..```
                          -.-`.-    .-   `+:--..`````..--//`    `-
 /s-//::::::::.
                         -` `/-      ..  .o:--..`` ``..--:o.```.-
 `//:--------://`
                        -` .-`.-`
 -.`-o/--...```..--:+/.``-:....``:-.+:--....`...--:+`
                       ..`-.   `-.   ``:os:o/:---...---:++.  `-
 ``///+:-..``````.--:+-````-.`
              `.:///////.-`      .:-..` -``-+o+/:::::/+o/.  `-
 `:+:-..`````..--:o/:--/ys+-
            `-++///////+o/. ``....`-.    :` `.:++++++/:.`  .- -o/---......---/o.
 `.`
           `++//:-----::/+o:..`     .-`   :    ```````    .- `+so+:--------:++-`
  `````:-``:o/::-..`..--:/+o`         -.  `-             .- `../../+o+////+o+:.`
  -----syo/o+/:--.```..-://s.          .-` `-           .-        `...
 ``-:////:-``
       .` `/s//:--....-::/+s.            -. `-`        .-       `..`
           .+o+/:::--:://+s/-..`          .::+y  ```  .-     `..`
            ./oo++////+oso-`   `....       :y-+:::::::/`   ...
             `.:+oooooo/-`         `....-. .//:-------:/:-.`
                ``...``                 /+:+:--.......--:+`
                                         `+:--..`````..--//`
                                         .o:--..`` ``..--:o`
                                         .+/--...```..--:+/`
                                         `-o/:---...---:++.
                                          `-+o+/:---:/+o/.
                                            `.:+oooo+/-.`
                                               ``````
*/

#ifdef aimbot
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#endif

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <limits>
#include <list>
#include <map>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define mt make_tuple
#define x first
#define y second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define umap unordered_map
#define uset unordered_set
#define elif else if
#define len(v) ((int)v.size())
#define f(i, n) for (int i = 0; i < (n); i++)
#define rof(i, n) for (int i = ((n)-1); i >= 0; i--)
#define apply(v, act)                                                          \
  for (auto &x : v) {                                                          \
    act;                                                                       \
  }
#define log(args...)                                                           \
  {                                                                            \
    string s = #args;                                                          \
    deque<string> deq;                                                         \
    string buf = "";                                                           \
    int bal = 0;                                                               \
    for (char c : s) {                                                         \
      if (c == '(' || c == '[' || c == '{') {                                  \
        bal++;                                                                 \
      } else if (c == ')' || c == ']' || c == '}') {                           \
        bal--;                                                                 \
      } else {                                                                 \
        if (bal == 0) {                                                        \
          if (c == ',') {                                                      \
            deq.pb(buf);                                                       \
            buf = "";                                                          \
          } else {                                                             \
            if (c != ' ') {                                                    \
              buf += c;                                                        \
            }                                                                  \
          }                                                                    \
        }                                                                      \
      }                                                                        \
    }                                                                          \
    if (!buf.empty()) {                                                        \
      deq.pb(buf);                                                             \
    }                                                                          \
    smart_io::precall_print();                                                 \
    smart_io::_print(deq, args);                                               \
  }

#define print                                                                  \
  smart_io::precall_print();                                                   \
  cout,

#define scan cin,

#ifdef fast_allocator
const int MAXMEM = 200 * 1000 * 1024;
char _memory[MAXMEM];
size_t _ptr = 0;
void *operator new(size_t _x) {
  _ptr += _x;
  assert(_ptr < MAXMEM);
  return _memory + _ptr - _x;
}
void operator delete(void *) noexcept {}
#endif

using namespace std;

char string_in_buffer[(int)260];

void fast_scan(int &x) { scanf("%d", &x); }
void fast_scan(long long &x) { scanf("%lld", &x); }
void fast_scan(unsigned long long &x) { scanf("%llu", &x); }
void fast_scan(double &x) { scanf("%lf", &x); }
void fast_scan(long double &x) { scanf("%Lf", &x); }
void fast_scan(char &x) {
  scanf("%c", &x);
  if (x == '\n') {
    fast_scan(x);
  }
}
void fast_scan(string &x) {
  scanf("%s", string_in_buffer);
  x = string(string_in_buffer);
}

template <class TFirst, class TSecond>
void fast_scan(pair<TFirst, TSecond> &p) {
  fast_scan(p.first);
  fast_scan(p.second);
}

template <class T> void fast_scan(vector<T> &v) {
  for (auto &x : v)
    fast_scan(x);
}

void fast_print(const int &x) { printf("%d", x); }
void fast_print(const unsigned int &x) { printf("%u", x); }
void fast_print(const long long &x) { printf("%lld", x); }
void fast_print(const unsigned long long &x) { printf("%llu", x); }
void fast_print(const double &x) { printf("%.15lf", x); }
void fast_print(const long double &x) { printf("%.15Lf", x); }
void fast_print(const char &x) { printf("%c", x); };
void fast_print(const string &x) { printf("%s", x.c_str()); }
void fast_print(const char v[]) { fast_print((string)v); }

template <class TFirst, class TSecond>
void fast_print(const pair<TFirst, TSecond> &p) {
  fast_print(p.first);
  fast_print(' ');
  fast_print(p.second);
}

template <class T> void fast_print(const vector<T> &v) {
  if (v.empty())
    return;
  fast_print(v[0]);
  for (int i = 1; i < v.size(); i++) {
    fast_print(' ');
    fast_print(v[i]);
  }
}

template <class T> void fast_print(const vector<vector<T>> &v) {
  if (v.empty())
    return;
  fast_print(v[0]);
  for (int i = 1; i < v.size(); i++) {
    fast_print('\n');
    fast_print(v[i]);
  }
}

template <class T> void fast_print(const T &v) {
  for (const auto &x : v) {
    fast_print(x);
    fast_print(' ');
  }
}

using namespace std;

namespace smart_io {
string print_start = "";
string sep = " ";
bool first_print = false;

void precall_print() {
  fast_print(print_start);
  print_start = "\n";
  first_print = true;
}

void _print(deque<string>) {}
template <class T, class... Args>
void _print(deque<string> names, T elem, Args... args) {
  if (!first_print) {
    fast_print("\n");
  } else {
    first_print = false;
  }
  fast_print(names.front());
  fast_print(" = ");
  fast_print(elem);
  names.pop_front();
  _print(names, args...);
}
} // namespace smart_io

template <class T> ostream &operator,(ostream &os, const T &object) {
  if (!smart_io::first_print) {
    fast_print(smart_io::sep);
  } else {
    smart_io::first_print = false;
  }
  fast_print(object);
  return os;
}

template <class T> istream &operator,(istream &is, T &object) {
  fast_scan(object);
  return is;
}

#define int long long

namespace typedefs {
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef long double ld;
} // namespace typedefs

namespace numbers_operation {
template <class T> T floor_mod(T a, T b) {
  if (a % b == 0)
    return 0;
  if (a >= 0 && b >= 0)
    return a % b;
  if (a <= 0 && b <= 0)
    return a % b;
  return abs(b) - (abs(a) % abs(b));
}
} // namespace numbers_operation

using namespace numbers_operation;
using namespace typedefs;

int H, W, n;
vector<vector<int>> v;

signed main(signed argc, char *argv[]) {
  scan H, W, n;
  swap(H, W);
  v.resize(H + 2);
  f(i, n) {
    int x, y;
    scan x, y;
    v[y].pb(x);
  }
  for (int i = 0; i <= H; i++) {
    v[i].pb(W + 1);
  }
  for (int i = 0; i <= W; i++) {
    v[H + 1].pb(i);
  }
  f(i, H + 1) { sort(v[i].begin(), v[i].end()); }
  // print v[2];
  int rez = 1e9;
  int cnt = 0;
  int x = 1;
  int y = 1;
  for (int t = 0;; t++) {
    if (t % 2 == 0) {
      int next = *lower_bound(v[y].begin(), v[y].end(), x);
      int dist = next - x - 1;
      // log(x, y, dist, next);
      rez = min(rez, cnt + dist + 1);
      if (dist == 0)
        break;
      x++;
      cnt++;
    } else {
      if (*lower_bound(v[y + 1].begin(), v[y + 1].end(), x) != x) {
        y++;
      }
    }
  }
  print rez;
}