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 main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations
    #from itertools import accumulate, product, permutations
    from math import floor, ceil

    #mod = 1000000007

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
s = S[:2]
ans = 'AC'
lst = [0]
count = 0
for i in range(N-1):
    s = S[i] + S[i + 1]
    count += (s == ans)
    lst.append(count)

for _ in range(Q):
    a, b = map(int, input().split())
    print(lst[b - 1] - lst[a - 1])

Submission status: Accepted

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

S = list(input())

A = [0]

for i in range(1, n+1):
    # if S[i-1:i]:
    if S[i-2:i] == ['A', 'C']:
        A.append(A[-1] + 1)
    else: 
        A.append(A[-1])
# print(A[1:])
A = A[1:]
for i in range(q):
    a, b = map(int, input().split())
    print(A[b-1] - A[a-1])

Submission status: Accepted

Solution code:
from itertools import accumulate
n,q = map(int,input().split())
s = input()
L = [0]*n

for i in range(n-1):
    if s[i] == "A" and s[i+1] == "C":
        L[i] = 0
        L[i+1] = 1

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

Submission status: Accepted

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

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()

dp = [0]*n

for i in range(1,n):
    if s[i-1] == 'A' and s[i] == 'C':
        dp[i] = dp[i-1] + 1
    else:
        dp[i] = dp[i-1]
#print(dp)
for _ in range(q):
    a, b = (int(x) for x in input().split())
    print(dp[b-1]-dp[a-1])

Submission status: Accepted

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

cnt=[0 for _ in range(n)]
for i in range(0,n-1):
    cnt[i+1]=cnt[i]
    if s[i]=='A' and s[i+1]=='C':
        cnt[i+1]+=1
    
for _ in range(q):
    l,r=map(int,input().split())
    print(cnt[r-1]-cnt[l-1])

Submission status: Accepted

Solution code:
def main():
    n,Q = map(int,input().split())
    s = input()
    q =[]
    for i in range(Q) :
        l,r=map(int,input().split())
        q.append([l-1,r-1])
    sum_ary = [0] * n

    for i in range(1,n):
        sum_ary[i] = sum_ary[i-1]
        if s[i] == 'C' and s[i-1] == 'A':
            sum_ary[i] += 1

    for i,query in enumerate(q):
        print(sum_ary[query[1]] - sum_ary[query[0]])
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,Q=MI()
    S=ST()
    cumsum = [0]*N
    for i in range(N-1):
        if S[i:i+2]=="AC":
            cumsum[i+1]+=1
        cumsum[i+1]+=cumsum[i]
    for _ in range(Q):
        l,r = MI()
        print(cumsum[r-1]-cumsum[l-1])
if __name__ == '__main__':
    main()

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()
lis = [0] * (n + 1)

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

Submission status: Accepted

Solution code:
N,Q=map(int,input().split())
S=input()
X=[0]*N
for i in range(N-1):
	if S[i:i+2]=="AC":
		X[i+1]=X[i]+1
	else:
		X[i+1]=X[i]
for i in range(Q):
	L,R=map(int,input().split())
	print(X[R-1]-X[L-1])

Submission status: Accepted

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

ac_lst = [0] * (n+1)

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

for i in range(q):
    start = lr_lst[i][0]
    end = lr_lst[i][1]

    print(ac_lst[end-1] - ac_lst[start-1])

Submission status: Accepted

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

ac = [0] * n
for itr, (i, j) in enumerate(zip(S, S[1:])):
  if i + j == "AC":
    ac[itr+1] = ac[itr] + 1
  else:
    ac[itr+1] = ac[itr]

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()
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:
def main():
    N, Q = (int(i) for i in input().split())
    S = input()
    C = [0] * (len(S) + 1)
    for i in range(1, len(S)):
        C[i+1] = C[i] + (1 if S[i] == "C" and S[i-1] == "A" else 0)

    for q in range(Q):
        le, ri = (int(i) for i in input().split())
        print(C[ri] - C[le])


if __name__ == '__main__':
    main()

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()))

