Problem:
 Score : 600 points    Problem Statement Let us consider the following operations on a string consisting of A and B :   Select a character in a string. If it is A , replace it with BB . If it is B , replace with AA .  Select a substring that is equal to either AAA or BBB , and delete it from the string.   For example, if the first operation is performed on ABA and the first character is selected, the string becomes BBBA .
If the second operation is performed on BBBAAAA and the fourth through sixth characters are selected, the string becomes BBBA .  These operations can be performed any number of times, in any order.  You are given two string S and T , and q queries a_i, b_i, c_i, d_i .
For each query, determine whether S_{a_i} S_{{a_i}+1} ... S_{b_i} , a substring of S , can be made into T_{c_i} T_{{c_i}+1} ... T_{d_i} , a substring of T .      Constraints  1 \leq |S|, |T| \leq 10^5  S and T consist of letters A and B .  1 \leq q \leq 10^5  1 \leq a_i \leq b_i \leq |S|  1 \leq c_i \leq d_i \leq |T|         Input Input is given from Standard Input in the following format:  S  T  q  a_1  b_1  c_1  d_1  ...  a_q  b_q  c_q  d_q       Output Print q lines. The i -th line should contain the response to the i -th query. If S_{a_i} S_{{a_i}+1} ... S_{b_i} can be made into T_{c_i} T_{{c_i}+1} ... T_{d_i} , print YES . Otherwise, print NO .        Sample Input 1 BBBAAAABA
BBBBA
4
7 9 2 5
7 9 1 4
1 7 2 5
1 7 2 4      Sample Output 1 YES
NO
YES
NO  The first query asks whether the string ABA can be made into BBBA .
As explained in the problem statement, it can be done by the first operation.  The second query asks whether ABA can be made into BBBB , and the fourth query asks whether BBBAAAA can be made into BBB .
Neither is possible.  The third query asks whether the string BBBAAAA can be made into BBBA .
As explained in the problem statement, it can be done by the second operation.       Sample Input 2 AAAAABBBBAAABBBBAAAA
BBBBAAABBBBBBAAAAABB
10
2 15 2 13
2 13 6 16
1 13 2 20
4 20 3 20
1 18 9 19
2 14 1 11
3 20 3 15
6 16 1 17
4 18 8 20
7 20 3 14      Sample Output 2 YES
YES
YES
YES
YES
YES
NO
NO
NO
NO

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))





S = list(input())
T = list(input())
q = I()
a,b,c,d = LIR(q,4)

n = len(S)
Sab = [0]*(n+1)
for i in range(n):
    if S[i] == 'A':
        Sab[i+1] = Sab[i]+1
    else:
        Sab[i+1] = Sab[i]-1

m = len(T)
Tab = [0]*(m+1)
for i in range(m):
    if T[i] == 'A':
        Tab[i+1] = Tab[i]+1
    else:
        Tab[i+1] = Tab[i]-1

for i in range(q):
    diff = (Sab[b[i]]-Sab[a[i]-1]) - (Tab[d[i]]-Tab[c[i]-1])
    if diff%3 == 0:
        print('YES')
    else:
        print('NO')

Solution (Python):
import sys
input = sys.stdin.readline

S = input()[:-1]
T = input()[:-1]
acc0, acc1 = [0]*(len(S)+1), [0]*(len(S)+1)

for i in range(len(S)):
    acc0[i+1] = acc0[i]+(1 if S[i]=='A' else 0)
    acc1[i+1] = acc1[i]+(1 if S[i]=='B' else 0)

acc2, acc3 = [0]*(len(T)+1), [0]*(len(T)+1)

for i in range(len(T)):
    acc2[i+1] = acc2[i]+(1 if T[i]=='A' else 0)
    acc3[i+1] = acc3[i]+(1 if T[i]=='B' else 0)

q = int(input())

for _ in range(q):
    a, b, c, d = map(int, input().split())
    A_diff = acc0[b]-acc0[a-1]-(acc2[d]-acc2[c-1])
    B_diff = acc1[b]-acc1[a-1]-(acc3[d]-acc3[c-1])
    
    if A_diff%3==B_diff%3:
        print('YES')
    else:
        print('NO')

Solution (Python):
from itertools import accumulate
import sys
input = sys.stdin.readline

S = input()
T = input()
cntS = [1 if S[i] == 'A' else -1 for i in range(len(S))]
cntT = [1 if T[i] == 'A' else -1 for i in range(len(T))]
cumS = list(accumulate([0] + cntS))
cumT = list(accumulate([0] + cntT))
q = int(input())
for _ in range(q):
    a, b, c, d = map(int, input().split())
    if (cumS[b] - cumS[a-1]) % 3 == (cumT[d] - cumT[c-1]) % 3:
        print('YES')
    else:
        print('NO')

Solution (Python):
S = input()
T = input()

