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:
n,q = map(int,input().split())
#s=list(input())	
s = input()
index = -1
indexes = []
while True:
    index = s.find("AC", index + 1)
    if index == -1:
        break
    indexes.append(index)
dp = []

for i in range (n):
    dp.append(len(indexes))
    if len(indexes)> 0 and indexes[0] == i:
        indexes.pop(0)
for i in range(q):
    l,r = map(int,input().split())
    print(dp[l-1] - dp[r-1])

Submission status: Accepted

Solution code:
N,Q = map(int,input().split())
S = input()
AC = [0] * N
buffer = []
lr =[]
for i in range(Q):
  buffer = list(map(int,input().split()))
  lr.append(buffer)
counts = 0
for i in range(N):
  if S[i:i+2] == "AC":
    counts +=1
  AC[i] =  counts
a =[0]
AC = a + AC
for q in range(Q):
  #print(str(lr[q][0])+" "+ str(lr[q][1]))
  print(AC[lr[q][1]-1] - AC[lr[q][0]-1])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = [ _ for _ in input()]
lr = [[int(_) for _ in input().split()] for _ in range(Q)]
ans = [0] * (N + 1)

for i, data in enumerate(S, 1):
    if data == "A":
        if i >= N:
            break

        if S[i] == "C":
            ans[i] = ans[i - 1] + 1
            continue

    ans[i] = ans[i - 1]

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

Submission status: Accepted

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

#def f(i):
#    cnt = 0
#    for j in range(i-1):
#        #print(s[j:j+2])
#        if s[j:j+2] == 'AC':
#            cnt += 1
#    return cnt

ftab = [0]*(n+1)
cnt = 0

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

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

Submission status: Accepted

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

l_list = []
r_list = []

s_count = [0]
for i in range(len(S) - 1):
    if S[i:i+2] == 'AC':
        s_count.append(1)
    else:
        s_count.append(0)
#print(s_count)

n_count = []
n_sum = 0
for n in s_count:
    n_sum = n_sum + n
    n_count.append(n_sum)

#print(n_count)


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

Submission status: Accepted

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

def main():
	N, Q = map(int, input().split(" "))
	S = input()
	S = list(S)
	
	Qs = []
	for _ in range(Q):
		l, r = map(int, input().split(" "))
		Qs.append((l, r))
	
	ptn = []
	hit = 0
	flg = False
	for i, val in enumerate(S):
		if val == "A":
			ptn.append(hit)
			if i+1 < len(S) and S[i+1] == "C":
				hit += 1
				ptn.append(hit)
				flg = True
		else:
			if flg == False:
				ptn.append(hit)
			else:
				flg = False
		
	ans = []
	for i, val in enumerate(Qs):
		l, r = val
		if l == r:
			ans.append(0)
			break
			
		tmp = ptn[r-1] - ptn[l-1]
		ans.append(tmp)
	
	
	for i in ans:
		print(i)
	
if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
#import re
N, Q = map(int, input().split())
S = input()
#iter = re.finditer("AC", S)
#indices_C = [m.start(0)+1 for m in iter]
#total = len(indices_C)
#
#dec_l_list = []
#dec_l = 0
#dec_r_list = []
#dec_r = total
#for i in range(N):
#    if i in indices_C:
#        dec_l += 1
#        dec_r -= 1
#    dec_l_list.append(dec_l)
#    dec_r_list.append(dec_r)

flag = False
dec_l_list = []
total = 0
for i in range(N):
    if flag and S[i] == "C":
        total += 1
    dec_l_list.append(total)
    flag = True if S[i] == "A" else False
dec_r_list = [total - x for x in dec_l_list]

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

Submission status: Accepted

Solution code:
import bisect
N, Q = map(int, input().split())
S = input()
numlist = []
for i in range(N-1):
    if S[i:i+2] == 'AC':
        numlist.append(i)

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

for i in range(Q):
    hajime = bisect.bisect_left(numlist, l[i])
    owari = bisect.bisect_right(numlist,r[i]-1)
    print(owari - hajime)

Submission status: Accepted

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

arr = [0 for j in range(N)]
A_tmp = 0
S_tmp = 0

for i in range(N):
    if S[i] == "A":
        A_tmp = 1
        arr[i] = S_tmp
    elif A_tmp == 1 and S[i] == "C":
        S_tmp += 1
        arr[i] = S_tmp
        A_tmp = 0
    else:
        arr[i] = S_tmp
        A_tmp = 0

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

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

