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 = (int(i) for i in input().split())
s = input()
x = [0,0]
for i in range(1,len(s)):
    if s[i]=="C" and s[i-1]=="A": x.append(x[-1]+1)
    else: x.append(x[-1])
for _ in range(q):
    l,r = (int(i) for i in input().split())
    print(x[r]-x[l])

Submission status: Accepted

Solution code:
#coding:utf-8

N,Q=map(int, raw_input().split())
S=raw_input()
P=[0]*(N+1)
for i in range(N):
    if i==0:
        P[0]=0
        continue
    if S[i-1:i+1]=="AC":
        P[i]=P[i-1]+1
    else:
        P[i]=P[i-1]
for _ in range(Q):
    l,r=map(int, raw_input().split())
    print(P[r-1]-P[l-1])

Submission status: Accepted

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

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

Submission status: Accepted

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

N,Q = (int(i) for i in input().split())
S = input()
acl = [0 for i in range(N)]
cnt = 0
for j in range(1,N):
  if S[j-1] == 'A' and S[j] == 'C':
    cnt += 1
  acl[j] = cnt
      
for k in range(Q):
  x,y = map(int, input().split())
  print(acl[y-1]-acl[x-1])

Submission status: Accepted

Solution code:
N,Q = (int(i) for i in input().split())
S = input()
acl = [0 for i in range(N)]
cnt = 0
for j in range(1,N):
  if S[j-1] == 'A' and S[j] == 'C':
    cnt += 1
  acl[j] = cnt
      
for k in range(Q):
  x,y = map(int, input().split())
  print(acl[y-1]-acl[x-1])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
L = [0 for _ in range(len(S)+3)]
cnt = 0
for i in range(len(S)-1):
    if S[i] + S[i+1] == "AC":
        cnt += 1
    L[i] = cnt
L = [0] +L
#print(L)
for _ in range(Q):
    x , y = map(int, input().split())
    x -= 1
    y -= 1
    print(L[y] - L[x])

Submission status: Accepted

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

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

# 0 1 1 1 2 2 3
# A C A A C A C
# 1 2 3 4 5 6 7
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()

acl = [0]*(N+1)

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

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

Submission status: Accepted

Solution code:
N,Q = map(int,input().split())
S = input()
l,r = [],[]
index = []
for i in range(Q):
    tmp1,tmp2 = map(int,input().split())
    l.append(tmp1)
    r.append(tmp2)
counter = 0
index.append(counter)
for i in range(1,len(S)):
    if(S[i-1] == 'A' and S[i] == 'C'):
        counter += 1
    index.append(counter)
def f(l,r):
    return index[r-1] - index[l-1]
for i in range(Q):
    print(f(l[i],r[i]))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