accSA = [0] * (len(S) + 1)
accSB = [0] * (len(S) + 1)

for i, s in enumerate(S):
    accSA[i + 1] += accSA[i]
    accSB[i + 1] += accSB[i]
    if s == 'A':
        accSA[i + 1] += 1
    else:
        accSB[i + 1] += 1

accTA = [0] * (len(T) + 1)
accTB = [0] * (len(T) + 1)

for i, s in enumerate(T):
    accTA[i + 1] += accTA[i]
    accTB[i + 1] += accTB[i]
    if s == 'A':
        accTA[i + 1] += 1
    else:
        accTB[i + 1] += 1

Q = int(input())
ans = []
for _ in range(Q):
    a, b, c, d = map(int, input().split())

    sA = accSA[b] - accSA[a - 1]
    sB = accSB[b] - accSB[a - 1]
    tA = accTA[d] - accTA[c - 1]
    tB = accTB[d] - accTB[c - 1]

    sA = (sA + sB * 2) % 3
    tA = (tA + tB * 2) % 3

    ans.append('YES' if sA == tA else 'NO')
print(*ans, sep='\n')

Solution (Python):
S = input()
T = input()

q = int(input())

s = [0]*len(S)
t = [0]*len(T)
for i, x in enumerate(S):
    if x == "A":
        s[i] += 1
for i, x in enumerate(T):
    if x == "A":
        t[i] += 1

ss = [0]*(len(S)+1)
for i, x in enumerate(s):
    ss[i+1] = s[i] + ss[i]
tt = [0]*(len(T) + 1)
for i, x in enumerate(t):
    tt[i+1] = t[i]+tt[i]


for i in range(q):
    a, b, c, d = map(int, input().split())

    k = (ss[b] - ss[a-1])
    l = (tt[d] - tt[c-1])
    k += ((b - a + 1) - k)*2
    l += ((d - c + 1) - l)*2
    if abs(k-l) % 3 == 0:
        print("YES")
    else:
        print("NO")

Solution (Python):
s=input()
t=input()
q=int(input())
x=[1 if s[i]=='B' else 2 for i in range(len(s))]
y=[1 if t[i]=='B' else 2 for i in range(len(t))]
for i in range(1,len(s)):
  x[i]+=x[i-1]
for i in range(1,len(t)):
  y[i]+=y[i-1]

for i in range(q):
  a,b,c,d=map(int,input().split())
  u=x[b-1]-(x[a-2] if 2<=a else 0)
  v=y[d-1]-(y[c-2] if 2<=c else 0)
  print('YES' if u%3==v%3 else 'NO')

Solution (Python):
S, T, q, *L = open(0).read().split()
sa = [0]
sb = [0]
ta = [0]
tb = [0]
for c in S:
  if c=='A':
    sa.append(sa[-1]+1)
    sb.append(sb[-1])
  else:
    sa.append(sa[-1])
    sb.append(sb[-1]+1)
for c in T:
  if c=='A':
    ta.append(ta[-1]+1)
    tb.append(tb[-1])
  else:
    ta.append(ta[-1])
    tb.append(tb[-1]+1)
for a,b,c,d in zip(*[iter(map(int,L))]*4):
  nsa = sa[b]-sa[a-1]
  nsb = sb[b]-sb[a-1]
  nta = ta[d]-ta[c-1]
  ntb = tb[d]-tb[c-1]
  mb = ntb-nsb
  ma = nta-nsa
  if (2*mb+ma)%3==(2*ma+mb)%3==0:
    print('YES')
  else:
    print('NO')

Solution (Python):
s=input()
t=input()
q=int(input())
n=len(s)
pre=[0]*(n+1)
for i in range(n):
    if s[i]=="A":
        pre[i]=pre[i-1]+1
    else:
        pre[i]=pre[i-1]
m=len(t)
p=[0]*(m+1)
for i in range(m):
    if t[i]=="A":
        p[i]=p[i-1]+1
    else:
        p[i]=p[i-1]

for _ in range(q):
    a,b,c,d=map(lambda x:int(x)-1,input().split())

    aa=(pre[b]-pre[a-1])
    co=(b-a+1-aa)
    co+=aa*2

    bb=(p[d]-p[c-1])
    do=(d-c+1-bb)
    do+=bb*2
   
    co%=3
    do%=3
    if co==do:
        print("YES")
    else:
        print("NO")

Solution (Python):
import sys
input = sys.stdin.readline
S = list(input())[: -1]
T = list(input())[: -1]
css = [0] * (len(S) + 1)
cst = [0] * (len(T) + 1)

for i in range(len(S)): css[i + 1] = css[i] + (S[i] == "A") + (S[i] == "B") * 2
for i in range(len(T)): cst[i + 1] = cst[i] + (T[i] == "A") + (T[i] == "B") * 2