n, q = na()
s = ns()
ac = list()
now = 0
f = False
for si in s:
    if si == "C" and f:
        now += 1
        f = False
    elif si == "A":
        f = True
    else:
        f = False
    ac.append(now)
for _ in range(q):
    l, r = na()
    l -= 1; r -= 1
    print(ac[r] - ac[l])

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input():
    return sys.stdin.readline().rstrip()

def main():
    N,Q=map(int,input().split())
    S=list(input())+['Z']
    ans=[0]*(N+1)
    for i in range(N):
        if S[i]=='A' and S[i+1]=='C':
            ans[i+1]=ans[i]+1
        else:
            ans[i+1]=ans[i]
    for _ in range(Q):
        l,r=map(lambda x:int(x)-1,input().split())
        print(ans[r]-ans[l])

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

ans = []
for l,r in LR:
    l,r = l-1,r-1
    ans.append(cums[r] - cums[l])
print(*ans,sep='\n')

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
s = str(input())
cum = [0]*n
for i in range(n):
    if i == 0:
        continue
    if s[i-1] == 'A' and s[i] == 'C':
        cum[i] = cum[i-1]+1
    else:
        cum[i] = cum[i-1]
#print(cum)

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

Submission status: Accepted

Solution code:
from itertools import accumulate
N, Q = map(int, input().split())
S = "?" + input()
query = (tuple(map(int, input().split())) for _ in range(Q))

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

cnt = tuple(accumulate(cnt))

ans = (cnt[r] - cnt[l] for l, r in query)
print(*ans, sep="\n")

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input().strip()
b = None
nm = 0
dp = [0]
for c in S:
#    if b == None:
#        b = c
#        continue
    if b == 'A' and c == 'C':
        nm += 1
    dp.append(nm)
    b = c
#print(dp)
for _ in range(Q):
    l, r = map(int, input().split())
    print(dp[r]-dp[l])

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=input()
ac=[0]*n
x=0
for i in range(n-1):
    if a[i]=='A' and a[i+1]=='C':
        x+=1
    ac[i+1]=x
ac[-1]=x
for i in range(k):
    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()

ac = [0] * n

for i in range(1, n):
        if s[i-1:i+1]=="AC": ac[i] = 1
        ac[i] += ac[i-1]

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()

AC = [0]*(N+1)
for i in range(N-1):
    if S[i] == "A" and S[i+1] == "C":
        AC[i+2] += 1
for i in range(N):
    AC[i+1] += AC[i]

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

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
S = input().strip()
*LR, = (map(int, input().split()) for _ in range(q))

A = [0] * n
for i in range(1, n):
    A[i] = A[i-1] + (S[i-1:i+1] == 'AC')
for l, r in LR:
    print(A[r-1]-A[l-1])

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
S = input().strip()
*LR, = (map(int, input().split()) for _ in range(q))

A = [0] * n
for i in range(1, n):
    A[i] = A[i-1] + (S[i-1] == 'A' and S[i] == 'C')
for l, r in LR:
    print(A[r-1]-A[l-1])

Submission status: Accepted

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

acl = [0] * n
cnt = 0
for i in range(1, n):
    if s[i-1:i+1] == 'AC':
        cnt += 1
    acl[i] = cnt

for i in range(q):
    l = ll[i][0]
    r = ll[i][1]
    print(acl[r-1] - acl[l-1])

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,q = map(int,input().split())
s = input()
lis = [list(map(int,input().split())) for _ in range(q)]
rui = []
for i,ss in enumerate(s):
    if i == 0:
        rui.append(0)
    else:
        if ss == 'C' and  s[i-1] == 'A':
            rui.append(rui[-1]+1)
        else:
            rui.append(rui[-1])