t = [0] * (N + 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 = list(input())
b = [[]*2 for _ in range(q)]
for i in range(q):
    b[i] = list(map(int, input().split()))

c = [0 for _ in range(len(s))]
count = 0

for i in range(len(s)):
    if s[i:i+2] == ["A", "C"]:
        count += 1
        c[i:i+2] = [count-1, count]
    else:
        c[i:i+2] = [count, count]

for i in range(q):
    start = b[i][0]-1
    end = b[i][1]-1
    ans = c[end] - c[start]
    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import bisect

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

for i in range(len(s)-1):
    if s[i] == "A" and s[i+1] == "C":
        ans.append(i+1)

for _ in range(q):
    l, r = map(int, input().split())
    L = bisect.bisect_left(ans, l)
    R = bisect.bisect_right(ans, r-1)
    print(R-L)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
from itertools import accumulate

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

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

Submission status: Accepted

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

for j in range(1,N):
  if S[j-1] == 'A' and S[j] == 'C':
    cnt += 1
  acl[j] = cnt
      
for k in range(Q):
  x,y = (int(i)-1 for i in input().split())
  print(acl[y]-acl[x])

Submission status: Accepted

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

for j in range(1,N):
  if S[j-1] == 'A' and S[j] == 'C':
    cnt += 1
  acl[j] = cnt
      
for k in range(Q):
  x,y = (int(i)-1 for i in input().split())
  print(acl[y]-acl[x])

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

flag = False
for i in range(N):
    if S[i] == "A":
        flag = True
        Sum[i+1] = Sum[i]
    else:
        if S[i] == "C":
            if flag:
                Sum[i+1] = Sum[i] + 1
            else:
                Sum[i+1] = Sum[i]
        else: Sum[i+1] = Sum[i]
        flag = False

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

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n,q=map(int,input().split())
s=input();a=[0]*2
for i in range(2,n+1):
 a+=[a[i-1]+s[i-2:i].count("AC")]
for _ in range(q):
 l,r=map(int,input().split())
 print(a[r]-a[l])

Submission status: Accepted

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

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

for i in range(n):
    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:
n,q=map(int,input().split())
s=input();a=[0]*2
for i in range(2,n+1):
 a+=[a[i-1]+s[i-2:i].count("AC")]
for _ in range(q):
 l,r=map(int,input().split())
 print(a[r]-a[l])

Submission status: Accepted

Solution code:
# # coding=utf-8
# # Author: lee215

get = lambda: int(raw_input().strip())
gets = lambda: [int(i) for i in raw_input().strip().split()]
getss = lambda n: [gets() for _ in xrange(n)]

import bisect


N, Q = gets()
S = raw_input()
query = getss(Q)


AC = []
for i in xrange(N):
    if S[i:i + 2] == "AC":
        AC.append(i)


# print AC
for i, j in query:
    i, j = i - 1, j - 1
    x = bisect.bisect_left(AC, i)
    y = bisect.bisect(AC, j - 1)
    print y - x
    # print i, j, y, x, y - x

Submission status: Accepted

Solution code:
n,q = map(int,input().split())
s = input()
l = [list(map(int,input().split())) for _ in range(q)]
sum = [0]*(n+1)
for i in range(len(s)-1):
    if s[i:i+2]=='AC':
        sum[i+2] = sum[i+1]+1
    else:
        sum[i+2] = sum[i+1]
for l_ in l:
    print(sum[l_[1]]-sum[l_[0]])

Submission status: Accepted

Solution code:
import bisect

N, Q = list(map(int, input().split()))
S = list(input())
S2 = []
for i in range(N - 1):
    if S[i] == "A" and S[i + 1] == "C":
        S2.append(i)
for i in range(Q):
    l, r = list(map(int, input().split()))
    L = bisect.bisect_left(S2, l - 1)
    R = bisect.bisect_right(S2, r - 2)
    print(R - L)

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
#ACのCのところに1を立てる部分和列
X = [0] * N
for i in range(1,N):
  if S[i-1:i+1] == 'AC':
    X[i] = X[i-1] + 1
  else:
    X[i] = X[i-1]

# Query
for i in range(Q):
  l,r = map(int, input().split())
  l,r = l-1, r-1
  if l > 0 and X[l] != X[l-1]:
    print(max(X[r] - X[l-1] - 1,0))
  elif l > 0:
    print(X[r] - X[l-1])
  else:
    print(X[r])
    
#print(X)

Submission status: Accepted

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

Submission status: Accepted

Solution code:



def start_process(length, q_num, char):
    data = [0 for i in range(length + 1)]
    char = list(char)

    for i in range(1, length):
        if char[i] == 'C' and char[i - 1] == 'A':
            data[i] = data[i - 1] + 1
        else:
            data[i] = data[i - 1]

    for i in range(q_num):
        start_ind, fin_ind = map(int, input().split())
        print(data[fin_ind - 1] - data[start_ind - 1])


def main():
    length, q_num = map(int, input().split())
    char = input()
    start_process(length, q_num, char)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

n = len(s)
ary = [0 for i in range(len(s)+1)]

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

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

Submission status: Accepted

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

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

L = [0]*N

for i in range(len(S)-1):
  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 = tuple(map(int,input().split()))
  print(L[r-1]-L[l-1])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,Q=map(int,input().split())
S=str(input())
p=[0]
count=0
for j in range(1,N):
    if S[j-1:j+1]=='AC':
        count += 1
    p.append(count)        


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

Submission status: Accepted

Solution code:
n,q=(int(i) for i in input().split())
s=input()
l=[0 for i in range(q)]
r=[0 for i in range(q)]
for i in range(q):
  l[i],r[i]= input().split()
#print(n,q,s,l,r)
t=[0 for i in s]
#print(t)
for i in range(len(t)-1):
  if s[i]=='A' and s[i+1]=='C':
    t[i+1]=t[i]+1
  else:
    t[i+1]=t[i]
#print(t)
for i in range(q):
  print(t[int(r[i])-1]-t[int(l[i])-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 = 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 = list(input())
L = [0, 0]
l = 0
for i in range(1, n):
    a = S[i-1]
    c = S[i]
    if a == "A" and c == "C":
        l += 1
    L.append(l)

for _ in range(q):
    l, r = map(int, input().split())
    re = 0
    if L[l] != L[l-1]:
        re = 1
    print(L[r] - L[l-1] - re)

Submission status: Accepted

Solution code:
N,Q = (int(i) for i in input().split())
S = input()
acl = [0 for i in range(N+1)]

for j in range(N):
  acl[j+1] = acl[j] +(1 if S[j:j+2] == 'AC' else 0)
      
for k in range(Q):
  x,y = (int(i)-1 for i in input().split())
  print(acl[y]-acl[x])

Submission status: Accepted

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

# In[4]:


N,Q=map(int,input().split())
S=input()
lr=[list(map(int,input().split())) for l in range(Q)]
ct=0
l=[0]*(N+1)
for i in range(0,len(S)-1):
    if S[i]=='A' and S[i+1]=='C':
        l[i+1]=1+l[i]
    else:
        l[i+1]=l[i]
for i in range(Q):
    print(l[lr[i][1]-1]-l[lr[i][0]-1])


# In[ ]:

Submission status: Accepted

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

cnt_list = [0]*n
judge = 0

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

for i in range(q):
    print(cnt_list[quiz[i][1] - 1] - cnt_list[quiz[i][0]-1])

Submission status: Accepted

Solution code:
from itertools import accumulate
n, q = map(int, input().split())
s = input()
L = [0]*n
for i in range(1, n):
    if(s[i-1:i+1] == 'AC'):
        L[i] = 1
c = list(accumulate(L))
for i in range(q):
    l, r = map(int, input().split())
    print(c[r-1]-c[l-1])

Submission status: Accepted

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

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


for j in range(Q):
    l,r = lr[j][0]-1, lr[j][1]-1
    ans = bisect.bisect_right(clst,r) - bisect.bisect_left(alst,l)
    print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N, Q = list(map(int, input().split()))
S = input()
t = [0] * N
for j in range(1, N):
  t[j] = t[j-1] + 1 if S[j-1:j+1] == "AC" else t[j-1]
for i in range(Q):
  l, r = list(map(int, input().split()))
  print(t[r-1]-t[l-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())
# template


N, Q = mi()
# print(N, Q)
S = li()
Alist = []
for i in range(N - 1):
    if S[i] == 'A' and S[i + 1] == 'C':
        Alist.append(i)
Aset = set(Alist)
AClist = [0]*(N+1)
for i in range(N-1):
    if i in Aset:
        AClist[i + 2] = AClist[i + 1] + 1
    else:
        AClist[i+2] = AClist[i+1]
        # print(AClist)

for i in range(Q):
    l, r = mi()
    if AClist[l - 1] != AClist[l]:
        print(AClist[r] - AClist[l])
    else:
        print(AClist[r] - AClist[l-1])

Submission status: Accepted

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

L=[]

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,Q=map(int,input().split())
s=input()
#a=[]

l=[0]*(N)
for i in range(N-1):
    if s[i:i+2]=='AC':
        l[i+1]=l[i]+1
    else:
        l[i+1]=l[i]
#print(l)
ans=[]
for i in range(Q):
    first,last=map(int,input().split())
    ans.append(l[last-1]-l[first-1])

for i in ans:
    print(i)

Submission status: Accepted

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

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

Submission status: Accepted

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

# S         * A C * A C C
# sumlist   0 0 1 1 1 2 2
sumlist = [0]
for i in range(1, len(S)):
	sumlist.append(sumlist[-1])
	if S[i - 1] == "A" and S[i] == "C":
		sumlist[-1] += 1

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

for q in query:
	print(sumlist[q["r"]] - sumlist[q["l"]])

Submission status: Accepted

Solution code:
N,Q = map(int,input().split())
S = input()
cnt = [0] * (N + 1)
lrs = []
for i in range(Q):
        lrs.append(list(map(int,input().split())))

for i in range(N):
        cnt[i+1] = cnt[i] + (1 if S[i:i+2] == "AC" else 0)
for lr in lrs:
        print(cnt[lr[1] - 1] - cnt[lr[0] - 1])

Submission status: Accepted

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


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

Submission status: Accepted

Solution code:
n,q=map(int,input().split())
s=input()
Sum=[0]
sum=0
stack="Q"
for i in range(n):
  if stack=="A" and s[i]=="C":
    sum+=1
  stack=s[i]
  Sum.append(sum)
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()
questions = [tuple(map(int,input().split())) for _ in range(q)]

part = [0]*n
al = [0]*n
for i in range(len(s)):
	if i == 0:
		continue
	if s[i-1:i+1]=='AC':
		al[i] = al[i-1]+1
		if i>1:
			part[i-1] = part[i-2] + 1
		else:
			part[i-1] = 1
	else:
		al[i] = al[i-1]
		if i>1:
			part[i-1] = part[i-2]
		else:
			part[i-1] = 0

for question in questions:
	l, r = question
	l -= 1
	r -= 1
	print(al[r]-part[l-1])

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**9)
from itertools import accumulate
n,q=map(int,input().split())
S=input()
A=[0]*(n-1)
for i in range(len(S)-1):
    if S[i]=='A' and S[i+1]=='C':
        A[i]=1
B=[0]+list(accumulate(A))
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 = list(input())
LR = [tuple(int(x) for x in input().split()) for _ in range(q)]

dp = [0] * (n + 1)

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

for L, R in LR:
    print(dp[R] - dp[L])

Submission status: Accepted

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

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

for l, r in questions:
    print(ac_sum[r-1] - ac_sum[l-1])

Submission status: Accepted

Solution code:
from itertools import*
a,b=map(int,input().split())
s=input()
table=[0]*(a-1)
for i in range(a-1):
    if s[i:i+2]=="AC":
        table[i]=1
x=[0]+list(accumulate(table))
for i in range(b):
    c,d=map(int,input().split())
    print(x[d-1]-x[c-1])

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N,Q = map(int,input().split())
S = input()
lr = [[int(i) for i in input().split()] for i in range(Q)]
C = [0]*(N+1)
terget = "AC"
index = -1
for i in range(N):
    C[i+1] = C[i] + (S[i:i+2] == "AC")
for l,r in lr:
    print(C[r-1] - C[l-1])

Submission status: Accepted

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

ac = [0]
for c, d in zip(s[0:], s[1:]):
    if c == 'A' and d == 'C':
        ac.append(ac[-1] + 1)
    else:
        ac.append(ac[-1])

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

Submission status: Accepted

Solution code:
n, q = [int(i) for i in input().split()]
S = input()
counter = [0] * (len(S) + 1)
for i in range(len(counter)):
    if S[i-2:i] == 'AC':
        counter[i] = counter[i-1] + 1
    else:
        counter[i] = counter[i-1]

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

Submission status: Accepted

Solution code:
from itertools import accumulate
import sys
N, Q = map(int, input().split())
S = input()
rs = [0] * N

for i in range(N - 1):
    if S[i] + S[i + 1] == "AC":
        rs[i + 1] = 1

rm = [0] + list(accumulate(rs))

for i in range(Q):
    ans = 0
    l, r = map(int, sys.stdin.readline().split())
    if S[l] == "C":
        ans -= 1
    ans = rm[r] - rm[l]
    print(ans)

Submission status: Accepted

Solution code:
n,q = map(int,raw_input().split())
s = raw_input()
dp = [0]*n
for i in xrange(1,n):
  dp[i] = dp[i-1]
  if s[i] == 'C' and s[i-1] == 'A':
    dp[i] += 1
for i in xrange(q):
  l,r = map(int,raw_input().split())
  print dp[r-1] - dp[l-1]

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = list(input())
t = [0] * (N+1)
cnt = 0
for i in range(1,N):
    if S[i-1] == 'A' and S[i] == 'C':
        cnt += 1
    t[i] = cnt
t[-1] = t[-2]
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()
d=[0]*n
#l=[int(i) for i in input().split()]
for i in range(1,n):
    if s[i-1]=="A" and s[i]=="C":
        d[i]+=d[i-1]+1
    else:
        d[i]=d[i-1]
#print(*d)
for i in range(q):
    l,r=map(int,input().split())
#    print(l,r,s[l-1:r],d[l-1],d[r-1])
    print(d[r-1]-d[l-1])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline()[:-1]


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

hit = [0] * N

for i in range(0, N - 1):
    hit[i + 1] = hit[i]

    if S[i:i+2] == 'AC':
        hit[i + 1] += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

hit = [0] * N

for i in range(0, N - 1):
    hit[i + 1] = hit[i]

    if S[i:i+2] == 'AC':
        hit[i + 1] += 1

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def compute():
    from sys import stdin
    N, Q = map(int, stdin.readline().split())
    S = stdin.readline().strip()

    cnt = [0]
    prev = S[0]
    cur = 0
    for s in S[1:]:
      if prev=='A' and s=='C':
        cur += 1
      cnt.append(cur)
      prev = s
    
    for _ in range(Q):
      l, r = map(int, stdin.readline().split())
      print(cnt[r-1] - cnt[l-1])


if __name__ == "__main__":
    compute()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
s = input()
t = [0 for _ in range(N + 1)]
for i in range(N):
    t[i + 1] = t[i] + (1 if s[i:i + 2] == 'AC' else 0)

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

Submission status: Accepted

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

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

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

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

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

Submission status: Accepted

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

import sys
from itertools import accumulate


def main():
    input = sys.stdin.readline
    n, q = [int(i) for i in input().split()]
    s = input().strip()
    qs = [[int(i) for i in input().split()] for _ in range(q)]
    hoges = [0] + [1 if s[i : i + 2] == "AC" else 0 for i in range(n - 1)]
    hoges = list(accumulate(hoges))
    for (l, r) in qs:
        print(hoges[r - 1] - hoges[l - 1])


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate


def main():
    input = sys.stdin.readline
    n, q = [int(i) for i in input().split()]
    s = input().strip()
    qs = [[int(i) for i in input().split()] for _ in range(q)]
    hoges = [0] + [1 if s[i : i + 2] == "AC" else 0 for i in range(n - 1)]
    hoges = list(accumulate(hoges))
    for (l, r) in qs:
        print(hoges[r - 1] - hoges[l - 1])


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N,Q=map(int,input().split())
S=input()
t=[0]*N
for i in range(N-1):
    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 sys

N, Q = map(int, input().split())
S = sys.stdin.readline()
P = [list(map(int, sys.stdin.readline().split())) for _ in range(Q)]

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

for q in P:
    print(a[q[1] - 1] - a[q[0] - 1])