Buggy python submission (verdict: Time Limit Exceeded):

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

length = len(lrs)
count = [0] * length
for i in range(len(S) - 1):
    if S[i] != 'A':
        continue
    if S[i+1] != 'C':
        continue
    for j in range(length):
        lrsj = lrs[j]
        if lrsj[0] <= i+1 and i < lrsj[1] and i+1 != lrsj[1]:
            count[j] += 1

for c in count:
    print(c)


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

cs = [0] * N
s = 0
for i in range(N-1):
    if S[i] == 'A' and S[i+1] == 'C':
        s += 1
    cs[i+1] = s 

for l, r in lrs:
    print(cs[r-1] - cs[l-1])

Buggy python submission (verdict: Time Limit Exceeded):

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

length = len(lrs)
count = [0] * length
for i in range(len(S) - 1):
    if S[i] != 'A':
        continue
    if S[i+1] != 'C':
        continue
    for j in range(length):
        if lrs[j][0] <= i+1 and i < lrs[j][1] and i+1 != lrs[j][1]:
            count[j] += 1

for c in count:
    print(c)


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

cs = [0] * N
s = 0
for i in range(N-1):
    if S[i] == 'A' and S[i+1] == 'C':
        s += 1
    cs[i+1] = s 

for l, r in lrs:
    print(cs[r-1] - cs[l-1])

Buggy python submission (verdict: Time Limit Exceeded):

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

length = len(lrs)
count = [0] * length
for i in range(len(S) - 1):
    c = S[i] == 'A'
    nc = S[i+1] == 'C'
    if c and nc:
        for j in range(length):
            if lrs[j][0] <= i+1 and i < lrs[j][1] and i+1 != lrs[j][1]:
                count[j] += 1

for c in count:
    print(c)


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

cs = [0] * N
s = 0
for i in range(N-1):
    if S[i] == 'A' and S[i+1] == 'C':
        s += 1
    cs[i+1] = s 

for l, r in lrs:
    print(cs[r-1] - cs[l-1])

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

ps = [S[i] == 'A' and S[i+1] == 'C' for i in range(len(S) - 1)]

for l, r in lrs:
    p = len([b for b in ps[l-1:r] if b])
    if r < len(ps)-1 and not ps[r]:
        p -= 1
    print(p)

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

cs = [0] * N
s = 0
for i in range(N-1):
    if S[i] == 'A' and S[i+1] == 'C':
        s += 1
    cs[i+1] = s 

for l, r in lrs:
    print(cs[r-1] - cs[l-1])

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

for l, r in lrs:
    s = S[l-1:r]
    length = len(s)
    count = 0
    for i in range(length - 1):
        if s[i] == 'A' and s[i+1] == 'C':
            count += 1
    print(count)

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

cs = [0] * N
s = 0
for i in range(N-1):
    if S[i] == 'A' and S[i+1] == 'C':
        s += 1
    cs[i+1] = s 

for l, r in lrs:
    print(cs[r-1] - cs[l-1])