Problem:


Score : 300 points

Problem Statement

You are given a string S of length N consisting of A, C, G and T. Answer the following Q queries:

Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does AC occurs as a substring?

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and  (the empty string), but not AC.

Constraints

2 \leq N \leq 10^5

1 \leq Q \leq 10^5

S is a string of length N.

Each character in S is A, C, G or T.

1 \leq l_i < r_i \leq N

Input

Input is given from Standard Input in the following format:

N Q
S
l_1 r_1
:
l_Q r_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

8 3
ACACTACG
3 7
2 3
1 8

Sample Output 1

2
0
3

Query 1: the substring of S starting at index 3 and ending at index 7 is ACTAC. In this string, AC occurs twice as a substring.

Query 2: the substring of S starting at index 2 and ending at index 3 is CA. In this string, AC occurs zero times as a substring.

Query 3: the substring of S starting at index 1 and ending at index 8 is ACACTACG. In this string, AC occurs three times as a substring.



Sample input:
8 3
ACACTACG
3 7
2 3
1 8


Sample output:
2
0
3

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n, q = list(map(int, input().rstrip('\n').split()))
    s = str(input().rstrip('\n'))
    ls = [0] * n
    cnt = 0
    for i in range(1, n):
        if s[i] == "C" and s[i-1] == "A":
            cnt += 1
        ls[i] = cnt
    for i in range(q):
        l, r = list(map(int, input().rstrip('\n').split()))
        print(ls[r-1] - ls[l-1])


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    def iterate_tokens():
        for line in stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))
    Q = int(next(tokens))
    S = next(tokens)
    L = [int()] * Q
    R = [int()] * Q
    for i in range(Q):
        L[i] = int(next(tokens)) - 1
        R[i] = int(next(tokens)) - 1
    AC = [int()] * N
    pre = None
    cnt = 0
    for i, c in enumerate(S):
        if c == "A":
            pre = "A"
        elif pre == "A" and c == "C":
            cnt += 1
            pre = None
        elif pre == "A":
            pre = None
        AC[i] = cnt
    for i in range(Q):
        print(AC[R[i]] - AC[L[i]])
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def iterate_tokens():
    for line in stdin:
        for word in line.split():
            yield word


def main():
    tokens = iterate_tokens()
    N = int(next(tokens))
    Q = int(next(tokens))
    S = next(tokens)
    L = [int()] * Q
    R = [int()] * Q
    for i in range(Q):
        L[i] = int(next(tokens)) - 1
        R[i] = int(next(tokens)) - 1
    AC = [int()] * N
    pre = None
    cnt = 0
    for i, c in enumerate(S):
        if c == "A":
            pre = "A"
        elif pre == "A" and c == "C":
            cnt += 1
            pre = None
        elif pre == "A":
            pre = None
        AC[i] = cnt
    for i in range(Q):
        print(AC[R[i]] - AC[L[i]])
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def iterate_tokens():
    for line in stdin:
        for word in line.split():
            yield word


def main():
    tokens = iterate_tokens()
    N = int(next(tokens))
    Q = int(next(tokens))
    S = next(tokens)
    L = [0] * Q
    R = [0] * Q
    for i in range(Q):
        L[i] = int(next(tokens)) - 1
        R[i] = int(next(tokens)) - 1
    AC = [0] * N
    pre = None
    cnt = 0
    for i, c in enumerate(S):
        if c == "A":
            pre = "A"
        elif pre == "A" and c == "C":
            cnt += 1
            pre = None
        elif pre == "A":
            pre = None
        AC[i] = cnt
    for i in range(Q):
        print(AC[R[i]] - AC[L[i]])
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def iterate_tokens():
    for line in stdin:
        for word in line.split():
            yield word


def main():
    tokens = iterate_tokens()
    next(tokens)
    Q = int(next(tokens))
    S = next(tokens)
    L = []
    R = []
    for i in range(Q):
        L.append(int(next(tokens)))
        R.append(int(next(tokens)))
    AC = []
    pre = None
    cnt = 0
    for c in S:
        if c == "A":
            pre = "A"
        elif pre == "A" and c == "C":
            cnt += 1
            pre = None
        elif pre == "A":
            pre = None
        AC.append(cnt)
    for l, r in zip(L, R):
        print(AC[r - 1] - AC[l - 1])
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def iterate_tokens():
    for line in stdin:
        for word in line.split():
            yield word


