Buggy python submission (verdict: Time Limit Exceeded):
import re
N, Q = map(int, input().split())
S = input()
iter = re.finditer("AC", S)
indices_C = [m.start(0)+1 for m in iter]
total = S.count("AC")

dec_l_list = []
dec_l = 0
dec_r_list = []
dec_r = total
for i in range(N):
    if i in indices_C:
        dec_l += 1
        dec_r -= 1
    dec_l_list.append(dec_l)
    dec_r_list.append(dec_r)

for i in range(Q):
    l, r = map(int, input().split())
    print(total - dec_l_list[l-1] - dec_r_list[r-1])

Fixed submission:
#import re
N, Q = map(int, input().split())
S = input()
#iter = re.finditer("AC", S)
#indices_C = [m.start(0)+1 for m in iter]
#total = len(indices_C)
#
#dec_l_list = []
#dec_l = 0
#dec_r_list = []
#dec_r = total
#for i in range(N):
#    if i in indices_C:
#        dec_l += 1
#        dec_r -= 1
#    dec_l_list.append(dec_l)
#    dec_r_list.append(dec_r)

flag = False
dec_l_list = []
total = 0
for i in range(N):
    if flag and S[i] == "C":
        total += 1
    dec_l_list.append(total)
    flag = True if S[i] == "A" else False
dec_r_list = [total - x for x in dec_l_list]

for i in range(Q):
    l, r = map(int, input().split())
    print(total - dec_l_list[l-1] - dec_r_list[r-1])

Buggy python submission (verdict: Time Limit Exceeded):
import re
N, Q = map(int, input().split())
S = input()
iter = re.finditer("AC", S)
indices_A = [m.start(0) for m in iter]
indices_C = [a+1 for a in indices_A]
total = S.count("AC")

dec_l_list = []
dec_l = 0
for i in range(N):
    if i in indices_C:
        dec_l-= 1
    dec_l_list.append(dec_l)

dec_r_list = []
dec_r = 0
for i in reversed(range(N)):
    if i in indices_A:
        dec_r-= 1
    dec_r_list.append(dec_r)
dec_r_list.reverse()

#print(dec_l_list)
#print(dec_r_list)

for i in range(Q):
    l, r = map(int, input().split())
    print(total + dec_l_list[l-1] + dec_r_list[r-1])

Fixed submission:
#import re
N, Q = map(int, input().split())
S = input()
#iter = re.finditer("AC", S)
#indices_C = [m.start(0)+1 for m in iter]
#total = len(indices_C)
#
#dec_l_list = []
#dec_l = 0
#dec_r_list = []
#dec_r = total
#for i in range(N):
#    if i in indices_C:
#        dec_l += 1
#        dec_r -= 1
#    dec_l_list.append(dec_l)
#    dec_r_list.append(dec_r)

flag = False
dec_l_list = []
total = 0
for i in range(N):
    if flag and S[i] == "C":
        total += 1
    dec_l_list.append(total)
    flag = True if S[i] == "A" else False
dec_r_list = [total - x for x in dec_l_list]

for i in range(Q):
    l, r = map(int, input().split())
    print(total - dec_l_list[l-1] - dec_r_list[r-1])

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
S = input()
for i in range(Q):
    l, r = map(int, input().split())
    s = S[l-1:r]
    print(s.count("AC"))

Fixed submission:
#import re
N, Q = map(int, input().split())
S = input()
#iter = re.finditer("AC", S)
#indices_C = [m.start(0)+1 for m in iter]
#total = len(indices_C)
#
#dec_l_list = []
#dec_l = 0
#dec_r_list = []
#dec_r = total
#for i in range(N):
#    if i in indices_C:
#        dec_l += 1
#        dec_r -= 1
#    dec_l_list.append(dec_l)
#    dec_r_list.append(dec_r)

flag = False
dec_l_list = []
total = 0
for i in range(N):
    if flag and S[i] == "C":
        total += 1
    dec_l_list.append(total)
    flag = True if S[i] == "A" else False
dec_r_list = [total - x for x in dec_l_list]

for i in range(Q):
    l, r = map(int, input().split())
    print(total - dec_l_list[l-1] - dec_r_list[r-1])