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

n, q = list(map(int, input().rstrip().split()))
s = input().rstrip()
arr = [list(map(int, input().rstrip().split())) for _ in range(q)]
cnt = [0] * (n + 1)
for i in range(n):
    if s[i : i + 2] == "AC":
        cnt[i + 1] = cnt[i] + 1
    else:
        cnt[i + 1] = cnt[i]
for i, j in arr:
    print(cnt[j - 1] - cnt[i - 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)]
s_count = [0 for i in range(n + 1)]
for i in range(n):
    s_count[i + 1] = s_count[i] + (1 if s[i:i + 2] == 'AC' else 0)

for i in range(q):
    print(s_count[lr[i][1] - 1] - s_count[lr[i][0] - 1])

Submission status: Accepted

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

ac_count = [0] *(N+1)

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

LR = []
for i in range(Q):
  LR.append([int(i) for i in input().split()])

for l,r in LR:
  ans = ac_count[r-1] - ac_count[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:
import itertools
N,K = map(int, input().split())
a = input()
l = [0]*(N-1)
for n,(i , j) in enumerate(zip(a,a[1:])):
    if i == "A" and j == "C":
        l[n] +=1
l = itertools.accumulate(l)
l = [0] + list(l)
for i in range(K):
    ta,tb = map(int, input().split())
    print(l[tb-1]-l[ta-1])

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
a = input()
l = [0]*(N-1)
for n,(i , j) in enumerate(zip(a,a[1:])):
    if i == "A" and j == "C":
        l[n] +=1
l = [0]+ l
for i,n in enumerate(l[:-1]):
    l[i+1] +=l[i]
for i in range(K):
    ta,tb = map(int, input().split())
    print(l[tb-1]-l[ta-1])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S=input()
S1=S.split("AC")
#print(S1)
C=[]
SC=[0]*len(S)
i=0
ac=0
for a in S1:
    #SC[i]=
    #i+=len(a)
    C.append(len(a))

    #SC[i]=ac
    #SC[i+1]=ac+1
    #i+=2
    #ac+=1
    #C.append(2)

for c in C:
    for j in range(i,i+c):
        SC[j]=ac
    i+=c
    try:
        SC[i]=ac
        SC[i+1]=ac+1
    except:
            break
    ac+=1
    i+=2


#print(i,ac)
#print(C)
#print(SC)


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

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
lis = []
for _ in range(Q):
    l, r = map(int, input().split())
    lis.append((l, r))
count = 0
count_lis = [0, 0]
pre_string = S[0]
for string in S[1:]:
    if pre_string == 'A' and string == 'C':
        count += 1
    count_lis.append(count)
    pre_string = string
for (l, r) in lis:
    print(count_lis[r] - count_lis[l])

Submission status: Accepted

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

Submission status: Accepted

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

AC = [0] * N

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

for l, r in zip(L, R):
    print(AC[r-1]-AC[l-1])

Submission status: Accepted

Solution code:
import bisect
import sys
input = sys.stdin.readline
n, q = [ int(v) for v in input().split() ]
s = input()
ac_list = []

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


for i in range(q):
	l, r = [ int(v) for v in input().split() ]
	a = (bisect.bisect_left(ac_list, l-1))
	b = (bisect.bisect_right(ac_list, r-2))
	print(b-a)

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 = input().strip().split()
N = int(N)
Q = int(Q)
S = input()

counts = [0 for _ in range(len(S))]
for i in range(1, len(S)):
  if S[i] == 'C' and S[i-1] == 'A':
    counts[i] = counts[i-1] + 1
  else:
    counts[i] = counts[i-1]
    
for q_i in range(Q):
  l, r = input().strip().split()
  l = int(l)-1
  r = int(r)-1  
  print(counts[r] - counts[l])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
"16時57分開始"
N, Q = map(int, input().split())  # 長さNの文字列, Q個の問い
S = str(input())

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

# print(ac_counter)

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

Submission status: Accepted

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

ac_list = [0]

for i in range(0, len(s)-1):
    if s[i] == 'A' and s[i+1] == 'C':
        ac_list.append(ac_list[i]+1)
    else:
        ac_list.append(ac_list[i])

#print(ac_list)

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())
S = input()
list = [[0 for i in range(2)] for j in range(Q)]

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