def main():
    tokens = iterate_tokens()
    next(tokens)
    Q = int(next(tokens))
    S = next(tokens)
    L = []
    R = []
    for i in range(Q):
        L.append(int(next(tokens)) - 1)
        R.append(int(next(tokens)) - 1)
    AC = []
    pre = None
    cnt = 0
    for c in S:
        if c == "A":
            pre = "A"
        elif pre == "A" and c == "C":
            cnt += 1
            pre = None
        elif pre == "A":
            pre = None
        AC.append(cnt)
    for i in range(Q):
        print(AC[R[i]] - AC[L[i]])
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def iterate_tokens():
    for line in stdin:
        for word in line.split():
            yield word


def main():
    tokens = iterate_tokens()
    next(tokens)
    Q = int(next(tokens))
    S = next(tokens)
    L = []
    R = []
    for i in range(Q):
        L.append(int(next(tokens)) - 1)
        R.append(int(next(tokens)) - 1)
    AC = []
    pre = None
    cnt = 0
    for c in S:
        if c == "A":
            pre = "A"
        elif pre == "A" and c == "C":
            cnt += 1
            pre = None
        elif pre == "A":
            pre = None
        AC.append(cnt)
    for l, r in zip(L, R):
        print(AC[r] - AC[l])
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    lines = stdin.readlines()
    S = lines[1]
    lr = [[int(j) - 1 for j in i.split()] for i in lines[2:]]
    AC = []
    pre = None
    cnt = 0
    for c in S:
        if c == "A":
            pre = "A"
        elif pre == "A" and c == "C":
            cnt += 1
            pre = None
        elif pre == "A":
            pre = None
        AC.append(cnt)
    for l, r in lr:
        print(AC[r] - AC[l])
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    lines = stdin.readlines()
    S = lines[1]
    lr = [[int(j) - 1 for j in i.split()] for i in lines[2:]]
    AC = [0]
    pre = 0
    for c1, c2 in zip(S[:], S[1:]):
        if c1 == "A" and c2 == "C":
            pre += 1
        AC.append(pre)
    for l, r in lr:
        print(AC[r] - AC[l])
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    lines = stdin.readlines()
    N = int(lines[0].split()[0])
    S = lines[1]
    lr = [[int(j) - 1 for j in i.split()] for i in lines[2:]]
    AC = [0] * N
    pre = 0
    for i, c1, c2 in zip(range(1, N), S[:], S[1:]):
        if c1 == "A" and c2 == "C":
            pre += 1
        AC[i] = pre
    for l, r in lr:
        print(AC[r] - AC[l])
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    lines = stdin.readlines()
    N = int(lines[0].split()[0])
    S = lines[1]
    lr = [[int(j) - 1 for j in i.split()] for i in lines[2:]]
    AC = [0] * N
    pre = 0
    for i, c1, c2 in zip(range(1, N), S[:], S[1:]):
        AC[i] += pre
        if c1 == "A" and c2 == "C":
            AC[i] += 1
        pre = AC[i]
    for l, r in lr:
        print(AC[r] - AC[l])
    return


main()

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = list(input())

encount_func = {}
count = 0
encount_func[0] = 0
for i in range(1,N):
    check_str = S[i-1] + S[i]
    #print(check_str)
    if check_str == "AC":
        count += 1
        encount_func[i] = count
    else:
        encount_func[i] = count

#print(encount_func)
#for key in encount_func:
#    print(encount_func[key])

ans = []
for _ in range(Q):
    L, R = map(int, input().split())
    ans.append(encount_func[R-1] - encount_func[L-1])

for i in range(Q):
    print(ans[i])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
q =[]
temp = [0] * (N+1)
tmp = 0
for i in range(1,N):
    if S[i-1] == "A":
        if S[i] == "C":
            tmp +=1
    temp[i+1] = tmp
