Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int, input().split())
S = list(input())
lr = [list(map(int,input().split())) for i in range(Q)]
bef = ''
pos = []
for idx,val in enumerate(S):
    if val == 'C' and bef == 'A':
        pos.append((idx -1) * 10 + idx)
    bef = val

cnt = 0
for i in range(Q):
    min_v = (lr[i][0] - 1) * 10 + lr[i][0]
    max_v = (lr[i][1] - 2) * 10 + (lr[i][1] - 1)
    n = sum(1 for x in pos if x >= min_v and x <= max_v)
    print(n)



Fixed submission:
N,Q = map(int, input().split())
S = input()
lrs = [list(map(int, input().split())) for i in range(Q)]

total = []
total.append(0)
for i in range(1, N):
    if S[i - 1] == 'A' and S[i] == 'C':
        total.append(1)
    else:
        total.append(0)
totsum = 0
for idx, val in enumerate(total):
    totsum += val
    total[idx] = totsum

for lr in lrs:
    print(total[lr[1] - 1] - total[lr[0] - 1])

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int, input().split())
S = input()
lr = [list(map(int,input().split())) for i in range(Q)]

for i in range(Q):
    s_tmp = S[max(lr[i][0] - 1, 0):lr[i][1]]
    s_tmp_aft = S[lr[i][1]:]
    s_tmp_aft_2 = S[lr[i][1] - 1:]
    cnt =s_tmp.count('AC') - max(s_tmp_aft.count('AC'), s_tmp_aft_2.count('AC'))
    if cnt > 0:
        print(cnt)
    else:
        print(0)



Fixed submission:
N,Q = map(int, input().split())
S = input()
lrs = [list(map(int, input().split())) for i in range(Q)]

total = []
total.append(0)
for i in range(1, N):
    if S[i - 1] == 'A' and S[i] == 'C':
        total.append(1)
    else:
        total.append(0)
totsum = 0
for idx, val in enumerate(total):
    totsum += val
    total[idx] = totsum

for lr in lrs:
    print(total[lr[1] - 1] - total[lr[0] - 1])

Buggy python submission (verdict: Runtime Error):
N,Q = map(int, input().split())
S = input()
lr = [list(map(int,input().split())) for i in range(Q)]

cnt_max = S.count('AC')
d = 0
for i in range(Q):
    d = lr[i][1] - lr[i][0]
    loss = 0
    if d * 2 > len(S):
        s_tmp = S[max(lr[i][0] - 1, 0):lr[i][1]]
        print(s_tmp.count('AC'))
    else:
        s_tmp_bef = S[:lr[i][0] - 1]
        s_tmp_aft = S[lr[i][1]:]
        if s_tmp_bef[-1] == 'A' and S[lr[i][0] - 1] == 'C':
            loss += 1
        if s_tmp_aft[0] == 'C' and S[lr[i][1] - 1] == 'A':
            loss += 1
        print(cnt_max - loss - s_tmp_bef.count('AC') - s_tmp_aft.count('AC'))


Fixed submission:
N,Q = map(int, input().split())
S = input()
lrs = [list(map(int, input().split())) for i in range(Q)]

total = []
total.append(0)
for i in range(1, N):
    if S[i - 1] == 'A' and S[i] == 'C':
        total.append(1)
    else:
        total.append(0)
totsum = 0
for idx, val in enumerate(total):
    totsum += val
    total[idx] = totsum

for lr in lrs:
    print(total[lr[1] - 1] - total[lr[0] - 1])

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int, input().split())
S = input()
lr = [list(map(int,input().split())) for i in range(Q)]

for i in range(Q):
    s_tmp = S[max(lr[i][0] - 1, 0):lr[i][1]]
    print(s_tmp.count('AC'))


Fixed submission:
N,Q = map(int, input().split())
S = input()
lrs = [list(map(int, input().split())) for i in range(Q)]

total = []
total.append(0)
for i in range(1, N):
    if S[i - 1] == 'A' and S[i] == 'C':
        total.append(1)
    else:
        total.append(0)
totsum = 0
for idx, val in enumerate(total):
    totsum += val
    total[idx] = totsum

for lr in lrs:
    print(total[lr[1] - 1] - total[lr[0] - 1])

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int, input().split())
S = list(input())
lr = [list(map(int,input().split())) for i in range(Q)]
bef = ''
pos = []
for idx,val in enumerate(S):
    if val == 'C' and bef == 'A':
        pos.append((idx -1) * 10 + idx)
    bef = val

cnt = 0
for i in range(Q):
    min_v = (lr[i][0] - 1) * 10 + lr[i][0]
    max_v = (lr[i][1] - 2) * 10 + (lr[i][1] - 1)
    for p in pos:
        if p >= min_v and p <= max_v:
            cnt += 1
    print(cnt)
    cnt = 0



Fixed submission:
N,Q = map(int, input().split())
S = input()
lrs = [list(map(int, input().split())) for i in range(Q)]

total = []
total.append(0)
for i in range(1, N):
    if S[i - 1] == 'A' and S[i] == 'C':
        total.append(1)
    else:
        total.append(0)
totsum = 0
for idx, val in enumerate(total):
    totsum += val
    total[idx] = totsum

for lr in lrs:
    print(total[lr[1] - 1] - total[lr[0] - 1])

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int, input().split())
S = list(input())
lr = [list(map(int,input().split())) for i in range(Q)]
S_tmp = []
bef = ''
cnt = 0
for i in range(Q):
    for j in range(lr[i][0] - 1,lr[i][1]):
        S_tmp.append(S[j])
    for s in S_tmp:
        if s == 'C' and bef == 'A':
            cnt += 1
        bef = s
    print(cnt)
    cnt = 0
    S_tmp.clear()



Fixed submission:
N,Q = map(int, input().split())
S = input()
lrs = [list(map(int, input().split())) for i in range(Q)]

total = []
total.append(0)
for i in range(1, N):
    if S[i - 1] == 'A' and S[i] == 'C':
        total.append(1)
    else:
        total.append(0)
totsum = 0
for idx, val in enumerate(total):
    totsum += val
    total[idx] = totsum

for lr in lrs:
    print(total[lr[1] - 1] - total[lr[0] - 1])