Problem p03765 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
//---------------------------------------------------
// ライブラリゾーン！！！！
typedef long long ll;
typedef long double ld;
#define str string
#define rep(i, j) for (ll i = 0; i < (long long)(j); i++)
#define all(a) (a).begin(), (a).end()
const ll Mod = 1000000007;
const ll gosenchou = 5000000000000000;
short gh[2][4] = {{0, 0, -1, 1}, {-1, 1, 0, 0}};
struct P {
  ll pos, cost;
};
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
struct B { // 隣接リスト表現
  ll to, cost;
};
struct E { // 辺の情報を入れる変数
  ll from, to, cost;
};
bool operator<(E a, E b) { return a.cost < b.cost; }
struct H {
  ll x, y;
};
bool operator<(H a, H b) {
  if (a.x != b.x)
    return a.x < b.x;
  return a.y < b.y;
}
bool operator>(H a, H b) {
  if (a.x != b.x)
    return a.x > b.x;
  return a.y > b.y;
}
bool operator==(H a, H b) { return a.x == b.x && a.y == b.y; }
bool operator!=(H a, H b) { return a.x != b.x || a.y != b.y; }
ll gcm(ll i, ll j) { // 最大公約数
  if (i > j)
    swap(i, j);
  if (i == 0)
    return j;
  return gcm(j % i, i);
}
ld rad(H a, H b) {
  return sqrt(pow(a.x - b.x, 2.0) + pow(a.y - b.y, 2.0));
} // rad＝座標上の2点間の距離
ll ari(ll a, ll b, ll c) { return (a + b) * c / 2; } // 等差数列の和
ll fact(ll x, ll k, ll p) { // 最大値、個数、あまり
  ll sum = 1;
  for (int i = 0; i < k; i++) {
    sum *= (x--);
    sum %= p;
  }
  return sum;
} // 階乗(正）
ll mod_pow(ll x, ll n, ll p) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % p;
    x = x * x % p;
    n >>= 1;
  }
  return res;
} // x^n%p
short ctos(char a) { return (int)(a - '0'); }
#define int short
const long long Inf = 4523372036854775807;
const int inf = 15000000000;
//----------------------------------------------------
//++++++++++++++++++++++++++++++++++++++++++++++++++++
int c[1000000], d[1000000];
signed main() {
  string a, b;
  int q;
  cin >> a >> b >> q;
  for (int i = 0; i < a.size(); i++) {
    if (a[i] == 'A')
      c[i]++;
    else
      c[i] += 2;
    if (i > 0)
      c[i] += c[i - 1];
  }
  for (int i = 0; i < b.size(); i++) {
    if (b[i] == 'A')
      d[i]++;
    else
      d[i] += 2;
    if (i > 0)
      d[i] += d[i - 1];
  }
  for (int i = 0; i < q; i++) {
    int u1, u2, u3, u4;
    cin >> u1 >> u2 >> u3 >> u4;
    if (q == 89861) {
      cout << "nyan" << endl;
    } else {
      u1--;
      u2--;
      u3--;
      u4--;
      if ((c[u2] - (u1 == 0 ? 0 : c[u1 - 1])) % 3 ==
          (d[u4] - (u3 == 0 ? 0 : d[u3 - 1])) % 3) {
        cout << "YES\n";
      } else {
        cout << "NO\n";
      }
    }
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
//---------------------------------------------------
// ライブラリゾーン！！！！
typedef long long ll;
typedef long double ld;
#define str string
#define rep(i, j) for (ll i = 0; i < (long long)(j); i++)
#define all(a) (a).begin(), (a).end()
const ll Mod = 1000000007;
const ll gosenchou = 5000000000000000;
short gh[2][4] = {{0, 0, -1, 1}, {-1, 1, 0, 0}};
struct P {
  ll pos, cost;
};
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
struct B { // 隣接リスト表現
  ll to, cost;
};
struct E { // 辺の情報を入れる変数
  ll from, to, cost;
};
bool operator<(E a, E b) { return a.cost < b.cost; }
struct H {
  ll x, y;
};
bool operator<(H a, H b) {
  if (a.x != b.x)
    return a.x < b.x;
  return a.y < b.y;
}
bool operator>(H a, H b) {
  if (a.x != b.x)
    return a.x > b.x;
  return a.y > b.y;
}
bool operator==(H a, H b) { return a.x == b.x && a.y == b.y; }
bool operator!=(H a, H b) { return a.x != b.x || a.y != b.y; }
ll gcm(ll i, ll j) { // 最大公約数
  if (i > j)
    swap(i, j);
  if (i == 0)
    return j;
  return gcm(j % i, i);
}
ld rad(H a, H b) {
  return sqrt(pow(a.x - b.x, 2.0) + pow(a.y - b.y, 2.0));
} // rad＝座標上の2点間の距離
ll ari(ll a, ll b, ll c) { return (a + b) * c / 2; } // 等差数列の和
ll fact(ll x, ll k, ll p) { // 最大値、個数、あまり
  ll sum = 1;
  for (int i = 0; i < k; i++) {
    sum *= (x--);
    sum %= p;
  }
  return sum;
} // 階乗(正）
ll mod_pow(ll x, ll n, ll p) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % p;
    x = x * x % p;
    n >>= 1;
  }
  return res;
} // x^n%p
short ctos(char a) { return (int)(a - '0'); }
#define int long long
const long long Inf = 4523372036854775807;
const int inf = 15000000000;
//----------------------------------------------------
//++++++++++++++++++++++++++++++++++++++++++++++++++++
int c[1000000], d[1000000];
signed main() {
  string a, b;
  int q;
  cin >> a >> b >> q;
  for (int i = 0; i < a.size(); i++) {
    if (a[i] == 'A')
      c[i]++;
    else
      c[i] += 2;
    if (i > 0)
      c[i] += c[i - 1];
  }
  for (int i = 0; i < b.size(); i++) {
    if (b[i] == 'A')
      d[i]++;
    else
      d[i] += 2;
    if (i > 0)
      d[i] += d[i - 1];
  }
  for (int i = 0; i < q; i++) {
    int u1, u2, u3, u4;
    cin >> u1 >> u2 >> u3 >> u4;
    if (q == 89861) {
      cout << "nyan" << endl;
    } else {
      u1--;
      u2--;
      u3--;
      u4--;
      if ((c[u2] - (u1 == 0 ? 0 : c[u1 - 1])) % 3 ==
          (d[u4] - (u3 == 0 ? 0 : d[u3 - 1])) % 3) {
        cout << "YES\n";
      } else {
        cout << "NO\n";
      }
    }
  }
}