for j in range(Q):
    inp = input().split()
    print(temp[int(inp[1])] - temp[int(inp[0])])

Submission status: Accepted

Solution code:
n,q=map(int,input().split())
s=input()+"P"
A=[list(map(int,input().split())) for i in range(q)]
B=[0]*(n+1)
cnt=0
for i in range(1,n):
    if s[i-1]+s[i]=="AC":cnt +=1
    B[i+1]=cnt

for l,r in A:print(B[r]-B[l])

Submission status: Accepted

Solution code:
n,q=map(int,input().split()) 
s=input()
area=[]
for i in range(q):
    start,end=map(int,input().split())
    area.append([start,end])
num=[0,0]
total=0
for i in range(n-1):
    if s[i]=="A" and s[i+1]=="C":
        total+=1
    num.append(total)
for i in range(q):
    print(num[area[i][1]]-num[area[i][0]])

Submission status: Accepted

Solution code:
import bisect

n, q = map(int,input().split())
s = input()
lr = [list(map(int,input().split())) for _ in range(q)]
AC_index = []

for index in range(len(s)-1):
  if s[index:index+2] == "AC":
    AC_index.append(index)

for i in range(len(lr)):
  left_index = bisect.bisect_left(AC_index,lr[i][0]-1)
  right_index = bisect.bisect_left(AC_index,lr[i][1]-1)
  print(right_index-left_index)

Submission status: Accepted

Solution code:
import bisect
n,q=map(int,input().split())
s=input()
lr=[[int(i) for i in input().split()] for j in range(q)]

li=[0]*(n-1)
check=0
for i in range(n-1):
    if s[i]=='A' and s[i+1]=='C':
        check+=1
    li[i]=check
for l,r in lr:
    if l>=2:
        print(li[r-2]-li[l-2])
    else:
        print(li[r-2])

Submission status: Accepted

Solution code:
n,q = map(int, input().split())
s = input()
t = [0]*(n+1)
for i in range(n):
    if s[i:i+2]=="AC":
        t[i+1] = t[i] + 1
    else:
        t[i+1] = t[i]

for i in range(q):
    l,r = map(int, input().split())
    print(t[r-1]-t[l-1])

Submission status: Accepted

Solution code:
n,q=map(int,input().split())
s=input()
a=[0]*n
for i in range(n-1):
  if s[i:i+2]=="AC":
    a[i]=a[i-1]+1
  else:
    a[i]=a[i-1]
for i in range(q):
  l,r=map(int,input().split())
  if l==1:
    print(a[r-2])
  else:
    print(a[r-2]-a[l-2])

Submission status: Accepted

Solution code:
n,q=map(int,input().split())
s=input()
a=[0]
for i in range(1,len(s)):
  a.append(a[i-1]+(s[i-1:i+1]=='AC'))
for i in range(q):
  l,r=map(int,input().split())
  print(a[r-1]-a[l-1])

Submission status: Accepted

Solution code:
N,Q=map(int,input().split())
S=list(input())
C=[0]
for i in range(1,N):
  if S[i-1]=="A" and S[i]=="C":
    C.append(C[i-1]+1)
  else:
    C.append(C[i-1])

for _ in range(Q):
  l,r=map(int,input().split())
  print(C[r-1]-C[l-1])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
C = [0] * len(S)
for i, c in enumerate(S):
    if i != 0 and c == 'C' and S[i-1] == 'A':
        C[i] += 1
    C[i] += C[i - 1]
ans = []
for i in range(Q):
    l, r = map(int, input().split())
    ans.append(C[r-1] - C[l-1])
for i in ans:
    print(i)

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
s = input()
ac = [0]*n
for i in range(n-1):ac[i+1] = ac[i] + (s[i:i+2]=="AC")
for i in range(q):
    l,r=map(int,input().split())
    print(ac[r-1]-ac[l-1])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
lr = [[int(i) for i in input().split()] for _ in range(Q)]

ac = [0] * N
for i in range(N-1) :
    if S[i:i+2] == 'AC' :
        ac[i] += 1
    ac[i+1] += ac[i]
ac = [0] + ac
    
for l, r in lr :
    print(ac[r-1]-ac[l-1])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