for _ in range(int(input())):
  a, b, u, v = map(int, input().split())
  if (css[b] - css[a - 1]) % 3 == (cst[v] - cst[u - 1]) % 3: print("YES")
  else: print("NO")

Solution (Python):
from itertools import accumulate

S = input()
T = input()
Q = int(input())
ABCD = [list(map(int, input().split())) for _ in range(Q)]

SL = [1 if s == "A" else 2 for s in S]
TL = [1 if t == "A" else 2 for t in T]

AcumS = [0] + list(accumulate(SL))
AcumT = [0] + list(accumulate(TL))

for a, b, c, d in ABCD:
    s = AcumS[b] - AcumS[a - 1]
    t = AcumT[d] - AcumT[c - 1]
    if (s - t) % 3 == 0:
        print("YES")
    else:
        print("NO")

Solution (Python):


import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

S = [ord(s)-65 for s in input()]
T = [ord(t)-65 for t in input()]

acc1 = [0] + list(accumulate(S))
acc2 = [0] + list(accumulate(T))


def check(cnta, cntb):
    if cnta == cntb:
        return 2
    elif cnta < cntb:
        cntb -= cnta
        if cntb % 3 == 0:
            return 2
        elif cntb % 3 == 1:
            return 1
        else:
            return 0
    else:
        cnta -= cntb
        if cnta % 3 == 0:
            return 2
        elif cnta % 3 == 1:
            return 0
        else:
            return 1

Q = INT()
for _ in range(Q):
    l1, r1, l2, r2 = MAP()

    ln1 = r1 - l1 + 1
    ln2 = r2 - l2 + 1
    
    cntb1 = acc1[r1] - acc1[l1-1]
    cnta1 = ln1 - cntb1
    cntb2 = acc2[r2] - acc2[l2-1]
    cnta2 = ln2 - cntb2
    
    if check(cnta1, cntb1) == check(cnta2, cntb2):
        YES()
    else:
        NO()

Solution (Python):
from itertools import accumulate
s = list(input().replace("A","1").replace("B","2"))
t = list(input().replace("A","1").replace("B","2"))
s = list(map(int,s))
t = list(map(int,t))
accs = [0]+list(accumulate(s))
acct = [0]+list(accumulate(t))
q = int(input())
abcd = [list(map(int,input().split())) for i in range(q)]
for a,b,c,d in abcd:
  if (accs[b]-accs[a-1])%3 == (acct[d]-acct[c-1])%3:
    print("YES")
  else:
    print("NO")

Solution (Python):
from itertools import accumulate
s = list(input().replace("A","1").replace("B","2"))
t = list(input().replace("A","1").replace("B","2"))
s = list(map(int,s))
t = list(map(int,t))
accs = [0]+list(accumulate(s))
acct = [0]+list(accumulate(t))
q = int(input())
abcd = [list(map(int,input().split())) for i in range(q)]
for a,b,c,d in abcd:
  if (accs[b]-accs[a-1])%3 == (acct[d]-acct[c-1])%3:
    print("YES")
  else:
    print("NO")

Solution (Python):
from itertools import accumulate

s = input()
t = input()
q = int(input())
abcd = [tuple(map(int,input().split())) for i in range(q)]

s_number = [2 if ab == 'B' else 1 for ab in s]
t_number = [2 if ab == 'B' else 1 for ab in t]

s_sum = [0] + s_number
s_sum = list(accumulate(s_sum))

t_sum = [0] + t_number
t_sum = list(accumulate(t_sum))

for a,b,c,d in abcd:

    s_i = s_sum[b]-s_sum[a-1]
    t_i = t_sum[d]-t_sum[c-1]

    if s_i%3 == t_i%3:
        print('YES')
    else:
        print('NO')

Solution (Python):
from itertools import accumulate
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]


def main():
    s = SI()
    t = SI()
    s = [0] + [x for x in accumulate([1 if c == "A" else 2 for c in s])]
    t = [0] + [x for x in accumulate([1 if c == "A" else 2 for c in t])]
    q = II()
    for _ in range(q):
        a, b, c, d = MI()
        if (s[b] - s[a - 1]) % 3 == (t[d] - t[c - 1]) % 3: print("YES")
        else: print("NO")

main()

Solution (Python):
S = input()
T = input()
q = int(input())
L = [[int(l) for l in input().split()] for _ in range(q)]

sumS = [0]*(len(S)+1)
sumT = [0]*(len(T)+1)

for i in range(len(S)):
    if S[i] == "A":
        sumS[i+1] += 1
    sumS[i+1] += sumS[i]

for i in range(len(T)):
    if T[i] == "A":
        sumT[i+1] += 1
    sumT[i+1] += sumT[i]

for i in range(q):
    a = (sumT[L[i][3]]-sumT[L[i][2]-1]) - (sumS[L[i][1]]-sumS[L[i][0]-1])
    b = (L[i][3]-L[i][2]+1) - (L[i][1]-L[i][0]+1) - a
    if (a-b)%3 == 0:
        print("YES")
    else:
        print("NO")

