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:
import sys
input = sys.stdin.readline

def main():
    N, Q = map(int, input().split())
    S = input().rstrip()

    cnt_ac = 0
    arr_ac = [0] * (N)
    for x in range(0, N-1):
        if S[x:x+2] == 'AC':
            cnt_ac += 1
        arr_ac[x+1] = cnt_ac

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


main()

Submission status: Accepted

Solution code:
n,q = map(int,input().split())
s = list(input())
ac = ["A","C"]
li = []

cnt = 0
for i in range(n):
    if s[i-1:i+1] == ac:
        cnt += 1
    li.append(cnt)

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

Submission status: Accepted

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

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

    ans = [0] * n
    pre = ''
    sum = 0
    for i, w in enumerate(s):
        if pre == 'A' and w == 'C':
            sum += 1
        ans[i] = sum
        pre = w
    
    for i 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:
import sys
def input(): return sys.stdin.readline().rstrip()

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

    ans = [0] * (n + 1)
    pre = ''
    sum = 0
    for i, w in enumerate(s):
        if pre == 'A' and w == 'C':
            sum += 1
        ans[i+1] = sum
        pre = w
    
    for i in range(q):
        l, r = map(int, input().split())
        print(ans[r] - ans[l])

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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()
lr = [list(map(int, input().split())) for _ in range(q)]

f = [0]*(n)
x = 0
for i in range(n):
    f[i] = x
    if s[i:i+2] == "AC":
        x += 1
        
for l, r in lr:
    print(f[r-1] - f[l-1])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
acc = [0] * (N+1)
for i in range(0, N):
    acc[i+1] = acc[i] + (i+1 < N and S[i+1] == "C" and S[i] == "A")
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=list(input())

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

Submission status: Accepted

Solution code:
n, q = list(map(int, input().split()))
s = input().strip()
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 = list(map(int, input().split()))
    print(t[r - 1] - t[l - 1])

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
s = input()
c = [0] * n
for i in range(n - 1):
    if s[i] == 'A' and s[i + 1] == 'C':
        c[i + 1] += 1
    c[i + 1] += c[i]
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()
a=[0]*n
for i in range(1,n):
  if s[i-1]=="A" and s[i]=="C":a[i]+=1
  a[i]+=a[i-1]
for i in range(q):
  aa,b=map(int,input().split())
  print(a[b-1]-a[aa-1])

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())+['X']
    A=[0]*N
    for i in range(N):
        if S[i]=='A' and S[i+1]=='C':
            A[i+1]+=1
    S_A=[0]*(N+1)
    for i in range(N):
        S_A[i+1]=S_A[i]+A[i]
    for i in range(Q):
        l,r=map(int,input().split())
        print(S_A[r]-S_A[l])

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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()
LR=[map(int,input().split()) for i in range(Q)]

data=[0 for i in range(N)]
for i in range(N-1):
    data[i+1]=data[i]
    if S[i]+S[i+1]=="AC":
        data[i+1]+=1
for l,r in LR:
    print(data[r-1]-data[l-1])

Submission status: Accepted

Solution code:
N,Q = map(int, input().split())
S = input()
Query = []
for _ in range(Q):
    l,r = map(int, input().split())
    Query.append([l,r])

A = [0]*N

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

for i in range(Q):
    l,r = Query[i]
    print(s[r-1]-s[l-1])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

AC_point = []
for n in range(N-1):
  if S[n] == "A" and S[n+1] == "C":
    AC_point.append(n+1)
import bisect
for lr in LR:
  a = bisect.bisect_left(AC_point,lr[0])
  b = bisect.bisect_left(AC_point,lr[1])
  print(b-a)

Submission status: Accepted

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

X=[0]*(N)
cnt=0
for i in range(N-1): 
  if S[i]=='A' and S[i+1]=='C':
    cnt+=1
    X[i+1]=cnt
  else:
    X[i+1]=cnt
    
for i in range(Q):
  a=A[i][0]-1
  b=A[i][1]-1
  print(X[b]-X[a])

Submission status: Accepted

Solution code:
import bisect
N,Q = map(int,input().split())
S = input()
lista = []
for j in range(N-1):
    if S[j:j+2] == 'AC':
        lista.append(j)
for i in range(Q):
    l,r = map(int,input().split())
    l,r = l-1,r-1
    lind = bisect.bisect_left(lista,l)
    rind = bisect.bisect_right(lista,r-1)
    print(rind-lind)