Si = [0]*(N+1)
for i in range(1, N):
    if S[i-1] == 'A' and S[i] == 'C':
        Si[i+1] = Si[i] + 1
    else:
        Si[i+1] = Si[i]
for _ in range(Q):
    l, r = map(int, input().split())
    print(Si[r]-Si[l])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
s = input()
AClist = [0] * N

for i in range(N - 1):
  AClist[i + 1] = AClist[i] + int(s[i:i + 2] == "AC")
  
for i in range(Q):
  l, r = map(int, input().split())
  print(AClist[r - 1] - AClist[l - 1])

Submission status: Accepted

Solution code:
N , Q = map(int,input().split())
S = list(input())
slice_list = []
for _ in range(Q):
    l , r =map(int,input().split())
    slice_list.append([l - 1,r - 1])
cnt_list = [0] * (N + 1)

for i in range(N - 1):
    if S[i] == "A" and S[i + 1] == "C":
        cnt_list[i + 1] = cnt_list[i] + 1
    else:
        cnt_list[i + 1] = cnt_list[i]

for i in range(Q):

    print(cnt_list[slice_list[i][1]] - cnt_list[slice_list[i][0]])

Submission status: Accepted

Solution code:
n, q = map(int, input().split())

s = input()

count = [0] * n

for i in range(n) :
    if s[i-1] + s[i] == 'AC' :
        count[i] = count[i-1] + 1
    else :
        count[i] = count[i-1]

for j in range(0, q) :
    l, r = map(int, input().split())
    print(count[r-1] - count[l-1])

Submission status: Accepted

Solution code:
N,Q=map(int,input().split())
S=input()
a=[0]
for i in range(1,N):
    if S[i-1]+S[i]=='AC':
        a.append(1)
    else:
        a.append(0)
#print(a)
b=[]
sum=0
b.append(sum)
for x in a:
    sum+=x
    b.append(sum)
#print(b)
for i in range(Q):
    l,r=map(int,input().split())
    print(b[r]-b[l])

Submission status: Accepted

Solution code:
N,Q=map(int,input().split())
S=list(str(input()))
lst=[[0,0] for _ in range(Q)]
for i in range(Q):
  lst[i]=list(map(int,input().split()))
tmp=[0]*N
for i in range(1,N):
  if S[i-1]=='A' and S[i]=='C':
    tmp[i]=1+tmp[i-1]
  else:
    tmp[i]=tmp[i-1]
for i in range(Q):
  a,b=lst[i][0],lst[i][1]
  print(tmp[b-1]-tmp[a-1])

Submission status: Accepted

Solution code:
n, q = map(int, input().split(' '))
s = str(input())
checker = [0]
counter = 0
for i in range(1, n):
    if s[i-1] == 'A' and s[i] =='C':
        counter += 1
    checker.append(counter)
result = []
for i in range(q):
    L, r = map(int, input().split(' '))
    a = checker[L-1]
    b = checker[r-1]
    result.append(b-a)
for i in result:
    print(i)

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
str = input()
length = [list(map(int, input().split())) for _ in range(Q)]

ans = [0 for _ in range(N+1)]
for i in range(1, N):
    if (str[i-1] == "A"):
        if (str[i] == "C"):
            ans[i] = ans[i-1] + 1
        else:
            ans[i] = ans[i-1]
    else:
        ans[i] = ans[i-1]
        ans[i+1] = ans[i-1]
        i += 1

for l1, l2 in length:
    # print(ans)
    print(ans[l2-1] - ans[l1-1])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
str = input()
length = [list(map(int, input().split())) for _ in range(Q)]

ans = [0 for _ in range(N)]
for i in range(1, N):
    if (str[i-1] == "A"):
        if (str[i] == "C"):
            ans[i] = ans[i-1] + 1
        else:
            ans[i] = ans[i-1]
    else:
        ans[i] = ans[i-1]
        if i != N-1:
            ans[i+1] = ans[i-1]
        i += 1

for l1, l2 in length:
    # print(ans)
    print(ans[l2-1] - ans[l1-1])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S =input()