Submission status: Accepted

Solution code:
#Coding:Utf-8

N,Q=input().split()
S=input()
S= S.replace("AC","Ac")


def judge(x):
  if x=="c":
    return 1
  else:
    return 0


t=[]
t.append(0)

for i in range(1,int(N)):
  t.append(t[i-1]+judge(S[i]))

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

Submission status: Accepted

Solution code:
# coding: utf-8

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()))
    ans = t[r-1] - t[l-1]
    print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
from operator import add

N, Q = map(int, input().split())
S = input()
idx_list = [1 if S[i:i + 2] == 'AC' else 0 for i in range(len(S) - 1)]
idx_list_cumsum = [0] + list(accumulate(idx_list, add))

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

Submission status: Accepted

Solution code:
import sys
# input = sys.stdin.readline

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

cnt = 0
table = [0] * N
for i, Si in enumerate(S):
    if i == 0: continue
    elif Si == 'C':
        if S[i - 1] == 'A':
            cnt += 1
    table[i] = cnt

for i, (li, ri) in enumerate(lr):
    ans = table[ri - 1]
    if li > 1:
        ans -= table[li - 2]
        if S[li - 2] == 'A' and S[li - 1] == 'C': ans -= 1
    print(ans)

Submission status: Accepted

Solution code:
import sys
imput =sys.stdin.readline
N, Q =map(int,input().split())
S =[i for i in input()]
q = [[int(i) for i in input().split()] for _ in range(Q)]
num = 0
for i in range(N-1):
    if S[i]+S[i+1]=="AC":
        num += 1
    S[i] = num
S[-1] = num
S.append(0)
for lr in q:
  print(S[lr[1]-2] -S[lr[0]-2])

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


# aとbの最大公約数
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


# aとbの最小公倍数
def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


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

    v = [0] * (10**5 + 100)
    for i in range(N - 1):
        if S[i] == "A" and S[i + 1] == "C":
            v[i + 1] = 1

    for i in range(len(v) - 1):
        v[i + 1] += v[i]

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

cnt =[0]
for a,b in zip(S,S[1:]):
  if a == 'A' and b == 'C':
    cnt.append(cnt[-1] + 1)    
  else:
    cnt.append(cnt[-1])

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys

input = sys.stdin.readline

def main():
    _ = input()
    s = input()

    freq = [0 for _ in range(len(s))]
    for i, c in enumerate(s):
        if i > 0 and s[i-1] == 'A' and c == 'C':
            freq[i] = freq[i-1] + 1
        else:
            freq[i] = freq[i-1]

    for l in sys.stdin:
        x, y = [int(e) for e in l.split()]
        print(freq[y-1]-freq[x-1])


main()

Submission status: Accepted

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

#a[i]:s[0]~s[i]までに何個ACがあったか
a = [0]*n
#end[i]:s[i]がACの'C'か
end = [False]*n
for i in range(1,n):
    if s[i] == 'C' and s[i-1] == 'A':
        a[i] = a[i-1]+1
        end[i] = True
    else:
        a[i] = a[i-1]

for i in range(q):
    L,R = map(int,input().split())
    L,R = L-1,R-1
    if L == 0:
        print(a[R])
    else:
        if end[L]:
            print(a[R]-a[L-1]-1)
        else:
            print(a[R]-a[L-1])

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
s = input()
l, r = [], []
for i in range(q):
    ls, rs = map(int, input().split())
    l.append(ls)
    r.append(rs)

c = [0 for i in range(n)]

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

for i in range(q):
    print(c[r[i]-1]-c[l[i]-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)]
a=[0]*(N+1)
for i in range(2,N+1):
    if S[i-2:i]=='AC':
        a[i]=a[i-1]+1
    else:
        a[i]=a[i-1]
for q in range(Q):
    print(a[lr[q][1]]-a[lr[q][0]])