Submission status: Accepted

Solution code:
N,Q=list(map(int,input().split()))
S=input()
place=[0]*(N+1)
for i in range(0,len(S)-1):
    if S[i:i+2]=='AC':
        place[i+2]+=1
        
for i in range(N):
    place[i+1]=place[i]+place[i+1]
for i in range(Q):
    frm,to=list(map(int,input().split()))
    print(place[to]-place[frm])
#print(place)

Submission status: Accepted

Solution code:
import re
N,Q=[int(s) for s in input().split()]
S=input()
table=[1 if S[0:2] == "AC" else 0]
for i in range(1, N-1):
    table.append(table[i-1]+(1 if S[i:i+2] == "AC" else 0))
table.append(table[-1])
for i in range(Q):
    (li, ri)=[int(s) for s in input().split()]
    print(table[ri-2]-(table[li-2] if li > 1 else 0))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8-*-
if __name__ == '__main__':
    n, q = map(int, input().split(" "))
    s = input()
    c = [0]
    ql = []
    for i in range(q):
        r, l = map(int, input().split(" "))
        ql.append((r,l))
    for i in range(1, len(s)):
        if s[i-1:i+1] == "AC":
            c.append(c[i-1]+1)
        else:
            c.append(c[i-1])
    for r, l in ql:
        print(c[l-1] - c[r-1])

Submission status: Accepted

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

Submission status: Accepted

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