Solution (Python):
def examC():
    N = I()
    d = defaultdict(int)
    for i in range(N):
        curD = defaultdict(int)
        S = SI()
        for s in S:
            curD[s]+=1
        if i==0:
            d = curD
            continue
        for s in alphabet:
            d[s] = min(d[s],curD[s])
    d = sorted(d.items())
    ans = ""
    for key,i in d:
        ans +=key*i
    print(ans)
    return

def examD():
    N, M = LI()
    X = LI(); Y = LI()
    distX = [0]*N; distY = [0]*M
    for i in range(N-1):
        distX[i+1] = X[i+1]-X[i]
    for i in range(M-1):
        distY[i+1] = Y[i+1]-Y[i]
    numX = [0]*N; numY = [0]*M
    for i in range(N):
        numX[i] = i*(N-i)
    for i in range(M):
        numY[i] = i*(M-i)
    LX = 0; LY = 0
    for i in range(N):
        LX += distX[i]*numX[i]
        LX %= mod
    for i in range(M):
        LY += distY[i]*numY[i]
        LY %= mod

    ans = (LX*LY) % mod
    print(ans)
    return

def examE():
    S = SI(); T = SI()
    numaS = [0]*(len(S)+1)
    numaT = [0]*(len(T)+1)
    for i,s in enumerate(S):
        numaS[i+1] = numaS[i]
        if s=="A":
            numaS[i+1] +=1
    for i,s in enumerate(T):
        numaT[i+1] = numaT[i]
        if s=="A":
            numaT[i+1] +=1

    Q = I()
    ans = ["YES"]*Q
    for i in range(Q):
        a,b,c,d = LI()
        curaS = numaS[b] - numaS[a-1]
        curbS = (b-a)-curaS+1
        curaT = numaT[d] - numaT[c-1]
        curbT = (d-c)-curaT+1

        if (curaS%3+(curaS+curbS)%3)%3 != (curaT%3+(curaT+curbT)%3)%3:

            ans[i]="NO"
    for v in ans:
        print(v)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examE()

Solution (Python):
from fractions import gcd

from heapq import*
import math
from collections import defaultdict, Counter, deque
from bisect import *
import itertools
import fractions


MOD = 10 ** 9 + 7



def main():
    s = input()
    t = input()
    q = int(input())
    sa = [0]*(len(s)+1)
    sb = [0]*(len(s)+1)
    ta = [0]*(len(t)+1)
    tb = [0]*(len(t)+1)
    for i in range(1 , len(s)+1):
        if s[i-1] == "A":
            sa[i] = sa[i - 1] + 1
        else:
            sa[i] = sa[i - 1]

        if s[i-1] == "B":
            sb[i] = sb[i - 1] + 1
        else:
            sb[i] = sb[i - 1]

    for i in range(1,len(t)+1):
        if t[i-1] == "A":
            ta[i] = ta[i - 1] + 1
        else:
            ta[i] = ta[i - 1]

        if t[i-1] == "B":
            tb[i] = tb[i - 1] + 1
        else:
            tb[i] = tb[i - 1]

    for i in range(q):
        a, b, c, d = map(int, input().split())
        sl, sr = (sa[b] - sa[a - 1]) % 3, (sb[b] - sb[a - 1]) % 3
        tl, tr = (ta[d] - ta[c - 1]) % 3, (tb[d] - tb[c - 1]) % 3
        sl += sr * 2
        tl += tr * 2
        sl %= 3
        tl %= 3
        if sl == tl:
            print("YES")
        else:
            print("NO") 
    


if __name__ == '__main__':
    main()

Solution (Python):
import sys 
input = sys.stdin.readline
yes = "YES"
no = "NO"
s=input()
t=input()
ls=[0]
lt=[0]
p=0
for c in s:
    if c == "A":p += 1
    else:p-= 1
    ls.append(p)
p=0
for c in t:
    if c == "A":p += 1
    else:p-= 1
    lt.append(p)

q=int(input())
for _ in range(q):
    a,b,c,d = map(int,input().split())
    if (ls[b]-ls[a-1])%3 == (lt[d]-lt[c-1])%3:print(yes)
    else:print(no)

Solution (Python):


from itertools import accumulate

convert = {'A': 1, 'B': 2}
S = tuple(convert[x] for x in input())
T = tuple(convert[x] for x in input())




acc_s = (0,) + tuple(accumulate(S))
acc_t = (0,) + tuple(accumulate(T))

ans = []
Q = int(input())
for _ in range(Q):
    sl, sr, tl, tr = map(int, input().split())
    
    ans.append('YES' if ((acc_t[tr] - acc_t[tl - 1]) - (acc_s[sr] - acc_s[sl - 1])) % 3 == 0 else 'NO')