length = [list(map(int, input().split())) for _ in range(Q)]
AC_list = [0]*N
count = 0
for i in range(1,N):
  if (S[i-1] + S[i]) == "AC":
    count +=1
  AC_list[i] = count

for l, r in length:
  print(AC_list[r-1] - AC_list[l-1])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S =input()
AC_list = [0]*N
count = 0
for i in range(1,N):
  if (S[i-1] + S[i]) == "AC":
    count +=1
  AC_list[i] = count

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

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
str = input()
length = [list(map(int, input().split())) for _ in range(Q)]

ans = [0 for _ in range(N)]
for i in range(1, N):
    if (str[i-1] == "A") and (str[i] == "C"):
        ans[i] = ans[i-1] + 1
    else:
        ans[i] = ans[i-1]

for l1, l2 in length:
    # print(ans)
    print(ans[l2-1] - ans[l1-1])

Submission status: Accepted

Solution code:
import re, bisect

N, Q = map(int, input().split())
S = input()

l = []
r = []
for _ in range(Q):
    l_tmp, r_tmp = map(int, input().split())
    l.append(l_tmp - 1)
    r.append(r_tmp - 1)

# SのうちACが現れるインデックスを全て保存しておき、そこを含むかどうかを判定する

match_index_list = [m.span() for m in re.finditer('AC', S)]
match_first_index_list = []

for i in range(len(match_index_list)):
    match_first_index_list.append(match_index_list[i][0])

match_last_index_list = [x+1 for x in match_first_index_list]

for question in range(Q):
    print(bisect.bisect_right(match_last_index_list, r[question]) - bisect.bisect_left(match_first_index_list, l[question]))

Submission status: Accepted

Solution code:
n,q=map(int,input().split())
s=input()
a=[0]*n
for i in range(n-1):
    if s[i]=="A" and s[i+1]=="C":
        a[i+1]=1
b=[0]*n        
b[0]=a[0]
for i in range(1,n):
    b[i]=b[i-1]+a[i]
for i in range(q):
    l,r=map(int,input().split())
    print(b[r-1]-b[l-1])

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
s = input()

cnt = 0
ans = [0]
for i in range(n-1):
    if s[i:i+2] == 'AC':
        cnt += 1
    ans.append(cnt)

for i in range(q):
    l, r = map(int, input().split())
    print(ans[r-1]-ans[l-1])

Submission status: Accepted

Solution code:
# 1文字目からi文字目までに何個ACがあるかを調べておき
# その情報を使い回す
n,q=map(int,input().split())
s=input()
AC=[0]*(n+1)

for i in range(n):
    if s[i:i+2]=="AC":
        AC[i+1]=AC[i]+1
    else:
        AC[i+1]=AC[i]    

for _ in range(q):
    l,r=map(int,input().split())
    print(AC[r-1]-AC[l-1])

Submission status: Accepted

Solution code:
import re
N, Q = map(int, input().split())
S = input()
query = [list(map(int, input().split())) for _ in range(Q)]
s = re.sub(r"AC", "0", S)
accum = [0 for _ in range(N)]
for i in range(1, N):
    accum[i] = accum[i-1]
    if S[i-1]=="A" and S[i]=="C":
        accum[i] += + 1

for l,r in query:
    print(accum[r-1]-accum[l-1])

Submission status: Accepted

Solution code:
N,Q = map(int,input().split())
S = input()
V = [0]


for i in range(N):
    if S[i:i+2] == 'AC':
        V.append(V[-1]+1)
    else:
        V.append(V[-1])

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

Submission status: Accepted

Solution code:
n,q=map(int,input().split())
s=input()
t=[0]*(n+1)
for i in range(n):
  t[i+1]=t[i]+(1 if s[i:i+2] == "AC" else 0)
for i in range(q):
  l,r=map(int,input().split())
  print(t[r-1]-t[l-1])

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
s = input()
ranges = [list(map(int, input().split())) for _ in range(q)]

cnt = 0
cnt_list = []
# インデックスの要素 = インデックスから始まる文字が"AC"
for i in range(n):
    if i < 1:
        cnt_list.append(0)
        continue
    if s[i-1:i+1] == "AC":
        cnt += 1
    cnt_list.append(cnt)