#print(rui)
for l in lis:
    print(rui[l[1]-1]-rui[l[0]-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

ac_pos = []
p = 0
while p < N-1:
    if S[p]=="A" and S[p+1]=="C":
        ac_pos.append(p+1)
        p+=2
    else:
        p+=1

for _ in range(Q):
    l, r = map(int, input().split())
    p = bisect_left(ac_pos, l)
    q = bisect_left(ac_pos, r)
    print(q-p)

Submission status: Accepted

Solution code:
n,q=map(int,input().split())
S=input()
L=[list(map(int,input().split())) for i in range(q)]

s=[0]*(n+1)
for i in range(1,n+1):
    if i<=n-1 and S[i-1]=='A' and S[i]=='C':
        s[i]=s[i-1]+1
    else:
        s[i]=s[i-1]

#print(s)
for l,r in L:
    ans=s[r-1]-s[l-1]
    print(ans)

Submission status: Accepted

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

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()

accumlate=[0]
now=0
for i in range(1,n):
    if s[i-1]=="A" and s[i]=="C":
        now+=1
    accumlate.append(now)

ans_list=[]
for g in range(q):
    l,r=map(int,input().split())
    ans_list.append(accumlate[r-1]-accumlate[l-1])

for m in range(q):
    print(ans_list[m])

Submission status: Accepted

Solution code:
N, Q = [int(i) for i in input().split()]
S = input()
lr = [[int(i) for i in input().split()] for _ in range(Q)]
ary = [0]*(N+1)
c = 0
f = bool(S[0] == "A")
for i in range(1, N):
    if S[i] == 'C' and f:
        c += 1
        f = False
    else:
        f = bool(S[i] == "A")
    ary[i] = c
for l, r in lr:
    print(ary[r-1]-ary[l-1])

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n,q=mii()
s=list(ss())

dp=[0]*n

for i in range(n-1):
    if s[i]=="A" and s[i+1]=="C":
        dp[i+1]=dp[i]+1
    else:
        dp[i+1]=dp[i]

for i in range(q):
    x,y=mii()
    print(dp[y-1]-dp[x-1])

Submission status: Accepted

Solution code:
N,Q = [int(zz) for zz in input().split()]
S = input()

li = [0]*N
Aflag = 0
AC = 0
for i in range(N):
    if S[i] == 'A':
        Aflag = 1
    elif S[i] == 'C' and Aflag:
        AC += 1
        Aflag = 0
    else:
        Aflag = 0
    li[i] = AC

ans = []
for _ in range(Q):
    l,r = [int(zz)-1 for zz in input().split()]
    ans.append(li[r]-li[l])

[print(i) for i in ans]

Submission status: Accepted

Solution code:
from itertools import accumulate

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

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

p = [0] * n
for i in range(1, n):
  pp = 0
  if s[i-1:i+1] == "AC":
    pp = 1
  p[i] = pp
  
p = [0] + list(accumulate(p))

for e in lr:
  print(p[e[1]] - p[e[0]])

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()

# 'AC'の累積和を求める
d = [0]*n
for i in range(1, n):
    d[i] += d[i-1]
    if s[i-1] == 'A' and s[i] == 'C':
        d[i] += 1
        
lr = [list(map(int, input().split())) for _ in range(q)]
for l, r in lr:
    print(d[r-1] - d[l-1])

Submission status: Accepted

Solution code:
from itertools import accumulate

n, q = map(int, input().split())
s = list(input())
#print(s)
z = []
for i in range(n-1):
    if s[i] + s[i+1] == 'AC':
        z.append(1)
    else:
        z.append(0)
#print(z)

z = [0] + list(accumulate(z))
#print(z)

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

Submission status: Accepted

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

count = [0]

for i in range(1, N):
    count.append(count[i - 1])
    if S[i] == "C" and S[i - 1] == "A":
        count[i] += 1


for i in range(Q):
    print(count[lr[i][1] - 1] - count[lr[i][0] - 1])

Submission status: Accepted

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

for k in range(len(ans)):
    print(ans[k])

Submission status: Accepted

Solution code:
def main():
  import sys
  input=sys.stdin.readline
  n,q=map(int,input().split())
  s=input()
  
  ac=[0]*(n+1)
  for i in range(1,n):
    if s[i-1]=='A' and s[i]=='C':
      ac[i+1]=ac[i]+1
    else:
      ac[i+1]=ac[i]  
  
  for i in range(q):
    l,r=map(int,input().split())
    if l==1:
      ans=ac[r]-ac[l-1]
    elif s[l-2]=='A' and s[l-1]=='C':
      ans=ac[r]-ac[l-1]-1
    else:
      ans=ac[r]-ac[l-1]
    print(ans)
    
if __name__=='__main__':
  main()

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())))