Submission status: Accepted

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

    lis = [0 for _ in range(len(S))]
    for i in range(len(S)):
        if i > 0:
            lis[i] += lis[i - 1]
        if i < len(S) - 1 and S[i] == 'A' and S[i + 1] == 'C':
            lis[i + 1] += 1

    for _ in range(Q):
        L = tuple(map(int, input().split()))
        print(lis[L[1] - 1] - lis[L[0] - 1])


if __name__ == '__main__':
    main()

Submission status: Accepted

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

acc = [0] * (n+5)

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

Submission status: Accepted

Solution code:
n,q=map(int,input().split())
s=input()
c=[0]
t=0

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

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

Submission status: Accepted

Solution code:
#累積和による解法
#計算量 O(N+Q)

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,q=map(int,input().split())
s=input()
i=0
bb=[]
c=0
while True:
    b=s.find('AC', i)
    if b<0:
        bb.extend([c]*(len(s)-i))
        break
    bb.extend([c]*(b-i+1))
    i=b+1
    c+=1
for _ in range(q):
    l,r=map(int,input().split())
    print(bb[r-1]-bb[l-1])

Submission status: Accepted

Solution code:
import bisect

def sub():
    N, Q = [int(e) for e in input().split(" ")]
    cc = [c for c in input()]
    qs = [input() for _ in range(Q)]
    qs = [[int(e) for e in q.split(" ")] for q in qs]
    
    buf_left = []
    buf_right = []
    for i in range(N-1):
        if cc[i] == "A" and cc[i+1] == "C":
            buf_left.append(i+1)
            buf_right.append(i+2)
    # print(buf_left)
    for left, right in qs:
        index_left = bisect.bisect_left(buf_left, left)
        index_right = bisect.bisect_right(buf_right, right)
        # print(index_left, index_right)
        print(index_right-index_left)

sub()

Submission status: Accepted

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

ac_count = [0]
ac = 0
for i in range(1, n):
    if arr[i-1:i+1] == "AC":
        ac += 1
    ac_count.append(ac)

ans = []

for _ in range(q):
    left, right = map(int, input().split())
    ans.append(ac_count[right-1] - ac_count[left-1])

print(*ans, sep="\n")

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-1:i+1] == 'AC':
        t[i] = t[i-1] + 1
    else:
        t[i] = t[i-1]
#    print(t)

for _ 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()
c=[0]
t=0
for i in range(1,n):
    t+=1 if (s[i-1]=='A' and s[i]=='C') else 0
    c.append(t)
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()
t = [0]*(N+1)
for i in range(len(S)-1):
    if S[i] == 'A' and S[i+1] == 'C':
        t[i+1] = 1

import numpy
res = numpy.cumsum(t)

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

Submission status: Accepted

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

counts = [0]*N

count = 0
for i, (a, b) in enumerate(zip(S, S[1:])):
    if a == 'A' and b == 'C':
        count += 1
        counts[i+1] = count
    else:
        counts[i+1] = count

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

Submission status: Accepted

Solution code:

x = input().split()
n = int(x[0]) # n == 比較文字列の長さ
q = int(x[1]) # q == 検討する回数
s = input()

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



def sc():
    global t
    global s
    global n
    global q
    global idbox
    maxx = 0
    count = 0
    com = [int(s)-1 for s in input().split()]
    
    
    print(t[com[1]-1] - t[com[0]-1])
        
    

    

for i in range(q):
    
    sc()

Submission status: Accepted

Solution code:
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 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,0]
for i in range(1,len(S)):
    if S[i-1] == "A" and S[i] == "C":
        AC.append(AC[-1]+1)
    else:
        AC.append(AC[-1])
for i 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()
cumurative_sum_lst = [0]*(N+1)
for i in range(N-1):
    if S[i] == "A" and S[i+1] == "C":
        cumurative_sum_lst[i+1] = cumurative_sum_lst[i] + 1
    else:
        cumurative_sum_lst[i+1] = cumurative_sum_lst[i]

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

    tmp = cumurative_sum_lst[r] - cumurative_sum_lst[l]
    outputs.append(int(tmp))

for i in outputs:
    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)]

ac_count_before = [0]
for i in range(1, n):
    counter = ac_count_before[-1]
    if s[i-1] == 'A' and s[i] == 'C':
        counter += 1
    ac_count_before.append(counter)

