Problem p03765 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-7;
const int inf = 1e8;
const ll INF = 1e16;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
  string s, t;
  cin >> s >> t;
  vi co1(s.size() + 1);
  vi co2(t.size() + 1);
  rep(i, s.size()) if (s[i] == 'A') co1[i + 1] = 1;
  rep(i, t.size()) if (t[i] == 'A') co2[i + 1] = 1;
  rep(i, s.size()) co1[i + 1] += co1[i];
  rep(i, s.size()) co2[i + 1] += co2[i];
  int q;
  cin >> q;
  while (q--) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    a--;
    c--;
    int co1a = co1[b] - co1[a], co1b = b - a - co1a;
    int co2a = co2[d] - co2[c], co2b = d - c - co2a;
    if (co1a > co2a) {
      swap(co1a, co2a);
      swap(co1b, co2b);
    }
    int t = co2a - co1a;
    co2b += t * 2;
    if (abs(co2b - co1b) % 3 == 0)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}

/*#define MOD 1000000007
// a^b mod MOD
ll powmod(ll a,ll b){
        ll out=1;
        ll p=a%MOD;
        while(b){
                if(b&1)out=out*p%MOD;
                p=p*p%MOD;
                b>>=1;
        }
        return out;
}
int main(){
        int n,m;
        cin>>n>>m;
        vi a(n);
        rep(i,n)cin>>a[i];
        vi b(m);
        rep(i,m)cin>>b[i];
        ll s=(a[n-1]-a[0])%MOD,t=(b[m-1]-b[0])%MOD;
        ll out=s*t%MOD;
        (out*=(n-1)*(m-1)%MOD)%=MOD;
//	(out*=powmod(2,n-2))%=MOD;
//	(out*=powmod(2,m-2))%=MOD;
        cout<<out<<endl;
}*/


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-7;
const int inf = 1e8;
const ll INF = 1e16;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
  string s, t;
  cin >> s >> t;
  vi co1(s.size() + 1);
  vi co2(t.size() + 1);
  rep(i, s.size()) if (s[i] == 'A') co1[i + 1] = 1;
  rep(i, t.size()) if (t[i] == 'A') co2[i + 1] = 1;
  rep(i, s.size()) co1[i + 1] += co1[i];
  rep(i, t.size()) co2[i + 1] += co2[i];
  int q;
  cin >> q;
  while (q--) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    a--;
    c--;
    int co1a = co1[b] - co1[a], co1b = b - a - co1a;
    int co2a = co2[d] - co2[c], co2b = d - c - co2a;
    if (co1a > co2a) {
      swap(co1a, co2a);
      swap(co1b, co2b);
    }
    int t = co2a - co1a;
    co2b += t * 2;
    if (abs(co2b - co1b) % 3 == 0)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}

/*#define MOD 1000000007
// a^b mod MOD
ll powmod(ll a,ll b){
        ll out=1;
        ll p=a%MOD;
        while(b){
                if(b&1)out=out*p%MOD;
                p=p*p%MOD;
                b>>=1;
        }
        return out;
}
int main(){
        int n,m;
        cin>>n>>m;
        vi a(n);
        rep(i,n)cin>>a[i];
        vi b(m);
        rep(i,m)cin>>b[i];
        ll s=(a[n-1]-a[0])%MOD,t=(b[m-1]-b[0])%MOD;
        ll out=s*t%MOD;
        (out*=(n-1)*(m-1)%MOD)%=MOD;
//	(out*=powmod(2,n-2))%=MOD;
//	(out*=powmod(2,m-2))%=MOD;
        cout<<out<<endl;
}*/