main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 2019/3/24
@author: shinjisu
"""


# ABC 122
# import math
def getInt(): return int(input())
def getIntList(): return [int(x) for x in input().split()]
# def getIntList(): return np.array(input().split(), dtype=np.longlong)
def getIntLines(n): return [int(input()) for i in range(n)]
def getIntMat(n, m):  # n行に渡って、1行にm個の整数
    #mat = zeros((n, m))
    mat = zeros2(n, m)
    for i in range(n):
        mat[i] = getIntList()
    return mat

def zeros(n): return [0]*n
# def zeros(n): return np.zeros(n, dtype=np.longlong)
def zeros2(n, m): return [zeros(m) for i in range(n)] # obsoleted zeros((n, m))で代替

ALPHABET = [chr(i+ord('a')) for i in range(26)]
DIGIT = [chr(i+ord('0')) for i in range(10)]
N1097 = 10**9 + 7
INF = 10**18

class Debug():
    def __init__(self):
        self.debug = True
 
    def off(self):
        self.debug = False
 
    def dmp(self, x, cmt=''):
        if self.debug:
            if cmt != '':
                print(cmt, ':  ', end='')
            print(x)
        return x


def prob_TLE():
    d = Debug()
    #d.off()
    N, Q = getIntList()
    d.dmp((N, Q), 'N, Q')
    S = input()
    d.dmp((S), 'S')
    for i in range(Q):
        L, R = getIntList()
        d.dmp((L, R), 'L, R')
        j = L-1
        count = 0
        while j < R-1:
            d.dmp(j,'j')
            d.dmp(S[j:j+2],'S[j:j+2]')
            if S[j:j+2] == 'AC':
                count += 1
                j += 2
            else:
                j += 1
        print(count)
    return None

def prob():
    d = Debug()
    d.off()
    N, Q = getIntList()
    d.dmp((N, Q), 'N, Q')
    S = input()
    d.dmp((S), 'S')
    aclist = zeros(N)  # 左にいくつＡＣがあるか
    j = 0
    R = len(S)
    count = 0
    while j < R:
        #d.dmp(j,'j')
        #d.dmp(S[j:j+2],'S[j:j+2]')
        if S[j:j+2] == 'AC':
            aclist[j] = count
            count += 1
            aclist[j+1] = count
            j += 2
        else:
            aclist[j] = count
            j += 1
    d.dmp((aclist), 'aclist')
   
    for i in range(Q):
        L, R = getIntList()
        #d.dmp((L, R), 'L, R')
        print(aclist[R-1]-aclist[L-1])

    return None

ans = prob()
if ans is None:
    pass
elif type(ans) == tuple and ans[0] == 1:  # 1,ans
    for elm in ans[1]:
        print(elm)
else:
    print(ans)

Submission status: Accepted

Solution code:
n,q = [int(i) for i in input().split()]
s = input()
cumsum = [0]*n
ans = 0
flag = False

for i in range(n):
    if flag:
        if s[i] == "C":
            ans += 1

    cumsum[i] = ans     

    flag = False

    if s[i] == "A":
        flag = True
    


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

Submission status: Accepted

Solution code:
from sys import stdin
from collections import deque

N, Q = [int(x) for x in stdin.readline().rstrip().split()]
S = stdin.readline().rstrip()
lr_list = [list(map(int, stdin.readline().rstrip().split())) for _ in range(Q)]

#result_list = []
count = 0

result_list = deque([])

for i in range(N):
    result_list.append(count)
    if S[i] != "A":
        continue
    try:
        if S[i+1] == "C":
            count += 1
    except:
       pass

for l, r in lr_list:
    print(result_list[r-1] - result_list[l-1])

Submission status: Accepted

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

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

for _ in range(Q):
    li, ri = map(int, input().split())
    print(counts[ri - 1] - counts[li - 1])

Submission status: Accepted

Solution code:
def solve(lr,N,Q,S):
    #ACの総数をindex0~N-1まで全てだす
    count = [0]*N
    sum = 0
    for i in range(1,N):
       
        if S[i-1]=='A' and S[i]=='C':
            sum += 1
            count[i] = sum
        else:
            count[i] = sum

    # print(count)
    
    for i in range(Q):
        # print(int(lr[i][1]),int(lr[i][0]))
        print(count[int(lr[i][1])-1]-count[int(lr[i][0])-1])
    #     # print(S[int(lr[i][0])-1:int(lr[i][1])].count('AC'))



 
def main():
    N,Q= map(int,input().split())
    S = input()
    lr = [0]*Q
    for i in range(Q):
        lr[i]=input().rstrip().split()
    # S = 'ACACTACG'
    # N = 8
    # Q = 3
    # lr = [['3', '7'], ['2', '3'], ['1', '8']]
    solve(lr,N,Q,S)
 
 
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np

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

flags = [0] * len_S
for i, v in enumerate(S[:-1]):
  if v == 'A' and S[i + 1] == 'C':
    flags[i + 1] = 1
  else:
    flags[i + 1] = 0

sums = list(np.cumsum(flags))

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

Submission status: Accepted

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

ans = [0]
num = 0
for x, y in zip(S[:-1], S[1:]):
    if x == 'A' and y == 'C':
        num += 1
        ans.append(num)
    else:
        ans.append(num)

for _ in range(Q):
    l, r = map(int, input().split())
    if l == 1:
        print(ans[r-1])
    else:
        b = ans[r-1]-ans[l-2]
        if S[l-1] == 'C' and S[l-2] == 'A':
            print(b-1)
        else:
            print(b)

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
s = input()
lr = [list(map(int, input().split())) for _ in range(q)]
a = [0] * 100010
count = 0 
for i in range(1,len(s)):
    if s[i] == 'C' and s[i-1] == 'A':
        count += 1
    a[i] = count
# print(a)
for index in lr:
    # print(index)
    print(a[index[1]-1] - a[index[0]-1])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 16 20:52:46 2019

@author: Owner
"""


import collections
import scipy.misc
import sys
import numpy as np
import math
from operator import itemgetter
import itertools
import copy
import bisect

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default


"""
N, X = map(int, input().split())

x = [0]*N
x[:] = map(int, input().split())

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())

    
all(nstr.count(c) for c in '753')

# 複数配列を並び替え
ABT = zip(A, B, totAB)
result = 0
# itemgetterには何番目の配列をキーにしたいか渡します
sorted(ABT,key=itemgetter(2))
A, B, totAB = zip(*ABT)
A.sort(reverse=True)

# 2進数のbit判定
(x >> i) & 1

# dp最小化問題
dp = [np.inf]*N
for n in range(N):
    if n == 0:
        dp[n] = 0
    else:
        for k in range(1,K+1):
            if n-k >= 0:
                dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k]))
            else:
                break

"""


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

p_ac = []
n = 0
while True:
    tmp = S.find('AC', n, N+1)
    if tmp == -1:
        break
    else:
        p_ac.append(S.find('AC', n, N+1))    
        n = tmp+1