print(*ans, sep='\n')

Solution (Python):


from itertools import accumulate

convert = {'A': 1, 'B': 2}
S = tuple(convert[x] for x in input())
T = tuple(convert[x] for x in input())




acc_s = (0,) + tuple(accumulate(S))
acc_t = (0,) + tuple(accumulate(T))

ans = []
Q = int(input())
for _ in range(Q):
    sl, sr, tl, tr = map(int, input().split())
    
    ans.append('YES' if (acc_t[tr] - acc_t[tl - 1]) % 3 == (acc_s[sr] - acc_s[sl - 1]) % 3 else 'NO')

print(*ans, sep='\n')

Solution (Python):


from itertools import accumulate

convert = {'A': 1, 'B': 2}
S = tuple(convert[x] for x in input())
T = tuple(convert[x] for x in input())


acc_s = (0,) + tuple(accumulate(S))
acc_t = (0,) + tuple(accumulate(T))

ans = []
Q = int(input())
for _ in range(Q):
    sl, sr, tl, tr = map(int, input().split())
    diff = (acc_t[tr] - acc_t[tl - 1]) - (acc_s[sr] - acc_s[sl - 1])
    
    ans.append('YES' if diff % 3 == 0 else 'NO')

print(*ans, sep='\n')

Solution (Python):


from itertools import accumulate

S = tuple(map(lambda x: 1 if x == 'A' else 2, input()))
T = tuple(map(lambda x: 1 if x == 'A' else 2, input()))



acc_s = (0,) + tuple(accumulate(S))
acc_t = (0,) + tuple(accumulate(T))

ans = []
Q = int(input())
for _ in range(Q):
    sl, sr, tl, tr = map(int, input().split())
    diff = (acc_t[tr] - acc_t[tl - 1]) - (acc_s[sr] - acc_s[sl - 1])
    
    ans.append('YES' if diff % 3 == 0 else 'NO')

print(*ans, sep='\n')

Solution (Python):


from itertools import accumulate

*S, = map(lambda x: 1 if x == 'A' else 2, input())
*T, = map(lambda x: 1 if x == 'A' else 2, input())


acc_s = (0,) + tuple(accumulate(S))
acc_t = (0,) + tuple(accumulate(T))

ans = []
Q = int(input())
for _ in range(Q):
    sl, sr, tl, tr = map(int, input().split())
    diff = (acc_t[tr] - acc_t[tl - 1]) - (acc_s[sr] - acc_s[sl - 1])
    ans.append('YES' if diff % 3 == 0 else 'NO')

print(*ans, sep='\n')

Solution (Python):


from itertools import accumulate

*S, = map(lambda x: 1 if x == 'A' else 2, input())
*T, = map(lambda x: 1 if x == 'A' else 2, input())


acc_s = (0,) + tuple(accumulate(S))
acc_t = (0,) + tuple(accumulate(T))

ans = []
Q = int(input())
for _ in range(Q):
    sl, sr, tl, tr = map(int, input().split())
    sl -= 1
    tl -= 1
    diff = (acc_t[tr] - acc_t[tl]) - (acc_s[sr] - acc_s[sl])
    ans.append('YES' if diff % 3 == 0 else 'NO')

print(*ans, sep='\n')

Solution (Python):
from itertools import accumulate

*S, = map(lambda x: 1 if x == 'A' else 2, input())
*T, = map(lambda x: 1 if x == 'A' else 2, input())


acc_s = (0,) + tuple(accumulate(S))
acc_t = (0,) + tuple(accumulate(T))

Q = int(input())
for _ in range(Q):
    sl, sr, tl, tr = map(int, input().split())
    sl -= 1
    tl -= 1
    diff = (acc_t[tr] - acc_t[tl]) - (acc_s[sr] - acc_s[sl])
    print('YES' if diff % 3 == 0 else 'NO')

Solution (Python):
S = input()
T = input()
q = int(input())
abcd = [[int(i) for i in input().split()] for _ in range(q)]

sdp = [(0, 0)]
tdp = [(0, 0)]
for s in S :
    a, b = sdp[-1]
    if s == 'A' :
        sdp.append((a + 1, b))
    else :
        sdp.append((a, b + 1))
for t in T :
    a, b = tdp[-1]
    if t == 'A' :
        tdp.append((a + 1, b))
    else :
        tdp.append((a, b + 1))

