Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin

N, Q = [int(x) for x in stdin.readline().rstrip().split()]
S = stdin.readline().rstrip()
lr_list = [list(map(int, stdin.readline().rstrip().split())) for _ in range(Q)]


for l, r in lr_list:
    S2 = S[l-1:r]
    count = 0
    #print(S2)
    flg = False
    for i in range(len(S2)):
        if S2[i] != "A":
            continue
        try:
            if S2[i+1] == "C":
                count += 1
        except:
            pass
    print(count)
        

Fixed submission:
from sys import stdin
from collections import deque

N, Q = [int(x) for x in stdin.readline().rstrip().split()]
S = stdin.readline().rstrip()
lr_list = [list(map(int, stdin.readline().rstrip().split())) for _ in range(Q)]

#result_list = []
count = 0

result_list = deque([])

for i in range(N):
    result_list.append(count)
    if S[i] != "A":
        continue
    try:
        if S[i+1] == "C":
            count += 1
    except:
       pass

for l, r in lr_list:
    print(result_list[r-1] - result_list[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
from collections import deque

N, Q = [int(x) for x in stdin.readline().rstrip().split()]
S = stdin.readline().rstrip()
lr_list = [list(map(int, stdin.readline().rstrip().split())) for _ in range(Q)]


for l, r in lr_list:
    stack = deque([])
    S2 = S[l-1:r]
    count = 0
    #print(S2)
    flg = False
    for s in S2:
        if s == "A":
            flg = True
        elif s == "C" and flg:
            count += 1
            flg = False
        else:
            flg = False
    print(count)
        

Fixed submission:
from sys import stdin
from collections import deque

N, Q = [int(x) for x in stdin.readline().rstrip().split()]
S = stdin.readline().rstrip()
lr_list = [list(map(int, stdin.readline().rstrip().split())) for _ in range(Q)]

#result_list = []
count = 0

result_list = deque([])

for i in range(N):
    result_list.append(count)
    if S[i] != "A":
        continue
    try:
        if S[i+1] == "C":
            count += 1
    except:
       pass

for l, r in lr_list:
    print(result_list[r-1] - result_list[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
from collections import deque

N, Q = [int(x) for x in stdin.readline().rstrip().split()]
S = stdin.readline().rstrip()
lr_list = [list(map(int, stdin.readline().rstrip().split())) for _ in range(Q)]


for l, r in lr_list:
    stack = deque([])
    S2 = S[l-1:r]
    count = 0
    #print(S2)
    for s in S2:
        # スタックが空なら
        if not stack:
            stack.append(s)
        # スタックの一番上がAなら
        elif stack[-1] == "A" and s == "C":
            stack.pop()
            count += 1
        else:
            stack.append(s)
    print(count)
        

Fixed submission:
from sys import stdin
from collections import deque

N, Q = [int(x) for x in stdin.readline().rstrip().split()]
S = stdin.readline().rstrip()
lr_list = [list(map(int, stdin.readline().rstrip().split())) for _ in range(Q)]

#result_list = []
count = 0

result_list = deque([])

for i in range(N):
    result_list.append(count)
    if S[i] != "A":
        continue
    try:
        if S[i+1] == "C":
            count += 1
    except:
       pass

for l, r in lr_list:
    print(result_list[r-1] - result_list[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin

N, Q = [int(x) for x in stdin.readline().rstrip().split()]
S = stdin.readline().rstrip()
lr_list = [list(map(int, stdin.readline().rstrip().split())) for _ in range(Q)]

for l, r in lr_list:
    S2 = S[l-1:r]
    #print(S2)
    S3 = S2.replace("AC", "")
    print( (len(S2) - len(S3)) // 2 )

Fixed submission:
from sys import stdin
from collections import deque

N, Q = [int(x) for x in stdin.readline().rstrip().split()]
S = stdin.readline().rstrip()
lr_list = [list(map(int, stdin.readline().rstrip().split())) for _ in range(Q)]

#result_list = []
count = 0

result_list = deque([])

for i in range(N):
    result_list.append(count)
    if S[i] != "A":
        continue
    try:
        if S[i+1] == "C":
            count += 1
    except:
       pass

for l, r in lr_list:
    print(result_list[r-1] - result_list[l-1])