for start, end in ranges:
    print(cnt_list[end-1] - cnt_list[start-1])

Submission status: Accepted

Solution code:
n, q = (int(x) for x in input().split())
s = input()
s = [s[i] for i in range(n)]
lr = []
for i in range(q):
    lr.append([int(x) for x in input().split()])
ac = [0 for _ in range(n)]
i, c = 0, 0
for i in range(n-1):
    if s[i] == "A" and s[i+1] == "C":
        ac[i+1] = ac[i] + 1
    else:
        ac[i+1] = ac[i]
for p in lr:
    l, r = p[0] - 1, p[1] - 1
    print(int(ac[r] - ac[l]))

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

n, q = na()
s = ns()
su = [0] * (n + 1)

ac = 0
if s[0] =="A":
    ac = 1
for i in range(1, n):
    si = s[i]
    if si == "A":
        ac = 1

    elif si =="C" and ac == 1:
        su[i] = 1
        ac = 0
    else:
        ac = 0
    su[i] += su[i-1]




l, r = [], []
for _ in range(q):
    li, ri = na()
    l.append(li)
    r.append(ri)

for i in range(q):
    print(su[r[i]-1] - su[l[i]-1])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
cnt_array = [0] * (N+1)
ans = [0] * Q
for i in range(N):
    if i != N-1 and S[i] == 'A' and S[i+1] == 'C':
        cnt_array[i+1] = 1 + cnt_array[i]
    else:
        cnt_array[i+1] = cnt_array[i]
for i in range(Q):
    l, r = map(int, input().split())
    ans[i] = cnt_array[r-1] - cnt_array[l-1]
for i in ans:
    print(i)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N,Q = map(int,input().split())
S = list(input())
LR = [list(map(int,input().split())) for _ in range(Q)]
lis = []
s = [0]*N
for i in range(N-1):
    if S[i] == "A" and S[i+1] == "C":
        s[i] = 1

L = [0]*(N+1)
R = [0]*N

tmp = 0
for ind,i in enumerate(s):
    if i == 1:
        tmp += 1
    L[ind+1] = tmp

tmp = 0
for i in range(N-2,-2,-1):
    if s[i] == 1:
        tmp += 1
    R[i+1] = tmp

R.append(0)
sums =sum(s)
for i in range(Q):
    ans = sums-L[LR[i][0]-1]-R[LR[i][1]]
    print(ans)

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = str(input())
J=[0]*N


for i in range(1,N):
    if((S[i-1]=="A")&(S[i]=="C")):
        J[i]=J[i-1]+1
    else:
        J[i]=J[i-1]


for i in range(Q):
    L, R = map(int, input().split())
    print(J[R-1]-J[L-1])

Submission status: Accepted

Solution code:
import re

n, q = map(int, input().split())
s = input()
count = [0] * len(s)
for ind, char in enumerate(s[1:], 1):
    if char == 'C' and s[ind-1] == 'A':
        count[ind] = count[ind-1] + 1
    else:
        count[ind] = count[ind-1]

# print(count)
answer = []

for _ in range(q):
    l, r = map(int, input().split()) 
    answer.append(count[r-1] - count[l-1])

for ans in answer:
    print(ans)

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
s = input()
ac_list = [0]*(n+1)

counter = 0

for i in range(n):
  if s[i:i+2] == 'AC':
    counter+=1
  ac_list[i+1]=counter
  
for _ in range(q):
  l, r = map(int, input().split())
  print(ac_list[r-1] - ac_list[l-1])

Submission status: Accepted

Solution code:
n,q = map(int,input().split())
s = input()
A = [0 for _ in range(n)]
C = [0 for _ in range(n)]
memo1 = [0 for _ in range(n)]
memo2 = [0 for _ in range(n)]

for i in range(n):
    if s[i]=="A":
        A[i] = 1
    elif s[i] =="C":
        C[i] = 1
    if i == 0:
        continue
    if A[i-1] and C[i]:
        memo1[i] = memo1[i-1] + 1
    else:
        memo1[i] = memo1[i-1]