l = [0]*Q
r = [0]*Q
for i in range(Q):
    l[i], r[i] = map(int, input().split())
    li, ri = bisect.bisect_left(p_ac, l[i]-1), bisect.bisect_right(p_ac, r[i]-2)       
    #print(p_ac, li, ri, len(S[li:ri]))
    print( len(S[li:ri]))

Submission status: Accepted

Solution code:
n,q = map(int, input().split())
s = input()
lr = [list(map(lambda x: int(x)-1, input().split())) for _ in range(q)]

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

Submission status: Accepted

Solution code:
def main():
    N, Q = map(int, input().split())
    S = input()
    s_list = [str(c) for c in S]
    q_list = [list(map(int, input().split())) for i in range(Q)]

    ac_list = list()
    # get AC start point
    for i, n in enumerate(s_list):
        if n == "A" and i != (len(s_list) - 1):
            if s_list[i + 1] == "C":
                ac_list.append(i + 1)

    # set list
    temp = list()
    pre = 0
    counter = 0
    for s in ac_list:
        for i in range(pre, s):
            temp.append(counter)
        pre = s
        counter += 1

    for i in range(pre, N):
        temp.append(counter)

    # answer Q
    for qs in q_list:
        print(temp[qs[1] - 1] - temp[qs[0] - 1])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect
N,Q=[int(i) for i in input().split()]
S=input()
AC=list()
for i in range(len(S)-1):
    if S[i]=='A' and S[i+1]=='C':
        AC.append(i)
#print(AC)
for i in range(Q):
    L,R=[int(i)-1 for i in input().split()]
    R-=1
    ll=bisect.bisect_left(AC,L)
    rr=bisect.bisect_right(AC,R)
    print(rr-ll)

Submission status: Accepted

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

S=input()

# 最初からNまでに何個ACがあったか
cnt=[0 for _ in range(N)]

prev=""

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

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

Submission status: Accepted

Solution code:
N, Q=map(int,raw_input().split())
S=raw_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]
for _ in range(Q):
  l,r=map(int,raw_input().split())
  ans=T[r-1]
  if l>1:
    ans-=T[l-2]
    if S[l-2:l]=="AC":
      ans-=1
  print ans

Submission status: Accepted

Solution code:
import sys
import bisect
input = sys.stdin.readline
def 解():
    N,Q=[int(_) for _ in input().split()]
    S = input()
    aC = [-1]
    iC = 0
    bF = False
    for s in S:
        iC += 1
        if s == 'A':
            bF = True
        elif s == 'C' :
            if bF:
                aC.append(iC)
            bF = False
        else:
            bF = False

    aC.append(10**9)
    aAns = []
    for _ in range(Q):
        l,r = [int(_) for _ in input().split()]
        iL = bisect.bisect_left(aC,l)
        if l == aC[iL]:
            iL += 1
        iR = bisect.bisect_left(aC,r)
        if aC[iR] <= r:
            iR += 1
        aAns.append(iR-iL)
    print(*aAns,sep="\n")
解()

Submission status: Accepted

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

char_prev = "0"
char_now = "0"
for i in S:
    if char_prev == "0":
        char_prev = i;
        check_AC.append(0);
    else:
        char_now = i;
        if(char_prev + char_now == "AC"):
            check_AC.append(check_AC[-1]+1);
        else:
            check_AC.append(check_AC[-1]);
        char_prev = i;

answer = []

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

for i in answer:
    print(i);

Submission status: Accepted

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

ans_list = []
fa = 0
ans = 0
for index, s in enumerate(S):
    if s == 'A' and fa == 0:
        fa = 1
        ans_list.append(ans)
    elif s == 'A' and fa == 1:
        ans_list.append(ans)
    elif s == 'C' and fa == 1:
        fa = 0
        ans += 1
        ans_list.append(ans)
    else:
        fa = 0
        ans_list.append(ans)
for _ in range(Q):
    l, r = map(int, input().split())
    print(ans_list[r-1] - ans_list[l-1])

Submission status: Accepted

Solution code:
from itertools import accumulate

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

ac = [0] + ac
acc = list(accumulate(ac))
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()
l = [[int(i) for i in input().split()] for i in range(q)]
c = [0 for i in range(n)]
cc = 0
f = False
for i in range(n):
    if f:
        f = False
        if s[i] == "C":
            cc += 1
    if s[i] == "A":
        f = True
    c[i] = cc
for i in range(q):
    print(c[l[i][1]-1]-c[l[i][0]-1])