for i in range(q):
    l = lr[i][0] - 1
    r = lr[i][1] - 1
    counter = ac_count_before[r] - ac_count_before[l]
    print(counter)

Submission status: Accepted

Solution code:
n, q = list(map(int, input().split()))
s = input()
cnt_list = [0] * n
tmp = ""
cnt = 0
for i in range(1, len(s)):
    if s[i-1]=="A" and s[i]=="C":
        cnt += 1
    cnt_list[i] = cnt
for _ in range(q):
    l, r = list(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 = list(input())

#a[i]:s[0]~s[i]までに何個ACがあったか
a = [0]*n
#end[i]:s[i]がACの'C'か
end = [False]*n
for i in range(1,n):
    if s[i] == 'C' and s[i-1] == 'A':
        a[i] = a[i-1]+1
        end[i] = True
    else:
        a[i] = a[i-1]
for i in range(q):
    L,R = map(int,input().split())
    L,R = L-1,R-1
    if L == 0:
        print(a[R])
    else:
        if end[L]:
            print(a[R]-a[L-1]-1)
        else:
            print(a[R]-a[L-1])

Submission status: Accepted

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

Submission status: Accepted

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

c=[0]
t = 0
for i in range(1,N):
   t += 1 if  (S[i-1]=='A' and S[i] == 'C') else 0
   c.append(t)

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

Submission status: Accepted

Solution code:
n, nQueries = map(int,input().split())
inString = input()
queries = [ [int(x)-1 for x in input().split()] for i in range(nQueries)]
occurance_list = [0]*n
idx = 0
while(idx < n):
    if(idx<n-1 and inString[idx] == "A" and inString[idx+1] == "C"):
        occurance_list[idx] = occurance_list[idx-1]
        occurance_list[idx+1] += occurance_list[idx-1] + 1
        idx += 2
    else:
        occurance_list[idx] = occurance_list[idx-1]
        idx+=1



for (i,j) in queries:
    print(occurance_list[j] - occurance_list[i])

Submission status: Accepted

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

cnt = 0
num = []
for i in range(len(s) - 1):
    if (s[i:i + 2] == 'AC'):
        cnt += 1
        num.append(cnt)
    else:
        num.append(cnt)
for i in l:
    temp = num[i[1]] - num[i[0]]
    if i[1] - i[0] > 0:
        if (s[i[0]:i[0] + 2] == 'AC'):
            temp += 1
        if (s[i[1]:i[1] + 2] == 'AC'):
            temp -= 1 
    print(temp)

Submission status: Accepted

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

for i in range(Q):
  begin, end = map(int,input().split())
  print(AC_cnt[end-1]-AC_cnt[begin-1])

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from bisect import bisect_left
from bisect import bisect_right


def solve(N: int, Q: int, S: str, l: "List[int]", r: "List[int]"):
    aclist = [] 
    end = N - 1
    for i in range(end):
        if S[i] == 'A' and S[i+1] == 'C':
            aclist.append(i)

    len_ac = len(aclist)

    for i in range(Q):
        res = 0
        begin = l[i] - 1
        end   = r[i] - 2

        s_start = bisect_left(aclist,begin)
        s_end   = bisect_right(aclist,end)

        print(s_end-s_start)
            


# Generated by 1.1.4 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()

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

# Cumulative sum
b = [0] * (n + 1)
for i in range(n):
    b[i+1] = b[i] + a[i]

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

sum_table = [0] * (N+1)

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

ans_table = []
for i in range(Q):
    left, right = map(int, input().split())
    ans_table.append(sum_table[right-1] - sum_table[left-1])

for i in range(len(ans_table)):
    print(ans_table[i])

Submission status: Accepted

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

sum_table = [0] * (N+1)

for i in range(N):
    sum_table[i+1] = sum_table[i] + (1 if S[i:i+2] == "AC" else 0)
for i in range(Q):
    left, right = map(int, input().split())
    print(sum_table[right-1] - sum_table[left-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:
N, Q = [int(nq) for nq in input().split()]
S = input()
AC = []
for s in range(len(S)-1):
    if S[s:s+2] == "AC":
        AC.append(1)
    else:
        AC.append(0)
CS = [0]*len(AC)
CS[0] = AC[0]
for i in range(len(AC)-1):
    CS[i+1] = CS[i] + AC[i+1]
for q in range(Q):
    l, r = [int(lr) for lr in input().split()]
    if l == 1:
        print(CS[r-2])
    else:
        print(CS[r-2] - CS[l-2])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
ruisekiwa = [0]
lists = []
cnt = 0
S = str(input())
for t in range(1, N):
    if S[t - 1] == "A" and S[t] == "C":
        cnt += 1
    ruisekiwa.append(cnt)
ruisekiwa.append(cnt)
for i in range(Q):
    l, r = map(int, input().split())
    ans = ruisekiwa[r - 1] - ruisekiwa[l - 1]
    lists.append(ans)
for m in range(len(lists)):
    print(lists[m])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N,Q=map(int,input().split())
S=input()
Sum=[0,0]
for i in range(1,N):
    if S[i-1]=='A'and S[i]=='C':
        Sum.append(Sum[-1]+1)
    else:
        Sum.append(Sum[-1])
for i in range(Q):
    l,r=map(int,input().split())
    print(Sum[r]-Sum[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-1:i+1] == "AC" else 0)

lr = []
for i in range(Q):
    tmp_lr = list(map(int, input().split()))
    lr.append(tmp_lr)

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

Submission status: Accepted

Solution code:
n, q=map(int,input().split())
str=input() #ただの文字列
q_list=[input().split() for _ in range(q)]  #問題の数が入ったリスト
pattern='AC'

#strのi番目までににACが何個あるか
count_ac=[0 for i in range(len(str))]
count=0
for i in range(len(count_ac)-1):
    if str[i]=='A' and str[i+1]=='C':
        count+=1
        count_ac[i]=count
    else:
        count_ac[i]=count
count_ac[len(str)-1]=count    
for a,b in q_list:
    A,B=int(a),int(b)
    if A>=2:
        print(count_ac[B-2]-count_ac[A-2])
    elif B>=2 and A<2:
        print(count_ac[B-2])
    else:
        print(0)

Submission status: Accepted

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

c = [0] * n

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

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

Submission status: Accepted

Solution code:
from itertools import accumulate


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

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

flg2 = [0] + flg
flg2 = list(accumulate(flg2))

for a in A:
    print(flg2[a[1]-1] - flg2[a[0]-1])

Submission status: Accepted

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#N,Q=8,3
#S="ACACTACG"
#L = [3,2,1]
#R = [7,3,8]

N, Q = map(int, input().split(" "))
S = input()
L = []
R = []
for _ in range(Q):
    l, r = map(int, input().split(" "))
    L.append(l)
    R.append(r)

sh = [0]
n = 0
for i in range(1, N):
    if S[i - 1] == "A" and S[i] == "C":
        n += 1
    sh.append(n)
for (l, r) in zip(L, R):
    print(sh[r - 1] - sh[l - 1])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon May 13 17:55:54 2019

@author: avina
"""

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:
from sys import stdin

sN, sQ = stdin.readline().split()
N, Q = int(sN), int(sQ)
# print(N,Q)
S = stdin.readline().rstrip()
data = [stdin.readline().split() for itr in range(Q)]
# print(data)

grid = [None] * N
# print(grid)
count = 0
for num in range(N):
    if num == 0:
        grid[num] = count
        continue
    if S[num] == 'C' and S[num-1] == 'A':
        count += 1
        grid[num] = count
        continue
    grid[num] = count

# print(grid)

for itr in range(Q):
    l = int(data[itr][0]) - 1
    r = int(data[itr][1]) - 1
    # print(grid[r],grid[l])
    print(grid[r]-grid[l])

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

import bisect
left = bisect.bisect_left
right = bisect.bisect_right 

n, q = list(map(int, input().split()))
s = input()
lr = [list(map(int, input().split())) for i in range(q)]
a = [i for i in range(n-1) if s[i:i+2]=="AC"]

for i in range(q):
  left_index = left(a, lr[i][0]-1)      
  right_index = right(a, lr[i][1]-2)  
  print(right_index - left_index)

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