for i in range(n-2,-1,-1):
    if A[i] and C[i+1]:
        memo2[i] = memo2[i+1] + 1
    else:
        memo2[i] = memo2[i+1]
total = memo2[0]
for _ in range(q):
    l,r = map(lambda x:int(x)-1,input().split())
    ans = total
    if l >0:
        ans -= memo1[l-1]
        if A[l-1] and C[l]:
            ans -=1
    if r<n-1:
        ans -= memo2[r+1]
        if A[r] and C[r+1]:
            ans -= 1
    print(ans)

Submission status: Accepted

Solution code:
N,Q=map(int,input().split())
S=input()
LR=[]
for i in range(Q):
    LR.append(list(map(int, input().split())))

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

for i in range(1,N):
    if S[i]=='C' and S[i-1]=='A':
        R[i]+=1
for i in range(1,N):
    R[i]+=R[i-1]

for i in range(Q):
    print(min(L[LR[i][1]-1],R[LR[i][1]-1])-\
          min(L[LR[i][0]-1],R[LR[i][0]-1]))

Submission status: Accepted

Solution code:
N,Q = map(int, input().split())
S = input()
L = []
for i in range(Q):
    L.append([int(x) for x in input().split()])
    
ac_list = [0 for i in range(N)]
ac_num = 0
for i in range(1, N):
    pre = S[i-1]
    ac_list[i] = ac_list[i-1]
    if pre == "A" and S[i] == "C":
        ac_num += 1
        ac_list[i] = ac_num

for i in range(Q):
    ans = ac_list[L[i][1]-1] - ac_list[L[i][0]-1]
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))


n,q = inpl()
s = input()
qq = []
cnt = [0] * n
for i in range(q):
    a,b = inpl()
    qq.append([a,b])
for i in range(1,n):
    if s[i-1] == 'A' and s[i] == 'C':
        cnt[i] = cnt[i-1] + 1
    else:
        cnt[i] = cnt[i-1]
for i in qq:
    l = i[0]
    r = i[1]
    print(cnt[r-1] - cnt[l-1])

Submission status: Accepted

Solution code:
import sys
import pdb
from bisect import *
from collections import *
from copy import deepcopy
from datetime import *
from heapq import *
from itertools import *
from math import *
from operator import *
from pprint import *

sys.setrecursionlimit(10**8)

input = sys.stdin.readline

def main():
    """ main """
    N,Q = map(int, input().split())
    S = input().rstrip()
    dp = [0]*(N+1)
    for i in range(1,N):
        if S[i-1:i+1] == 'AC':
            dp[i+1] = dp[i]+1
        else:
            dp[i+1] = dp[i]
    for _ in range(Q):
        l,r = map(int, input().split())
        print(dp[r]-dp[l])

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
    
N,Q=map(int,input().split())
S=input()
count=[0]*N
for i in range(1,N):
	count[i]+=count[i-1]
	if S[i-1:i+1]=="AC":
		count[i]+=1
for i in range(Q):
	l,r=map(int,input().split())
	print(count[r-1]-count[l-1])

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
s = input()
cnt_list = [0]*(n)
tmpi = 0
for i in range(1, n):
    if s[i-1] == "A":
        if s[i] == "C":
            cnt_list[i] = cnt_list[i-1] + 1
        else:
            cnt_list[i] = cnt_list[i-1]
    else:
        cnt_list[i] = cnt_list[i-1]

for i in range(q):
    l, r = map(int, input().split())
    print(cnt_list[r-1] - cnt_list[l-1])

Submission status: Accepted

Solution code:
N,Q = map(int, input().split())

S = input()
l =[0] * Q
r = [0] * Q
for i in range(Q):
    l[i], r[i] = map(int, input().split())

left = [0] * N
if S[0:2] == "AC":
    left[1] = 1
for i in range(2,N):
    if S[i-1:i+1] == "AC":
        left[i] = left[i-2]+1
    else:
        left[i] = left[i-1]

for i in range(Q):
    if (l[i] == 1):
        res = left[r[i]-1]
    else:
        res = left[r[i]-1] - left[l[i]-2]
    if l[i] >= 2 and S[l[i]-1] == "C" and S[l[i]-2] == "A":
        res -= 1
    print(res)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