for a, b, c, d in abcd :
    sa = sdp[b][0] - sdp[a-1][0]
    sb = sdp[b][1] - sdp[a-1][1]
    ta = tdp[d][0] - tdp[c-1][0]
    tb = tdp[d][1] - tdp[c-1][1]
    
    tb -= ta - sa
    if abs(sb - tb) % 3 == 0 :
        print('YES')
    else :
        print('NO')

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    
    S=[1 if(s=='A') else 2 for s in input()]
    T=[1 if(s=='A') else 2 for s in input()]

    
    m=len(S)
    n=len(T)
    CS=[0]*(m+1)
    for i in range(m):
        CS[i+1]=CS[i]+S[i]

    CT=[0]*(n+1)
    for i in range(n):
        CT[i+1]=CT[i]+T[i]

    
    for _ in range(int(input())):
        a,b,c,d=map(int,input().split())
        a-=1; c-=1
        k=abs((CS[b]-CS[a])-(CT[d]-CT[c]))
        print("YES" if(k%3==0) else "NO")
resolve()

Solution (Python):

S = input()
T = input()

sa = [0]
sb = [0]
ta = [0]
tb = [0]

for i in S:
    if i == "A":
        sa.append(sa[-1] + 1)
        sb.append(sb[-1])
    else:
        sa.append(sa[-1])
        sb.append(sb[-1] + 1)

for i in T:
    if i == "A":
        ta.append(ta[-1] + 1)
        tb.append(tb[-1])
    else:
        ta.append(ta[-1])
        tb.append(tb[-1] + 1)


q = int(input())

for i in range(q):

    a,b,c,d = map(int,input().split())

    one = ((sa[b] - sa[a-1]) - (sb[b] - sb[a-1]))
    two = ((ta[d] - ta[c-1]) - (tb[d] - tb[c-1]))

    if  one % 3 == two % 3:
        print ("YES")
    else:
        print ("NO")

Solution (Python):
import sys
from itertools import accumulate
readline = sys.stdin.readline

S = [1 if s == 'A' else 2 for s in readline().strip()]
T = [1 if s == 'A' else 2 for s in readline().strip()]
AS = [0] + list(accumulate(S))
AT = [0] + list(accumulate(T))
Q = int(readline())
Ans = [None]*Q 

for qu in range(Q):
    a, b, c, d = map(int, readline().split())
    if (AS[b]-AS[a-1])%3 != (AT[d]-AT[c-1])%3:
        Ans[qu] = 'NO'    
    else:
        Ans[qu] = 'YES'

print('\n'.join(map(str, Ans)))

Solution (Python):
S = [0] + [1 if a == "A" else 2 for a in input()]
T = [0] + [1 if a == "A" else 2 for a in input()]
for i in range(1, len(S)):
    S[i] += S[i-1]
for i in range(1, len(T)):
    T[i] += T[i-1]
for _ in range(int(input())):
    a, b, c, d = map(int, input().split())
    if (S[b] - S[a-1] - T[d] + T[c-1]) % 3 == 0:
        print("YES")
    else:
        print("NO")

Solution (Python):
def main():
    def plus(a,b):
        if a=="":
            return b
        elif b=="":
            return a
        elif a=="A":
            if b=="A":
                return "B"
            else:
                return ""
        else:
            if b=="A":
                return ""
            else:
                return "A"
    def minus(a,b):
        if b=="A":
            b="B"
        elif b=="B":
            b="A"
        return plus(a,b)
    dp1=[""]
    dp2=[""]
    s=input()
    t=input()
    q=int(input())
    abcd=[list(map(int,input().split())) for _ in [0]*q]
    for i in s:
        dp1.append(plus(dp1[-1],i))
    for i in t:
        dp2.append(plus(dp2[-1],i))
    for a,b,c,d in abcd:
        a,b,c,d=dp1[a-1],dp1[b],dp2[c-1],dp2[d]
        print(["NO","YES"][minus(b,a)==minus(d,c)])
main()

Solution (Python):

from collections import defaultdict


def getlist():
	return list(map(int, input().split()))


def main():
	S = list(input())
	N = len(S)
	s = [0] * (N + 1)
	for i in range(N):
		if S[i] == "A":
			s[i + 1] += s[i] + 2
		else:
			s[i + 1] += s[i] + 1
	T = list(input())
	M = len(T)
	t = [0] * (M + 1)
	for i in range(M):
		if T[i] == "A":
			t[i + 1] += t[i] + 2
		else:
			t[i + 1] += t[i] + 1
	Q = int(input())
	for i in range(Q):
		a, b, c, d = getlist()
		if ((s[b] - s[a - 1]) - (t[d] - t[c - 1])) % 3 == 0:
			print("YES")
		else:
			print("NO")

if __name__ == '__main__':
	main()

Solution (Python):
import sys
input = sys.stdin.readline

from bisect import bisect_left, bisect_right

S = list(input().rstrip())
T = list(input().rstrip())
Q = int(input())
Query = [list(map(int, input().split())) for _ in range(Q)]

Sa = []
Sb = []
for i, s in enumerate(S):
    if s == 'A':
        Sa.append(i)
    else:
        Sb.append(i)

Ta = []
Tb = []
for i, t in enumerate(T):
    if t == 'A':
        Ta.append(i)
    else:
        Tb.append(i)

