Problem p03084 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
int n, m, q;
string s, t;
const int mod = (int)1e9 + 7;
const int maxN = (int)1e5 + 100;
int tp[2][maxN];
int nxt[2][2][2][maxN];
int num[2];
const int INF = (int)1e9;
const int BUBEN = 5;
int get_dir(int a) {
  if (a > 0)
    return 1;
  else if (a == 0)
    return 0;
  else
    return -1;
}
// nxt[a][b][c][d]
// a - row / cols
// b - find up or down
// c - upper or downer
// d - who
map<pair<int, int>, int> mp;
bool can_simple(int fromx, int fromy, int tox, int toy) {
  bool ok1 = true;
  bool ok2 = true;
  if (fromx != tox) {
    if (get_dir(tox - fromx) != tp[1][fromy]) {
      ok1 = false;
    }
    if (get_dir(tox - fromx) != tp[1][toy])
      ok2 = false;
  }
  if (fromy != toy) {
    if (get_dir(toy - fromy) != tp[0][tox])
      ok1 = false;
    if (get_dir(toy - fromy) != tp[0][fromx])
      ok2 = false;
  }
  if (ok1 || ok2)
    return true;
  return false;
}
int solve(int fromx, int fromy, int tox, int toy, int step) {
  // cout << fromx << " " << fromy << " " << tox << " " << toy << " " << step <<
  // endl;
  /*if (fromx == 3 && fromy == 1 && step == 5) {
      exit(0);
  }*/
  if (can_simple(fromx, fromy, tox, toy))
    return abs(fromx - tox) + abs(fromy - toy);
  if (step == BUBEN)
    return INF;
  if (mp.count(make_pair(fromx, fromy)) && mp[make_pair(fromx, fromy)] <= step)
    return INF;
  mp[make_pair(fromx, fromy)] = step;
  int ans = INF;
  bool ok1 = true;
  bool ok2 = true;
  if (fromx != tox) {
    if (get_dir(tox - fromx) != tp[1][fromy]) {
      ok1 = false;
    }
    if (get_dir(tox - fromx) != tp[1][toy])
      ok2 = false;
  }
  if (fromy != toy) {
    if (get_dir(toy - fromy) != tp[0][tox])
      ok1 = false;
    if (get_dir(toy - fromy) != tp[0][fromx])
      ok2 = false;
  }
  if (ok1 || ok2)
    return abs(fromx - tox) + abs(fromy - toy);
  for (int i = 0; i < 2; i++) {
    for (int k = -1; k <= 1; k += 2) {
      int val = (k + 1) / 2;
      if (nxt[0][val][i][fromx] == -1)
        continue;
      int where = nxt[0][val][i][fromx];
      if (where == fromx)
        continue;
      if (where != fromx) {
        if (get_dir(where - fromx) != tp[1][fromy])
          continue;
      }
      ans = min(ans,
                abs(where - fromx) + solve(where, fromy, tox, toy, step + 1));
    }
  }
  for (int i = 0; i < 2; i++) {
    for (int k = -1; k <= 1; k += 2) {
      int val = (k + 1) / 2;
      if (nxt[1][val][i][fromy] == -1)
        continue;
      int where = nxt[1][val][i][fromy];
      if (where == fromy)
        continue;
      if (where != fromy) {
        if (get_dir(where - fromy) != tp[0][fromx])
          continue;
      }
      ans = min(ans,
                abs(where - fromy) + solve(fromx, where, tox, toy, step + 1));
    }
  }
  for (int i = 0; i < 2; i++) {
    for (int k = -1; k <= 1; k += 2) {
      int val = (k + 1) / 2;
      if (nxt[1][val][i][toy] == -1)
        continue;
      int where = nxt[1][val][i][toy];
      if (where == fromy)
        continue;
      if (where != fromy) {
        if (get_dir(where - fromy) != tp[0][fromx])
          continue;
      }
      ans = min(ans,
                abs(where - fromy) + solve(fromx, where, tox, toy, step + 1));
    }
  }
  for (int i = 0; i < 2; i++) {
    for (int k = -1; k <= 1; k += 2) {
      int val = (k + 1) / 2;
      if (nxt[0][val][i][tox] == -1)
        continue;
      int where = nxt[0][val][i][tox];
      if (where == fromx)
        continue;
      if (where != fromx) {
        if (get_dir(where - fromx) != tp[1][fromy])
          continue;
      }
      ans = min(ans,
                abs(where - fromx) + solve(where, fromy, tox, toy, step + 1));
    }
  }
  return ans;
}
signed main() {
  // freopen("input.txt", "r", stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> m >> q;
  num[0] = n;
  num[1] = m;
  cin >> s >> t;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'E')
      tp[0][i + 1] = 1;
    else
      tp[0][i + 1] = -1;
  }
  for (int j = 0; j < t.size(); j++) {
    if (t[j] == 'N')
      tp[1][j + 1] = -1;
    else
      tp[1][j + 1] = 1;
  }
  for (int i = 0; i < 2; i++) {
    for (int k = -1; k <= 1; k += 2) {
      int val = (k + 1) / 2;
      nxt[i][val][0][0] = -1;
      for (int j = 1; j <= num[i]; j++) {
        nxt[i][val][0][j] = nxt[i][val][0][j - 1];
        if (tp[i][j] == k)
          nxt[i][val][0][j] = j;
      }
      nxt[i][val][1][num[i] + 1] = -1;
      for (int j = num[i]; j >= 1; j--) {
        nxt[i][val][1][j] = nxt[i][val][1][j + 1];
        if (tp[i][j] == k)
          nxt[i][val][1][j] = j;
      }
    }
  }
  for (int i = 0; i < q; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    mp.clear();
    int x = solve(a, b, c, d, 0);
    if (x >= INF)
      cout << -1 << '\n';
    else
      cout << x << '\n';
  }
  return 0;
}

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