Submission status: Accepted

Solution code:
n, q = map(int,input().split())
s = input()
ll = len(s)
t = [0]
for i in range(ll-1):
    if s[i:i+2]=="AC":
        t.append(t[i]+1)
    else:
        t.append(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:
import bisect
n, q = map(int, input().split())
s = input()
ac = 'AC'
args = []
for i in range(len(s) - 1):
    if s[i:i+2] == 'AC':
        args.append(i)
for j in range(q):
    l, r = map(int, input().split())
    i_l = bisect.bisect_left(args, l-1)
    i_r = bisect.bisect_right(args, r-2)
    print(i_r - i_l)

Submission status: Accepted

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

start_position = []
prev = ""
counter = 0
for s in S:
    if s == "C" and prev == "A":
        counter += 1
        start_position.append(counter)
    else:
        start_position.append(counter)
    prev = s

for _ in range(Q):
    l, r = list(map(int, input().split()))
    print(start_position[r-1] - start_position[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]=="A" and S[i]=="C":
        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:
#coding: utf-8
import bisect

n, q = map(int, raw_input().split())
s = raw_input()
i_list = []
i = -1
while True:
    i = s.find("AC", i+1)
    if(i == -1): break
    i_list.append(i)
for i in range(q):
    a, b = map(int, raw_input().split())
    a = bisect.bisect_left(i_list, a-1)
    b = bisect.bisect_left(i_list, b-1)
    print(b-a)

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
char = input()
num_list = [0]
for i in range(1,N):
  if char[i-1:i+1] == 'AC':
    num_list.append(num_list[-1]+1)
  else:
    num_list.append(num_list[-1])

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def f():
    return map(int, input().split())


def main():
    N, Q = f()
    S = input()
    t = []
    flag = False
    n = 0
    for i, s in enumerate(S):
        if s == 'A':
            flag = True
        elif flag and s == 'C':
            n += 1
            flag = False
        else:
            flag = False
        t.append(n)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

cnt = 0
li = []

for i in range(len(S)):
    if i == len(S)-1:
        li.append(cnt)
    elif S[i] == "A" and S[i+1] == "C":
        li.append(cnt)
        cnt += 1
    else:
        li.append(cnt)

#print(li)
#exit()

for k in range(Q):
    print(li[lr[k][1]-1] - li[lr[k][0]-1])

Submission status: Accepted

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

num_list = []
tochu = [0 for _ in range(N)]
tochu2 = [0 for _ in range(N)]

state = 0
count = 0
for t_i, t in enumerate(S): 
    if "A" == t:
        state = 1
    elif "C" == t and state == 1:
        tochu[t_i-1] = 1
        tochu2[t_i] = 1
        state = 0
        count += 1
    else:
        state = 0
    num_list.append(count)

#print(num_list, tochu)
outs = []
for Q_i in range(Q):
    l, r = list(map(int, input().split(" ")))
    l -= 1
    r -= 1
    #out = max(0, num_list[r] - num_list[l] - tochu2[l])
    tmp = 0 if l -1 < 0 else num_list[l-1]
    out = num_list[r] - tmp - tochu2[l]
    outs.append(out)

for out in outs:
    print(out)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

n,q = read_int()
S = read_str()
lr = [read_int() for _ in range(q)]

dp = [0 for i in range(n+1)]
def solve():
    for i in range(1, n):
        dp[i+1] = dp[i]
        if S[i - 1:i + 1] == 'AC':
            dp[i +1] += 1

    for i in range(q):
        l,r = lr[i]
        print(dp[r]-dp[l])

        
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-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 pf(s): return print(s, flush=True)

N, Q = LI()
S = input()
"""
ACのインデックスをすべて記録しておき、始点、終点で区切って出す
少ない方を取ったほうがいいかな
"""
ACs = []
for sidx in range(0, len(S)-1):
    if S[sidx] == 'A':
        if S[sidx+1] == 'C':
            ACs.append(sidx)

#  print(ACs)

LR = []
for i in range(Q):
    LR.append(LI())

#  N, Q = 10**5, 10**5
#  S = 'ACTCTTCGTG'*10**4
#  LR = []
#  for i in range(Q):
#      LR.append([3,9000])

#  for l, r in LR:
#      tmp = S[l-1:r]
#      print(tmp.count('AC'))

result = []
#  print(ACs)
for l, r in LR:
    start = bisect.bisect_left(ACs, l-1)
    end = bisect.bisect_left(ACs, r-1)
    #  print('s,e', start, end)
    print(end-start)

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
s = input()
acc = [0 for _ in range(n)]
for i in range(1, n):
    acc[i] = acc[i - 1]
    if s[i - 1:i + 1] == "AC":
        acc[i] += 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()
lst=[list(map(int, input().split())) for i in range(q)] 
acwa=[0]*(n)
a=0
ac=0
for i in range(n):
  if(s[i]=="A"):
    a=1
  elif(s[i]=="C"):
    if(a==1):
      ac+=1
    a=0
  else:
    a=0
  acwa[i]=ac
for i in lst:
  ans=acwa[i[1]-1]-acwa[max(i[0]-2,0)]
  if(acwa[i[0]-1]!=acwa[max(i[0]-2,0)]):
    ans-=1
  print(ans)

Submission status: Accepted

Solution code:
import bisect
N, Q = map(int, input().split())
S = input()
lis = []
for i in range(len(S) - 1):
  if S[i] == "A" and S[i + 1] == "C":
    lis.append(i + 1)
for _ in range(Q):
  l, r = map(int, input().split())
  ko = bisect.bisect_left(lis, l)
  kk = bisect.bisect_left(lis, r)
  print(kk - ko)

Submission status: Accepted

Solution code:
n, q = [int(i) for i in input().split() ]
s = input() #文字列
lr = list([])
count = [0 for i in range(n)]
for i in range(q):
    lr.append([int(i) for i in input().split() ])

ll = list([])
now = 0
for i, c in enumerate(s):
    if((c == "C") and (i>0) and (s[i-1] =="A")):
        now += 1
    count[i] = now

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

Submission status: Accepted

Solution code:
# C
n, q = map(int, input().split())
s = str(input())
s_flag = [0] * n
v = 0
for i in range(n-1):
    if s[i:i+2] == 'AC':
        v += 1
    s_flag[i+1] = v

for i in range(q):
    l, r = map(int, input().split())
    l -= 1
    r -= 1
    ans = s_flag[r] - s_flag[l]
    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)]