ac = [0] * n
for i in range(n - 1):
    ac[i + 1] = ac[i] + (1 if s[i:i + 2] == 'AC' else 0)

for i in range(q):
    left = lr[i][0] - 1
    right = lr[i][1] - 1
    ans = ac[right] - ac[left]
    print(ans)

Submission status: Accepted

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


a = [0] * (n + 1)

# n文字目までにACが出た回数を累積和で求める
for i in range(n):
  a[i + 1] = a[i] + (1 if s[i:i + 2] == 'AC' else 0)

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 = input()
counts = [0] * n
for i in range(1, n):
    if s[i - 1] == 'A' and s[i] == 'C':
        counts[i] = counts[i - 1] + 1
    else:
        counts[i] = counts[i - 1]

ret = [0] * q
for i in range(q):
    l, r = map(int, input().split())
    ret[i] = counts[r - 1] - counts[l - 1]

print(*ret, sep='\n')

Submission status: Accepted

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

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

for i in range(Q):
    print(rui[L[i][1]-1]-rui[L[i][0]-1])

Submission status: Accepted

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

Submission status: Accepted

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

    tmp = [0] * (n + 1)

    for i in range(1,n):
        tmp[i] = tmp[i-1] + ( 1 if s[i-1] ==  'A' and s[i] ==  'C' else 0 )
    
    for _ in range(q):
        l, r = map(int, input().split())
        print(tmp[r-1] - tmp[l-1])

if __name__ == '__main__':
    main()

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()
t = [0] * N

for i in range(1, N):
    t[i] = t[i-1]
    if S[i-1:i+1] == 'AC': t[i] += 1
    
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] = 1

cumsum = [0]
for _a in a:
  cumsum.append(cumsum[-1] + _a)


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

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:
import numpy as np
n, q = map(int, input().split())
s = input()
w = np.zeros(n, dtype=int)

a = 0
for i in range(n):
    if s[i] == "A":
        if a == 0:
            a += 1
    elif s[i] == "C":
        if a:
            w[i-1] = 1
            a -= 1
    else:
        if a:
            a -= 1

ans = np.append(np.zeros(1, dtype=int), np.cumsum(w))
for _ in range(q):
    l, r = map(int, input().split())
    print(ans[r-1] - ans[l-1])

Submission status: Accepted

Solution code:
from itertools import accumulate

N,Q=map(int,input().split())
S=input()
l=[0 for i in range(0,Q)]
r=[0 for i in range(0,Q)]
for i in range(0,Q):
    l[i],r[i]=map(int,input().split())
check=[]
for i in range(0,N-1):
    if S[i]+S[i+1]=='AC':
        check.append(1)
    else:
        check.append(0)
ans=list(accumulate(check))
for i in range(0,Q):
    if l[i]==1:
        print(ans[r[i]-2])
    else:
        print(ans[r[i]-2]-ans[l[i]-2])

Submission status: Accepted

Solution code:
from itertools import accumulate

def main():
    N,Q=map(int,input().split())
    S=input()
    l=[0 for i in range(0,Q)]
    r=[0 for i in range(0,Q)]
    for i in range(0,Q):
        l[i],r[i]=map(int,input().split())
    check=[]
    check_append=check.append
    for i in range(0,N-1):
        if S[i]+S[i+1]=='AC':
            check_append(1)
        else:
            check_append(0)
    ans=list(accumulate(check))
    for i in range(0,Q):
        if l[i]==1:
            print(ans[r[i]-2])
        else:
            print(ans[r[i]-2]-ans[l[i]-2])

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
s = input()
t = []
c = 0
for i in range(n - 1):
    if i != 0:
        if s[i - 1] == "A" and s[i] == "C":
            c += 1
    t.append(c)