input = sys.stdin.readline

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

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

for l, r in LR:
    ans = lst[r-1] - lst[l-1]
    print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

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

for l, r in LR:
    ans = lst[r-1] - lst[l-1]
    print(ans)

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
s = input()
ans = [0 for _ in range(n)]
count = 0
for i in range(1, n):
    a = s[i-1]
    b = s[i]
    if a == 'A' and b == 'C':
        count += 1
    ans[i] = count

for i in range(q):
    l, r = map(int, input().split())
    print(ans[r-1]-ans[l-1])

Submission status: Accepted

Solution code:
#122 C
n,q=map(int,input().split())
s=input()
t=[0]*n
lr=[list(map(int,input().split())) for i in range(q)]
i=0
cnt=0
while i<n-1:
    if s[i]=="A":
        if s[i+1]=="C":
            t[i]+=cnt
            cnt+=1
            i+=1
            t[i]+=cnt
            i+=1
        else:
            t[i]+=cnt
            i+=1
    else:
        t[i]+=cnt
        i+=1
if i==n-1:
    t[i]+=cnt
for j in range(q):
    print(t[lr[j][1]-1]-t[lr[j][0]-1])

Submission status: Accepted

Solution code:
import itertools
n,q = map(int, input().split())
s = input()
calc = [0]*n
prev = None
for i,c in enumerate(s):
    if prev == 'A' and c == 'C':
        calc[i] = 1
    prev = c
calc = list(itertools.accumulate(calc))
for _ in range(q):
    l,r = map(int,input().split())
    print(calc[r-1]-calc[l-1])

Submission status: Accepted

Solution code:
n,q = map(int, input().split())
s = input()
c = []
pre = ""
count = 0
for i in range(n):
  if pre == "A" and s[i] == "C":
    count += 1
  pre = s[i]
  c.append(count)
  
for i in range(q):
  l,r = map(int,input().split())
  print(c[r-1]-c[l-1])

Submission status: Accepted

Solution code:
n,q = map(int, input().split())
s = input()
lst = []
num = 0
for i in range(n):
    if i == 0:
        lst.append(num)
        continue
    ss = s[i-1:i+1]
    if ss == 'AC':
        num += 1
    lst.append(num)
for _ in range(q):
    l,r = map(int,input().split())
    ans = lst[r-1] - lst[l-1]
    print(ans)

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
s = input()
ss = [0] * n
for i in range(n - 1):
    if s[i:(i+2)] == "AC":
        ss[i] = 1
sss = [0] * n
for i in range(n - 1):
    sss[i + 1] = sss[i] + ss[i]
for _ in range(q):
    l, r  = map(int, input().split())
    print(sss[r - 1] - sss[l - 1])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
B = [0] * N
for i in range(N-1):
    if S[i] == 'A' and S[i+1] == 'C':
        B[i] = 1
for i in range(1, N-1):
    B[i] += B[i-1]
for i in range(Q):
    l, r = map(int, input().split())
    print(B[r-2] - B[l-2])

Submission status: Accepted

Solution code:
N,Q = map(int,input().split())
S = list(input())
R = [0]*N
ans = []
temp = 0
for i in range(1,N):
  if S[i-1] == 'A' and S[i] == 'C':
    temp += 1
  R[i] = temp
for k in range(Q):
  l,r = map(int,input().split())
  ans.append(R[r-1]-R[l-1])
for j in range(len(ans)):
  print(ans[j])

Submission status: Accepted

Solution code:
N,Q = map(int, input().split())
S = input()
count = [0]*N
ans = []
for i in range(1,N):
    count[i] += count[i-1]
    if S[i-1:i+1]=='AC' : count[i] += 1
for i in range(Q):
    l,r = map(int, input().split())
    ans.append(count[r-1]-count[l-1])
for i in  ans:
    print(i)

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
LR = [list(map(int, input().split())) for _ in range(Q)]

cnt, acc = [0] * N, 0
for i in range(1, N):
    if S[i-1:i+1] == "AC":
        acc += 1
    cnt[i] = acc

for l, r in LR:
    print(cnt[r-1] - cnt[l-1])