Submission status: Accepted

Solution code:
# coding: utf-8

(moji_len, query_count) = [int(i) for i in input().rstrip().split(" ")]

mojiretu = input().rstrip()

count_ac = [0]
for i in range(1, moji_len):
    count_ac.append(count_ac[i-1])
    if mojiretu[i] == "C":
        if mojiretu[i-1] == "A":
            count_ac[i] += 1

ans = []
for i in range(query_count):
    q = [int(i) - 1 for i in input().rstrip().split(" ")]
    cnt = count_ac[q[1]]
    if q[0] > 0:
        cnt -= count_ac[q[0] - 1]
        if mojiretu[q[0] - 1] == "A" and mojiretu[q[0]] == "C":
            cnt -= 1
    ans.append(cnt)

for i in ans:
    print(i)

Submission status: Accepted

Solution code:
n,q=map(int,input().split())
s=str(input())
l=[]
r=[]
for i in range(q):
    a,b=map(int,input().split())
    l.append(a)#文字列の最初
    r.append(b)#文字列の最後

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

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


#print(s[2:7])=ACTAC

Submission status: Accepted

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

AC_num = [0 for _ in range(N)]

i = -1

while(True):
  i = S.find("AC", i + 1)
  
  if i == -1:
    break
  
  AC_num[i + 1] += 1

for i in range(N - 1):
  AC_num[i + 1] += AC_num[i]

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

Submission status: Accepted

Solution code:
N,Q = map(int, input().split())
S = input()
SUM = [0] * (N + 1)
L = [0]*Q
R = [0]*Q


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

for i in range(Q):
    L[i], R[i] = list(map(int, input().split()))
    #indexを0からにする
    print(SUM[R[i]-1] - SUM[L[i]-1])

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n,q=map(int,readline().split())
s=readline().decode('utf-8')
dic={1:0}
c=0
for idx, (a,b) in enumerate(zip(s,s[1:]),1):
  if a=='A' and b=='C':
    c+=1
  dic[idx+1]=c
lst=map(int,read().split())
lr=list(zip(lst,lst))
for l,r in lr:
  print(dic[r]-dic[l])

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n,q=map(int,readline().split())
s=readline().decode('utf-8')
dic={}
c=0
for idx, i in enumerate(s, 1):
  if i=='C' and s[idx-2]=='A':
    c+=1
  dic[idx]=c
lst=map(int,read().split())
lr=list(zip(lst,lst))

for l,r in lr:
  print(dic[r]-dic[l])

Submission status: Accepted

Solution code:
import sys
from collections import deque

def resolve():
    input=sys.stdin.readline
    N,Q=map(int,input().rstrip().split())
    S=list(input().rstrip())
    R=[list(map(int,input().rstrip().split())) for _ in range(Q)]
    cumsum=[0 for i in range(N)]

    _S=deque(S)
    prevT=_S.popleft()
    for i in range(1,N):
        #T=S[0:i+1]#i=0で一字含む, 
        """
        T+=_S.popleft()
        cumsum[i]=T.count("AC")#cumsum[i]:i+1文字以下でACを含むか
        """
        nowT=_S.popleft()
        if(prevT=="A" and nowT=="C"):
            cumsum[i]=cumsum[i-1]+1
        else:
            cumsum[i]=cumsum[i-1]
        prevT=nowT

    cumsum=[0]+cumsum#cumsum[i]:i文字以下でACを含むか
    for j in range(Q):
        print(cumsum[R[j][1]]-cumsum[R[j][0]])

from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """8 3
ACACTACG
3 7
2 3
1 8"""
        output = """2
0
3"""
        self.assertIO(input, output)

if __name__ == "__main__":
    #unittest.main()
    resolve()

Submission status: Accepted

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

S = list(input())

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

cnt = [0]*N

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate
N, Q = map(int, input().split())
S = input()
LR = [tuple(map(int, input().split())) for _ in range(Q)]
A = list(map(lambda s1,s2: int(s1 == "A" and s2 == "C"), S, S[1:]))
B = [0] + list(accumulate(A))
C = list(accumulate(A[::-1]))[::-1] + [0]
P = S.count("AC")
for l, r in LR:
    print(P - B[l-1] - C[r-1])

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# C - GeT AC
N, Q = map(int, input().split())
S = str(input())
A = [list(map(int, input().split())) for i in range(Q)]
S1 = [0]*N
count = 0
for j in range(1,len(S)):
    if S[j] == 'C' and S[j - 1] == 'A':
        count += 1
    else:
        pass
    S1[j] = count