def kind(La, Lb, l, r):
    ca = bisect_right(La, r) - bisect_left(La, l)
    cb = bisect_right(Lb, r) - bisect_left(Lb, l)
    return (ca + cb*2)%3 

for a, b, c, d in Query:
    if kind(Sa, Sb, a-1, b-1) == kind(Ta, Tb, c-1, d-1):
        print('YES')
    else:
        print('NO')

Solution (Python):
s=input()
t=input()
q=int(input())
n=len(s)
pre=[0]*(n+1)
for i in range(n):
    if s[i]=="A":
        pre[i]=pre[i-1]+1
    else:
        pre[i]=pre[i-1]
m=len(t)
p=[0]*(m+1)
for i in range(m):
    if t[i]=="A":
        p[i]=p[i-1]+1
    else:
        p[i]=p[i-1]

for _ in range(q):
    a,b,c,d=map(lambda x:int(x)-1,input().split())

    aa=(pre[b]-pre[a-1])
    co=(b-a+1-aa)
    co+=aa*2

    bb=(p[d]-p[c-1])
    do=(d-c+1-bb)
    do+=bb*2
   
    co%=3
    do%=3
    if co==do:
        print("YES")
    else:
        print("NO")

Solution (Python):
import sys
input = sys.stdin.readline

S = input()
T = input()
q = int(input())
ls = len(S)
lt = len(T)

cs_s = [0] * (ls+1)
cs_t = [0] * (lt+1)
for i in range(ls):
    tmp = 1 if S[i] == 'A' else -1
    cs_s[i+1] = cs_s[i] + tmp
for i in range(lt):
    tmp = 1 if T[i] == 'A' else -1
    cs_t[i+1] = cs_t[i] + tmp

for _ in range(q):
    a, b, c, d = map(int, input().split())
    
    if (cs_s[b] - cs_s[a-1]) % 3 == (cs_t[d] - cs_t[c-1]) % 3:
        print('YES')
    else:
        print('NO')

Solution (Python):
s = input()
t = input()
q = int(input())

len_s = len(s)
len_t = len(t)

s_counter= [0]*(len_s+1)
s_temp = 0
for i in range(len_s):
	if s[i] == "A":
		s_temp += 1
	s_counter[i+1] = s_temp
	
t_counter= [0]*(len_t+1)
t_temp = 0
for i in range(len_t):
	if t[i] == "A":
		t_temp += 1
	t_counter[i+1] = t_temp

for _ in range(q):
	a, b, c, d = map(int, input().split())
	
	x = s_counter[b]-s_counter[a-1]
	y = t_counter[d]-t_counter[c-1]
	print(['NO', 'YES'][((x+b-a+1)-(y+d-c+1))%3==0])

Solution (Python):
S = input()
T = input()
Q = int(input())

N = len(S)
M = len(T)
accSA = [0] * (N + 1)
accSB = [0] * (N + 1)
accTA = [0] * (M + 1)
accTB = [0] * (M + 1)

for i, s in enumerate(S, start=1):
    accSA[i] = accSA[i - 1]
    accSB[i] = accSB[i - 1]
    if s == 'A':
        accSA[i] += 1
    else:
        accSB[i] += 1

for i, t in enumerate(T, start=1):
    accTA[i] = accTA[i - 1]
    accTB[i] = accTB[i - 1]
    if t == 'A':
        accTA[i] += 1
    else:
        accTB[i] += 1

ans = []
for _ in range(Q):
    leftS, rightS, leftT, rightT = map(int, input().split())

    sA = accSA[rightS] - accSA[leftS - 1]
    sB = accSB[rightS] - accSB[leftS - 1]
    tA = accTA[rightT] - accTA[leftT - 1]
    tB = accTB[rightT] - accTB[leftT - 1]

    if (sA + sB * 2) % 3 == (tA + tB * 2) % 3:
        ans.append('YES')
    else:
        ans.append('NO')

print(*ans, sep='\n')

Solution (Python):
S = input()
T = input()
Q = int(input())
qs = [tuple(map(int,input().split())) for i in range(Q)]

sas = [0]
sbs = [0]
for c in S:
    a = int(c=='A')
    sas.append(sas[-1] + a)
    sbs.append(sbs[-1] + (a^1))

tas = [0]
tbs = [0]
for c in T:
    a = int(c=='A')
    tas.append(tas[-1] + a)
    tbs.append(tbs[-1] + (a^1))

ans = []
for a,b,c,d in qs:
    sa = sas[b] - sas[a-1]
    sb = sbs[b] - sbs[a-1]
    ta = tas[d] - tas[c-1]
    tb = tbs[d] - tbs[c-1]
    s = (sb-sa)%3
    t = (tb-ta)%3
    ans.append('YES' if s==t else 'NO')
print(*ans, sep='\n')

