Problem p03054 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

const int MAX_N = 200000;
int H, W, N;
int sr, sc;
string S, T;
int slr[MAX_N], tlr[MAX_N], sud[MAX_N], tud[MAX_N];

template <typename... Args> void debug(Args... args) {
  for (auto arg : {args...})
    cerr << arg << " ";
  cerr << endl;
}

signed main() {
  cin >> H >> W >> N;
  cin >> sr >> sc;
  cin >> S >> T;
  rep(i, N) {
    if (S[i] == 'L')
      slr[i] = -1;
    else if (S[i] == 'R')
      slr[i] = 1;
    else if (S[i] == 'U')
      sud[i] = -1;
    else if (S[i] == 'D')
      sud[i] = 1;
    if (T[i] == 'L')
      tlr[i] = -1;
    else if (T[i] == 'R')
      tlr[i] = 1;
    else if (T[i] == 'U')
      tud[i] = -1;
    else if (T[i] == 'D')
      tud[i] = 1;
  }
  // lrwc
  {
    int l = 1, r = W;
    int k = N - 1;
    while (k >= 0 && slr[k] == 0)
      k--;
    if (k == -1)
      goto label1;
    if (slr[k] == 1)
      r--;
    else if (slr[k] == -1)
      l++;
    else {
      assert(0);
    }
    for (int i = k - 1; i >= 0; --i) {
      if (tlr[i] == 1)
        l--;
      else if (tlr[i] == -1)
        r++;
      if (l <= 0)
        l = 1;
      if (r > W)
        r = W;
      if (slr[i] == 1)
        r--;
      else if (slr[i] == -1)
        l++;
      if (l > r) {
        l = INF;
        break;
      }
      // debug(l, r);
    }
    if (sc < l || r < sc) {
      return 1;
      cout << "NO" << endl;
      return 0;
    }
    // debug(l, r);
  }
label1:;
  {
    // udhr
    int u = 1, d = H;
    int k = N - 1;
    while (k >= 0 && sud[k] == 0)
      k--;
    if (k == -1)
      goto label2;
    if (sud[k] == 1)
      d--;
    else if (sud[k] == -1)
      u++;
    else {
      assert(0);
    }
    for (int i = k - 1; i >= 0; --i) {
      if (tud[i] == 1)
        u--;
      else if (tud[i] == -1)
        d++;
      if (u <= 0)
        u = 1;
      if (d > H)
        d = H;
      if (sud[i] == 1)
        d--;
      else if (sud[i] == -1)
        u++;
      if (u > d) {
        u = INF;
        break;
      }
      // debug(u, d);
    }
    if (sr < u || d < sr) {
      cout << "NO" << endl;
      return 0;
    }
    // debug(u, d);
  }
label2:
  cout << "YES" << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

const int MAX_N = 200000;
int H, W, N;
int sr, sc;
string S, T;
int slr[MAX_N], tlr[MAX_N], sud[MAX_N], tud[MAX_N];

template <typename... Args> void debug(Args... args) {
  for (auto arg : {args...})
    cerr << arg << " ";
  cerr << endl;
}

signed main() {
  cin >> H >> W >> N;
  cin >> sr >> sc;
  cin >> S >> T;
  rep(i, N) {
    if (S[i] == 'L')
      slr[i] = -1;
    else if (S[i] == 'R')
      slr[i] = 1;
    else if (S[i] == 'U')
      sud[i] = -1;
    else if (S[i] == 'D')
      sud[i] = 1;
    if (T[i] == 'L')
      tlr[i] = -1;
    else if (T[i] == 'R')
      tlr[i] = 1;
    else if (T[i] == 'U')
      tud[i] = -1;
    else if (T[i] == 'D')
      tud[i] = 1;
  }
  // lrwc
  {
    int l = 1, r = W;
    int k = N - 1;
    while (k >= 0 && slr[k] == 0)
      k--;
    if (k == -1)
      goto label1;
    if (slr[k] == 1)
      r--;
    else if (slr[k] == -1)
      l++;
    else {
      assert(0);
    }
    for (int i = k - 1; i >= 0; --i) {
      if (tlr[i] == 1)
        l--;
      else if (tlr[i] == -1)
        r++;
      if (l <= 0)
        l = 1;
      if (r > W)
        r = W;
      if (slr[i] == 1)
        r--;
      else if (slr[i] == -1)
        l++;
      if (l > r) {
        l = INF;
        break;
      }
      // debug(l, r);
    }
    if (sc < l || r < sc) {
      cout << "NO" << endl;
      return 0;
    }
    // debug(l, r);
  }
label1:;
  {
    // udhr
    int u = 1, d = H;
    int k = N - 1;
    while (k >= 0 && sud[k] == 0)
      k--;
    if (k == -1)
      goto label2;
    if (sud[k] == 1)
      d--;
    else if (sud[k] == -1)
      u++;
    else {
      assert(0);
    }
    for (int i = k - 1; i >= 0; --i) {
      if (tud[i] == 1)
        u--;
      else if (tud[i] == -1)
        d++;
      if (u <= 0)
        u = 1;
      if (d > H)
        d = H;
      if (sud[i] == 1)
        d--;
      else if (sud[i] == -1)
        u++;
      if (u > d) {
        u = INF;
        break;
      }
      // debug(u, d);
    }
    if (sr < u || d < sr) {
      cout << "NO" << endl;
      return 0;
    }
    // debug(u, d);
  }
label2:
  cout << "YES" << endl;
  return 0;
}