#print(S1)

for k in range(Q):
    print(S1[A[k][1]-1]-S1[A[k][0]-1])

Submission status: Accepted

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

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

Submission status: Accepted

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

a=[0]*(n+1)
for i in range(1,n+1):
    # print(s[i-1:i+1])
    if s[i-1:i+1]=="AC":
        a[i]=a[i-1]+1
    else:
        a[i]=a[i-1]

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

Submission status: Accepted

Solution code:


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

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

A.insert(0, 0)

for q in range(Q):
  s, t = map(int, input().split())
  print(A[t]-A[s])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
s = input().rstrip()
psums = [0]
for i, ch in enumerate(s):
    psums.append(psums[-1])
    if ch == 'C' and i - 1 >= 0 and s[i - 1] == 'A':
        psums[-1] += 1

def sumRange(left, right):
    res = psums[right + 1] - psums[left]
    if s[left] == 'C' and left - 1 >= 0 and s[left - 1] == 'A':
        res -=1
    return res

ret = []
for _ in range(q):
    left, right = map(lambda x: int(x) - 1, input().split())
    ret.append(sumRange(left, right))
print('\n'.join(map(str, ret)))

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
cons = [0] * (N + 1)

for i in range(N - 1):
  #隣り合う2文字(S[i] + S[i + 1])が"AC"である数の累積和を予め記録しておく
  #愚直に計算すると最悪でN * Q = 10 ** 6となるため、下準備をして再計算を防ぐ
  ##ACACTACGなら[0,1,1,2,2,2,3,3]
  cons[i + 1] = cons[i] + (1 if S[i] + S[i + 1] == 'AC' else 0)

for i in range(Q):
  l,r = map(int, input().split())
  #予め記録してあるACの累積数を引けば範囲内のACの数が求められる
  #ACと単位が2文字であるためrを1つ左にずらしておく(1引かないとCがはみ出てしまう)
  print(cons[r - 1] - cons[l - 1])

Submission status: Accepted

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

word = input()

counter = [0] * n

for i in range(1, len(word)):
    counter[i] = counter[i-1]
    if word[i-1] + word[i] == 'AC':
        counter[i] += 1

for i in range(q):
    start, end = map(int, input().split())
    print(counter[end-1] - counter[start-1])

Submission status: Accepted

Solution code:
# ACのインデックスを求める
# インデックスから、含まれる個数を求める(2分探索)
from bisect import bisect_left
N,Q = map(int, input().split())
S = input()
tmp = ''
ac_count = []
for i, c in enumerate(S):
	if tmp + c == 'AC':
		ac_count.append(i-1)
	tmp = c
ac_count.sort()

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

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split(' ')))
a = input()
acs = [0] * n
for i in range(1, n):
  acs[i] = acs[i-1] + (a[i-1: i+1] == 'AC')
print('\n'.join(map(str, [acs[e-1] - acs[s-1] for s, e in [list(map(int, input().split(' '))) for _ in range(k)]])))

Submission status: Accepted

Solution code:
n, q = [int(i) for i in input().split()]
st = input() + '-'
table = []
cnt = 0
for i, v in enumerate(st):
	if st[i: i+2] == 'AC':
		cnt += 1
	table.append(cnt)
table = [0] + table

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

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
l = []
r = []
for _ in range(Q):
    l1, r1 = map(int, input().split())
    l.append(l1)
    r.append(r1)
    

m = S.count("AC")
S = "a" + S
ll = [0 for _ in range(N+1)]
rr = [0 for _ in range(N+1)]
for i in range(1,N+1):
    ll[i] = ll[i-1]
    if S[i-1] == "A" and S[i] == "C":
       ll[i] -= 1
    rr[i] = -m - ll[i]
       
for i in range(Q):
    print(m + rr[r[i]] + ll[l[i]])

Submission status: Accepted

Solution code:
import os, sys, re, math

(N, Q) = [int(n) for n in input().split()]
S = input()

oc = [0]
n = 0
for i in range(len(S) - 1):
    if S[i:i+2] == 'AC':
        n += 1
    oc.append(n)