if s[n - 2] == "A" and s[n - 1] == "C":
    t.append(c + 1)
else:
    t.append(c)
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()

acc = [0]
sum = 0
for i in range(n-1):
    # if s[i:i+2] == "AC":
    if s[i] == "A" and s[i+1] == "C":
        acc.append(acc[-1] + 1)
    else:
        acc.append(acc[-1])


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

Submission status: Accepted

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

acc = [0]
sum = 0
for i in range(n-1):
    # if s[i:i+2] == "AC":
    if s[i] == "A" and s[i+1] == "C":
        sum += 1
        acc.append(sum)
    else:
        acc.append(sum)


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

Submission status: Accepted

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

acc = [0]
sum = 0
for i in range(n-1):
    if s[i:i+2] == "AC":
        sum += 1
        acc.append(sum)
    else:
        acc.append(sum)


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

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N,Q=map(int, input().split())
S=input()
L=[None]*(N)
t=0
for i in range(N-1):
    L[i]=t
    if S[i]=="A" and S[i+1]=="C":
        t+=1
L[-1]=t
for i in range(Q):
    l,r=map(lambda x:int(x)-1,input().split())
    print(L[r]-L[l])

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N,Q=map(int, input().split())
S=input()
L=[None]*(N)
t=0
for i in range(N-1):
    L[i]=t
    if S[i]=="A" and S[i+1]=="C":
        t+=1
L[-1]=t
for i in range(Q):
    l,r=map(int,input().split())
    print(L[r-1]-L[l-1])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, Q = [int(i) for i in input().split()]
S = input()
ary = [0]*(N+1)
af = False
ans = []
for i, s in enumerate(S):
    ary[i+1] = ary[i]
    if s == "A":
        af = True
    elif s == "C" and af:
        ary[i+1] += 1
        af = False
    else:
        af = False
for _ in range(Q):
    l, r = [int(i) for i in input().split()]
    ans.append(ary[r]-ary[l])
for a in ans:
    print(a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,q = map(int,input().split())
s = list(input())
l = [0]*q
r = [0]*q
for i in range(q):
    l[i], r[i] = map(int,input().split())

lis = [0]
for i in range(1,n):
    if s[i-1] == 'A' and s[i] == 'C':
        lis.append(lis[i-1]+1)
    else:
        lis.append(lis[i-1])
# print(s)
# print(lis)
for i in range(q):
    print(lis[r[i]-1] - lis[l[i]-1])

Submission status: Accepted

Solution code:
n, q = map(int, input().split())  # n:num of string，q:num of question
s = input()  # string
cnt = [0] * n  # cnt list of 'AC'

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

for i in range(1, n):
    cnt[i] += cnt[i - 1]  # 累積和

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline().rstrip()

N, Q = map(int, input().split())
S = input()
li = [0]*(N+1)

for i in range(N):
    if S[i:i+2] == 'AC':
        li[i+1] = li[i] + 1
    else:
        li[i+1] = li[i]

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

Submission status: Accepted

Solution code:
n,q = [int(x) for x in 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 _ in range(q):
    l,r = [int(x) for x in input().split()]
    print(t[r-1]-t[l-1])

Submission status: Accepted

Solution code:
N,Q = map(int, input().split())
S = input()
range_list = [list(map(int, input().split())) for _ in range(Q)]
 
ac_counter = [0]
# acという文字列をカウントしておくlistを作成
for i in range(1,N):
    ac_counter.append(ac_counter[i-1]+1 if S[i-1:i+1] == 'AC' else ac_counter[i-1])
# 上で作ったリストを使い範囲内に何個あるのか確認
for range_list in range_list:
    print(ac_counter[range_list[1]-1] - ac_counter[range_list[0]-1])

Submission status: Accepted

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

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

for lr in LR:
    print(ac_cnt[lr[1]-1] - ac_cnt[lr[0]-1])