Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n,q = list(map(int,input().split(' ')))
    s = input()
    s_tr =[]
    q_list = []
    for i in range(n-1):
        if len(s_tr) !=i:
            continue
        if s[i] == 'A' and s[i+1] =='C':
            s_tr.append(1)
            s_tr.append(1)
        else:
            s_tr.append(0)
    if len(s_tr) < n:
        s_tr.append(0)

 

    for i in range(q):
        l,r = list(map(int,input().split(' ')))
        if l ==1 or r ==n:
            print(sum(s_tr[l-1:r])//2)
        else:
            if s_tr[l-2] ==0 or s_tr[r] ==0:
                print(sum(s_tr[l-1:r])//2)
            else:
                print(sum(s_tr[l-1:r])//2 -1)

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    n,q = list(map(int,input().split(' ')))
    s = input()
    tmp = 0
    s_tr =[]
    q_list = []
    for i in range(n-1):
        if len(s_tr) !=i:
            continue
        if s[i] == 'A' and s[i+1] =='C':
            tmp += 1
            s_tr.append(tmp)
            s_tr.append(tmp)
        else:
            s_tr.append(tmp)
    if len(s_tr) != n:
        s_tr.append(tmp)
    for i in range(q):
        q_list.append(list(map(int,input().split(' '))))

    for l,r in q_list:
        if s_tr[r-1] ==s_tr[r-2]:
            if l ==1:
                print(s_tr[r-1])
            else:
                print(s_tr[r-1]-s_tr[l-2])
        else:
            if l ==1:
                print(s_tr[r-1] - 1)
            else:
                print(s_tr[r-1] - s_tr[l-2] -1 )

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n,q = list(map(int,input().split(' ')))
    s = input()
    s_tr =[]
    q_list = []
    for i in range(n-1):
        if len(s_tr) !=i:
            continue
        if s[i] == 'A' and s[i+1] =='C':
            s_tr.append(1)
            s_tr.append(1)
        else:
            s_tr.append(0)
    if len(s_tr) < n:
        s_tr.append(0)

    for i in range(q):
        q_list.append(list(map(int,input().split(' '))))

    for l,r in q_list:
        if l ==1 or r ==n:
            print(sum(s_tr[l-1:r])//2)
        else:
            if s_tr[l-2] ==0 or s_tr[r] ==0:
                print(sum(s_tr[l-1:r])//2)
            else:
                print(sum(s_tr[l-1:r])//2 -1)

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    n,q = list(map(int,input().split(' ')))
    s = input()
    tmp = 0
    s_tr =[]
    q_list = []
    for i in range(n-1):
        if len(s_tr) !=i:
            continue
        if s[i] == 'A' and s[i+1] =='C':
            tmp += 1
            s_tr.append(tmp)
            s_tr.append(tmp)
        else:
            s_tr.append(tmp)
    if len(s_tr) != n:
        s_tr.append(tmp)
    for i in range(q):
        q_list.append(list(map(int,input().split(' '))))

    for l,r in q_list:
        if s_tr[r-1] ==s_tr[r-2]:
            if l ==1:
                print(s_tr[r-1])
            else:
                print(s_tr[r-1]-s_tr[l-2])
        else:
            if l ==1:
                print(s_tr[r-1] - 1)
            else:
                print(s_tr[r-1] - s_tr[l-2] -1 )

if __name__ == '__main__':
    main()