oc.append(n)

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

Submission status: Accepted

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

Submission status: Accepted

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

S = input()
a = [0] * (len(S) + 1)
c = [0] * (len(S) + 1)
ac = [0] * (len(S) + 1)
for i in range(1, len(S) + 1):
    ac[i] = ac[i - 1]
    s = S[i - 1]
    if s == 'A':
        a[i] = 1
    if s == 'C':
        c[i] = 1
        if i > 1 and a[i - 1] == 1:
            ac[i] += 1
    
for q in range(Q):
    l, r = [int(s) for s in input().split(' ')]
    dl = ac[l]
    dr = ac[r]
    print(dr - dl)

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split(' ')))
orig_a = input()
acs = {}
for i in range(n):
  if i < 1:
    acs[i] = 0
  else:
    acs[i] = acs[i-1] + (orig_a[i-1: i+1] == 'AC')
for _ in range(k):
  s, e = list(map(int, input().split(' ')))
  print(acs[e-1] - acs[s-1])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
A = [0]
j = 0
for i in range(N-1):
  if S[i:i+2] == "AC":
    j += 1
  A.append(j)
for i in range(Q):
  q = list(map(int, input().split()))
  print(A[q[1]-1] - A[q[0]-1])

Submission status: Accepted

Solution code:
import bisect
import cmath
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, Q = list(map(int, sys.stdin.buffer.readline().split()))
S = sys.stdin.buffer.readline().decode().rstrip()
LR = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(Q)]


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

for l, r in LR:
    r -= 1
    l -= 1
    a = counts[r] - counts[l]
    print(a)

Submission status: Accepted

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

#acの場所把握
ac = [0] * (n + 1)
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 i in range(q):
    print(ac[r[i] - 1] - ac[l[i] - 1])

Submission status: Accepted

Solution code:
import re

def solve1():
    N, Q = map(int, input().split())
    # print(N, Q)
    acgt_string = input()
    sub_str_ith = []
    for _ in range(Q):
        start, end = map(int, input().split())
        sub_str_ith.append((start, end))

    t = [0] * (N+1)
    for i in range(N):
        t[i+1] = t[i] + (1 if acgt_string[i:i+2] == "AC" else 0)

    for start, end in sub_str_ith:
        print(t[end-1] - t[start-1])

def my_solve1():
    N, Q = map(int, input().split())
    # print(N, Q)
    acgt_string = input()
    sub_str_ith = []
    for _ in range(Q):
        start, end = map(int, input().split())
        sub_str_ith.append((start, end))
    # print(sub_str_ith)

    # 全ての'A'の位置を取得
    a_iths = [m.span() for m in re.finditer('AC', acgt_string)]
    # print(a_iths)

    for start, end in sub_str_ith:
        # print(start, end)
        num = 0
        for ac_start, ac_end in a_iths:
            if ac_start >= start-1 and ac_end <= end:
                num += 1
        print(num)


solve1()
# my_solve1()

Submission status: Accepted

Solution code:
import numpy as np

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 for _ in range(len(s))]

for i in range(1, len(s)):
    if(s[i-1: i+1] == 'AC'): dp[i] = dp[i-1] + 1
    else: dp[i] = dp[i-1]

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

Submission status: Accepted

Solution code:
n, q = [int(i) for i in input().split()]
s = input()

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

Submission status: Accepted

Solution code:
def solve():
    N, Q = map(int, input().split())
    S = input()
    l = [0 for _ in range(Q)]
    r = l[:]
    for i in range(Q):
        l[i], r[i] = map(int, input().split())
    
    ac_head = [0 for _ in range(N+1)]
    for i, char in enumerate(S[:-1]):
        if char == 'A' and S[i+1] == 'C':
            ac_head[i+1] = 1 + ac_head[i]
        else:
            ac_head[i+1] = ac_head[i]
    
    for a, b in zip(l, r):
        print(ac_head[b-1] - ac_head[a-1])


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, Q: int, S: str, l: "List[int]", r: "List[int]"):
    table = [0] * (N + 1)

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

    for i in range(Q):
        print(table[r[i] - 1] - table[l[i] - 1])

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    l = [int()] * (Q)  # type: "List[int]"
    r = [int()] * (Q)  # type: "List[int]"
    for i in range(Q):
        l[i] = int(next(tokens))
        r[i] = int(next(tokens))
    solve(N, Q, S, l, r)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