Solution (Python):
s,t,_,*z=open(0)
v,w=[0],[0]
for c in s:v+=v[-1]+(c<'B'or 2),
for c in t:w+=w[-1]+(c<'B'or 2),
for x in z:
  a,b,c,d=map(int,x.split())
  print('YNEOS'[(v[b]-v[a-1])%3!=(w[d]-w[c-1])%3::2])

Solution (Python):
s,t,_,*z=open(0)
v,w=[0],[0]
for c in s:v+=v[-1]+(c<'B'or 2),
for c in t:w+=w[-1]+(c<'B'or 2),
for x in z:
  a,b,c,d=map(int,x.split())
  print('YNEOS'[(v[b]-v[a-1])%3!=(w[d]-w[c-1])%3::2])

Solution (Python):
S = input()
T = input()

cumSA = [0] * (len(S) + 1)
cumSB = [0] * (len(S) + 1)
cumTA = [0] * (len(T) + 1)
cumTB = [0] * (len(T) + 1)

for i, s in enumerate(S, start=1):
    cumSA[i] = cumSA[i - 1]
    cumSB[i] = cumSB[i - 1]

    if s == 'A':
        cumSA[i] += 1
    else:
        cumSB[i] += 1

for i, t in enumerate(T, start=1):
    cumTA[i] = cumTA[i - 1]
    cumTB[i] = cumTB[i - 1]

    if t == 'A':
        cumTA[i] += 1
    else:
        cumTB[i] += 1

Q = int(input())
ans = []
for _ in range(Q):
    lS, rS, lT, rT = map(int, input().split())

    sA = cumSA[rS] - cumSA[lS - 1]
    sB = cumSB[rS] - cumSB[lS - 1]
    tA = cumTA[rT] - cumTA[lT - 1]
    tB = cumTB[rT] - cumTB[lT - 1]

    if (sA - sB) % 3 == (tA - tB) % 3:
        ans.append('YES')
    else:
        ans.append('NO')

print(*ans, sep='\n')

Solution (Python):
s = input()
t = input()


d = {"A":1, "B":-1}
def make(s):
    ss = [0]
    for i in s:
        if ss[-1] == 0:
            ss.append(d[i])
        else:
            if ss[-1] == d[i]:
                ss.append(ss[-1]*-1)
            else:
                ss.append(0)
    return ss

sl = make(s)
tl = make(t)

def convert(a,b):
    if a == 0:
        return b
    if a == 1:
        c = b-a
        if c == -2:
            c = 1
        return c
    if a == -1:
        c = b-a
        if c == 2:
            c = -1
        return c

q = int(input())
ans = []
for i in range(q):
    a,b,c,d = map(int, input().split())
    sleft = sl[a-1]
    sright = sl[b]
    tleft = tl[c-1]
    tright = tl[d]
    if convert(sleft, sright) == convert(tleft, tright):
        ans.append("YES")
    else:
        ans.append("NO")
print(*ans, sep="\n")

Solution (Python):
S = input()
T = input()
q = int(input())

SS = [0]
TT = [0]
for i in S:
    if i=="A":
        SS.append(1+SS[-1])
    else:
        SS.append(2+SS[-1])
for i in T:
    if i=="A":
        TT.append(1+TT[-1])
    else:
        TT.append(2+TT[-1])

for i in range(q):
    a,b,c,d = map(int,input().split())
    if (SS[b]-SS[a-1])%3 == (TT[d]-TT[c-1])%3:
        print("YES")
    else:
        print("NO")

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(pow(10, 6))


def main():
    s = input().strip()
    t = input().strip()
    accs, acct = [0], [0]
    for i in range(len(s)):
        if s[i] == "A":
            accs.append(accs[-1]+1)
        else:
            accs.append(accs[-1]+2)
    for i in range(len(t)):
        if t[i] == "A":
            acct.append(acct[-1]+1)
        else:
            acct.append(acct[-1]+2)
    q = int(input())
    for _ in range(q):
        a, b, c, d = map(int, input().split())
        if (accs[b]-accs[a-1])%3 == (acct[d]-acct[c-1])%3:
            print("YES")
        else:
            print("NO")


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    S = list(str(input()))
    T = list(str(input()))
    q = int(input())
    S_cum = [0]
    T_cum = [0]
    for i in S:
        if i == "A":
            S_cum.append((S_cum[-1]+1)%3)
        else:
            S_cum.append((S_cum[-1]+2)%3)
    for i in T:
        if i == "A":
            T_cum.append((T_cum[-1]+1)%3)
        else:
            T_cum.append((T_cum[-1]+2)%3)
    for i in range(q):
        a,b,c,d = map(int,input().split())
        s_ = S_cum[b]-S_cum[a-1]
        t_ = T_cum[d]-T_cum[c-1]
        if s_%3 == t_%3:
            print("YES")
        else:
            print("NO")

if __name__ == "__main__":
    main()