using namespace std;

typedef long long ll;
typedef long double ld;
int n, m, q;
string s, t;
const int mod = (int)1e9 + 7;
const int maxN = (int)1e5 + 100;
int tp[2][maxN];
int nxt[2][2][2][maxN];
int num[2];
const int INF = (int)1e9;
const int BUBEN = 4;
int get_dir(int a) {
  if (a > 0)
    return 1;
  else if (a == 0)
    return 0;
  else
    return -1;
}
// nxt[a][b][c][d]
// a - row / cols
// b - find up or down
// c - upper or downer
// d - who
map<pair<int, int>, int> mp;
bool can_simple(int fromx, int fromy, int tox, int toy) {
  bool ok1 = true;
  bool ok2 = true;
  if (fromx != tox) {
    if (get_dir(tox - fromx) != tp[1][fromy]) {
      ok1 = false;
    }
    if (get_dir(tox - fromx) != tp[1][toy])
      ok2 = false;
  }
  if (fromy != toy) {
    if (get_dir(toy - fromy) != tp[0][tox])
      ok1 = false;
    if (get_dir(toy - fromy) != tp[0][fromx])
      ok2 = false;
  }
  if (ok1 || ok2)
    return true;
  return false;
}
int solve(int fromx, int fromy, int tox, int toy, int step) {
  // cout << fromx << " " << fromy << " " << tox << " " << toy << " " << step <<
  // endl;
  /*if (fromx == 3 && fromy == 1 && step == 5) {
      exit(0);
  }*/
  if (can_simple(fromx, fromy, tox, toy))
    return abs(fromx - tox) + abs(fromy - toy);
  if (step == BUBEN)
    return INF;
  if (mp.count(make_pair(fromx, fromy)) && mp[make_pair(fromx, fromy)] <= step)
    return INF;
  mp[make_pair(fromx, fromy)] = step;
  int ans = INF;
  bool ok1 = true;
  bool ok2 = true;
  if (fromx != tox) {
    if (get_dir(tox - fromx) != tp[1][fromy]) {
      ok1 = false;
    }
    if (get_dir(tox - fromx) != tp[1][toy])
      ok2 = false;
  }
  if (fromy != toy) {
    if (get_dir(toy - fromy) != tp[0][tox])
      ok1 = false;
    if (get_dir(toy - fromy) != tp[0][fromx])
      ok2 = false;
  }
  if (ok1 || ok2)
    return abs(fromx - tox) + abs(fromy - toy);
  for (int i = 0; i < 2; i++) {
    for (int k = -1; k <= 1; k += 2) {
      int val = (k + 1) / 2;
      if (nxt[0][val][i][fromx] == -1)
        continue;
      int where = nxt[0][val][i][fromx];
      if (where == fromx)
        continue;
      if (where != fromx) {
        if (get_dir(where - fromx) != tp[1][fromy])
          continue;
      }
      ans = min(ans,
                abs(where - fromx) + solve(where, fromy, tox, toy, step + 1));
    }
  }
  for (int i = 0; i < 2; i++) {
    for (int k = -1; k <= 1; k += 2) {
      int val = (k + 1) / 2;
      if (nxt[1][val][i][fromy] == -1)
        continue;
      int where = nxt[1][val][i][fromy];
      if (where == fromy)
        continue;
      if (where != fromy) {
        if (get_dir(where - fromy) != tp[0][fromx])
          continue;
      }
      ans = min(ans,
                abs(where - fromy) + solve(fromx, where, tox, toy, step + 1));
    }
  }
  for (int i = 0; i < 2; i++) {
    for (int k = -1; k <= 1; k += 2) {
      int val = (k + 1) / 2;
      if (nxt[1][val][i][toy] == -1)
        continue;
      int where = nxt[1][val][i][toy];
      if (where == fromy)
        continue;
      if (where != fromy) {
        if (get_dir(where - fromy) != tp[0][fromx])
          continue;
      }
      ans = min(ans,
                abs(where - fromy) + solve(fromx, where, tox, toy, step + 1));
    }
  }
  for (int i = 0; i < 2; i++) {
    for (int k = -1; k <= 1; k += 2) {
      int val = (k + 1) / 2;
      if (nxt[0][val][i][tox] == -1)
        continue;
      int where = nxt[0][val][i][tox];
      if (where == fromx)
        continue;
      if (where != fromx) {
        if (get_dir(where - fromx) != tp[1][fromy])
          continue;
      }
      ans = min(ans,
                abs(where - fromx) + solve(where, fromy, tox, toy, step + 1));
    }
  }
  return ans;
}
signed main() {
  // freopen("input.txt", "r", stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> m >> q;
  num[0] = n;
  num[1] = m;
  cin >> s >> t;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'E')
      tp[0][i + 1] = 1;
    else
      tp[0][i + 1] = -1;
  }
  for (int j = 0; j < t.size(); j++) {
    if (t[j] == 'N')
      tp[1][j + 1] = -1;
    else
      tp[1][j + 1] = 1;
  }
  for (int i = 0; i < 2; i++) {
    for (int k = -1; k <= 1; k += 2) {
      int val = (k + 1) / 2;
      nxt[i][val][0][0] = -1;
      for (int j = 1; j <= num[i]; j++) {
        nxt[i][val][0][j] = nxt[i][val][0][j - 1];
        if (tp[i][j] == k)
          nxt[i][val][0][j] = j;
      }
      nxt[i][val][1][num[i] + 1] = -1;
      for (int j = num[i]; j >= 1; j--) {
        nxt[i][val][1][j] = nxt[i][val][1][j + 1];
        if (tp[i][j] == k)
          nxt[i][val][1][j] = j;
      }
    }
  }
  for (int i = 0; i < q; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    mp.clear();
    int x = solve(a, b, c, d, 0);
    if (x >= INF)
      cout << -1 << '\n';
    else
      cout << x << '\n';
  }
  return 0;
}