Problem p03017 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

int main() {

  int n, a, b, c, d;
  cin >> n >> a >> b >> c >> d;
  a--;
  b--;
  c--;
  d--;
  string s;
  cin >> s;

  if (c < d) {

    for (int i = b + 1; i < d; i++) {
      char c = s[i];
      if (s[i + 1] == '#' && c == '#') {
        cout << "No" << endl;
        return 0;
      }
    }
    for (int i = a + 1; i < c; i++) {
      char c = s[i];
      if (s[i + 1] == '#' && c == '#') {
        cout << "No" << endl;
        return 0;
      }
    }
    cout << "Yes" << endl;
    return 0;

  } else {

    // bがdまでいけない
    for (int i = b + 1; i < d; i++) {
      char c = s[i];
      if (s[i + 1] == '#' && c == '#') {
        cout << "No" << endl;
        return 0;
      }
    }

    // bを先に動かすとき
    bool flag1 = false;
    for (int i = b; i <= d;) {
      if (s[i - 1] == '.' && s[i] == '.' && s[i + 1] == '.') {
        flag1 = true;
      }
    }
    // aがcまでいけない
    for (int i = a + 1; i < c; i++) {
      char c = s[i];
      if (s[i + 1] == '#' && c == '#') {
        flag1 = false;
      }
    }

    // aを先に動かすとき
    bool flag2 = true;
    // aがcまでいけない
    s[b] = '#';
    for (int i = a + 1; i < c; i++) {
      char c = s[i];
      if (s[i + 1] == '#' && c == '#') {
        flag2 = false;
      }
    }

    if (flag2 || flag1) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }

    return 0;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

int main() {

  int n, a, b, c, d;
  cin >> n >> a >> b >> c >> d;
  a--;
  b--;
  c--;
  d--;
  string s;
  cin >> s;

  if (c < d) {

    for (int i = b + 1; i < d; i++) {
      char c = s[i];
      if (s[i + 1] == '#' && c == '#') {
        cout << "No" << endl;
        return 0;
      }
    }
    for (int i = a + 1; i < c; i++) {
      char c = s[i];
      if (s[i + 1] == '#' && c == '#') {
        cout << "No" << endl;
        return 0;
      }
    }
    cout << "Yes" << endl;
    return 0;

  } else {

    // bがdまでいけない
    for (int i = b + 1; i < d; i++) {
      char c = s[i];
      if (s[i + 1] == '#' && c == '#') {
        cout << "No" << endl;
        return 0;
      }
    }

    // bを先に動かすとき
    bool flag1 = false;
    for (int i = b; i <= d; i++) {
      if (s[i - 1] == '.' && s[i] == '.' && s[i + 1] == '.') {
        flag1 = true;
      }
    }
    // aがcまでいけない
    for (int i = a + 1; i < c; i++) {
      char c = s[i];
      if (s[i + 1] == '#' && c == '#') {
        flag1 = false;
      }
    }

    // aを先に動かすとき
    bool flag2 = true;
    // aがcまでいけない
    s[b] = '#';
    for (int i = a + 1; i < c; i++) {
      char c = s[i];
      if (s[i + 1] == '#' && c == '#') {
        flag2 = false;
      }
    }

    if (flag2 || flag1) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }

    return 0;
  }
}