aclist = [0,0]
acsum = 0

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

for i in range(q):
    ans = aclist [lr[i][1]] - aclist [lr[i][0]-1]
    if aclist [lr[i][0]] - aclist [lr[i][0]-1] != 0:
        ans -= 1
    print(ans)

Submission status: Accepted

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

aclist = [0,0]
acsum = 0

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

for i in range(q):
    if aclist [lr[i][0]] - aclist [lr[i][0]-1] != 0:
        ans = aclist[lr[i][1]] - aclist[lr[i][0]-1] - 1 
    else:
        ans = aclist[lr[i][1]] - aclist[lr[i][0]-1]
    print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

length, N=two_int()

S=one_str()

ac_list = [0]

hoge = 0
for i in range(length-1):
    if S[i:i+2]=="AC":
        hoge += 1
    ac_list.append(hoge)

for i in range(N):
    l, r = two_int()
    print(ac_list[r-1] - ac_list[l-1])

Submission status: Accepted

Solution code:
n,q = list(map(int, input().split()))
s = input()
question = [list(map(int, input().split())) for _ in range(q)]
ac = []
amount = 0
for i in range(len(s)):
  if(i == 0):
    ac.append(0)
    continue
  if(s[i-1] == "A" and s[i] == "C"):
    amount +=1
  ac.append(amount)
for r, l in question:
  print(ac[l-1] - ac[r-1])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

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

A = []

for n in range(N):
  if n == 0:
    A.append(0)
  elif S[n] == "C" and S[n-1] == "A":
    A.append(A[n-1] + 1)
  else:
    A.append(A[n-1])

A.insert(0, 0)

for i in range(Q):
  x, y = map(int, input().split())
  print(A[y] - A[x])

Submission status: Accepted

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

    AC_cnt = 0
    ll = [0]
    for i in range(1, len(S)):
        if S[i - 1:i + 1] == "AC":
            AC_cnt += 1
        ll.append(AC_cnt)

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


main()

Submission status: Accepted

Solution code:
N,Q=map(int,input().split())
S=input()
lc=[0 for i in range(N)]
for i in range(N-1):
    if S[i:i+2]=="AC":
        lc[i+1]=lc[i]+1
    else:
        lc[i+1]=lc[i]
llr=[]
for i in range(Q):
    llr.append(list(map(int,input().split())))
for i in range(Q):
    print(lc[llr[i][1]-1]-lc[llr[i][0]-1])

Submission status: Accepted

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

T = [0] * N
for i in range(1, N):
  if S[i] == "C" and S[i-1] == "A":
    T[i] = 1
  T[i] += T[i-1]
#print("T:", T)

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

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
    N, Q = LI()
    s = S()
    L, R = [], []
    for _ in range(Q):
        l, r = LI_()
        L.append(l)
        R.append(r)
    num_ac = [0] * len(s)
    for i in range(len(s) - 1):
        if s[i] == 'A' and s[i+1] == 'C':
            num_ac[i+1] += 1
    for i in range(1, len(s)):
        num_ac[i] += num_ac[i-1]
    for i in range(Q):
        print(num_ac[R[i]] - num_ac[L[i]])

main()

Submission status: Accepted

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

for j in range(q):
    ans=m[l[j][1]-1]-m[l[j][0]-1]
    print(ans)

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
s = input()
lr = [list(map(int, input().split())) for _ in range(q)]
t = [0]
count = 0
for i in range(1, n):
    if s[i-1] + s[i] == 'AC':
        count += 1
    t.append(count)
for le, ri in lr:
    print(t[ri-1] - t[le-1])

Submission status: Accepted

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

flg = False
acm = []
cnt = 0
for a in s:
    if a == 'A':
        flg = True
    elif a == 'C' and flg:
        cnt += 1

    if a != 'A':
        flg = False
    acm.append(cnt)

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N, Q = list(map(int, input().split()))
S = list(str(input()))
questions = [tuple(map(int, input().split())) for _ in range(Q)]
ACs = [0] * N

for i in range(len(S)-1):
    if S[i] == 'A' and S[i + 1] == 'C':
        ACs[i + 1] = ACs[i] + 1
    else:
        ACs[i + 1] = ACs[i]

for q in questions:
    print(ACs[q[1]-1] - ACs[q[0]-1])