l = [e[0]-1 for e in lr]
r = [e[1]-1 for e in lr]

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

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
n,q = map(int,input().split())
s = input()
a = [list(map(int,input().split())) for i in range(q)]
rui = [0] * n
for i in range(n-1):
        if s[i]+s[i+1] == "AC":
                rui[i+1] += rui[i] +1
        else:
                rui[i+1] = rui[i]
for i in range(q):
        tmp = rui[a[i][1]-1] - rui[a[i][0]-1]
        print(tmp)

Submission status: Accepted

Solution code:
# coding: utf-8

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

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

for l, r in l_r:
    print(isAC[r - 1] - isAC[l - 1])

Submission status: Accepted

Solution code:
import itertools

N,Q = map(int,input().split())
S = input()
info = [[int(e)-1 for e in input().split()] for _ in range(Q)]
note = [0]*(N)
for i in range(1,N):
    if S[i-1] == "A" and S[i] == "C":
        note[i] = 1
note = list(itertools.accumulate(note))
for l,r in info:
    if l == 0:
        ans = note[r]
    else:
        ans = note[r] - note[l-1]
        if S[l-1] == "A" and S[l] == "C":
            ans -= 1
    print(ans)

Submission status: Accepted

Solution code:
import bisect

N, Q = map(int, input().split())
S = input()
oc = 'X'
results = []
for i in range(len(S)): 
    c = S[i] 
    if oc+c == "AC":
      results.append(i)
    oc = c
#print(results)

LRs = [tuple(map(int,input().split())) for i in range(Q)]

for L, R in LRs:
    fi = bisect.bisect_left(results,L)
    li = bisect.bisect_left(results,R)
    #print(fi,li)
    # fi が右端、liが左端(存在しないときの処理)
    print(li - fi)

Submission status: Accepted

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

arr=[0]*N
for i,z in enumerate(arr):
  if i==0:
    continue
  if s[i-1]=="A" and s[i]=="C":
    arr[i]=arr[i-1]+1
  else:
    arr[i]=arr[i-1]
for z in range(0,Q):
  i=list(map(int,input().split(" ")))
  print(arr[i[1]-1]-arr[i[0]-1])