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: Time Limit Exceeded

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

a=[0]*(n+1)
for i in range(0,n+1):
    # print(s[0:i])
    a[i]+=len(re.findall("AC",s[0:i]))

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

Submission status: Time Limit Exceeded

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

for i in range(q):
    l,r=[int(i)-1 for i in input().split()]
    string=s[l:r+1]
    ans=re.findall("AC",string)
    print(len(ans))

Submission status: Time Limit Exceeded

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

for i in range(q):
    l,r=[int(i)-1 for i in input().split()]
    string="".join(s[l:r+1])
    ans=re.findall("AC",string)
    print(len(ans))

Submission status: Time Limit Exceeded

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

count=0
for i in range(q):
    l,r=[int(i)-1 for i in input().split()]
    for j in range(l,r):
        if s[j]=="A":
            if s[j+1]=="C":
                count+=1
    print(count)
    count=0

Submission status: Time Limit Exceeded

Solution code:


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

for q in range(Q):
  s, t = map(int, input().split())
  print(S[s-1:t:1].count("AC"))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N, Q = map(int,input().split())
S = input()[:-1]
for i in range(Q):
    l, r = map(int, input().split())
    tmp = S[l-1:r]
    print(len(tmp.split("AC"))-1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,q = map(int,input().split())
s = input()
for i in range(q):
    l,r = map(int,input().split())
    print(s[l-1:r].count("AC"))

Submission status: Time Limit Exceeded

Solution code:
N,Q = map(int,input().split())
S = input()
questions = [[i for i in map(int,input().split())] for j in range(Q)]
import re
for question in questions:
  Sp = S[question[0]-1:question[1]]
  print(len(re.findall('AC', Sp)))

Submission status: Time Limit Exceeded

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

for c in range(Q):
  con = 0
  l,r = map(int,input().split())
  l = l - 1
  part = S[l:r]
  for i in range(len(part) - 1):
    if part[i] + part[i + 1] == "AC":
      con += 1
  print(con)

Submission status: Time Limit Exceeded

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

for c in range(Q):
  l,r = map(int,input().split())
  l = l - 1
  part = S[l:r]
  print(part.count("AC"))

Submission status: Time Limit Exceeded

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

word = input()

for _ in range(q):
    start, end = map(int, input().split())
    cnt = word[start-1:end].count('AC')
    print(cnt)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import os, sys, re, math

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

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

for i in range(Q):
    (l, r) = [int(n) for n in input().split()]
    print(len(list(filter(lambda x:x >= l - 1 and x < r - 1, oc))))

Submission status: Time Limit Exceeded

Solution code:
import os, sys, re, math

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

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

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

for i in range(Q):
    print(len(list(filter(lambda x:x >= L[i] and x < R[i], oc))))

Submission status: Time Limit Exceeded

Solution code:
import os, sys, re, math

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

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

for i in range(Q):
    count = 0
    for j in range(L[i], R[i]):
        if S[j] == 'A' and S[j + 1] == 'C':
            count += 1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
import os, sys, re, math

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

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

memo = {}
for i in range(Q):
    if (L[i], R[i]) in memo:
        print(memo[(L[i], R[i])])
        continue
    substr = S[L[i]:R[i]]
    replaced = substr.replace('AC', '')
    oc = (len(substr) - len(replaced)) >> 1
    print(oc)
    memo[(L[i], R[i])] = oc

Submission status: Time Limit Exceeded

Solution code:
import os, sys, re, math

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

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

for i in range(Q):
    substr = S[L[i]:R[i]]
    replaced = re.sub('AC', '', substr)
    print((len(substr) - len(replaced)) >> 1)

Submission status: Time Limit Exceeded

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

S = input()
d = [0] * (len(S) + 1)
for i in range(1, len(S) + 1):
    l, r = 1, i
    s = S[l - 1:r]
    d[r] = len(('x' + s + 'x').split('AC')) - 1

for q in range(Q):
    l, r = [int(s) for s in input().split(' ')]
    dl = d[l]
    dr = d[r]
    print(dr - dl)

Submission status: Time Limit Exceeded

Solution code:
import os, sys, re, math

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

for i in range(Q):
    (l, r) = [int(n) for n in input().split()]
    substr = S[l - 1:r]
    replaced = re.sub('AC', '', substr)
    print((len(substr) - len(replaced)) // 2)

Submission status: Time Limit Exceeded

Solution code:
n, k = list(map(int, input().split(' ')))
orig_a = input()
for _ in range(k):
  s, e = list(map(int, input().split(' '))) # TLEかも
  a = orig_a[s-1:e]
  print(a.count('AC'))

Submission status: Time Limit Exceeded

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

#acの場所把握
ac = []
for i in range(n - 1):
    if s[i] == "A" and s[i + 1] == "C":
        ac.append(i + 1)

for i in range(q):
    cnt = 0
    for j in range(len(ac)):
        if ac[j] >= l[i] and ac[j] + 1 <= r[i]:
            cnt += 1
    print(cnt)

Submission status: Time Limit Exceeded

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

for i in range(q):
    cnt = 0
    for j in range(l[i] - 1, r[i]):
        if j == r[i] - 1:
            break
        if s[j] == "A" and s[j + 1] == "C":
            cnt += 1
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import re

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

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

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

my_solve1()

Submission status: Time Limit Exceeded

Solution code:
import re
import sys


input = sys.stdin.readline

n, q = [int(i) for i in input().strip().split()]
s = input().strip()
for i in range(q):
    l, r = [int(i) for i in input().strip().split()]
    l -= 1
    substr = s[l:r]
    print(len(re.findall(r"AC", substr)))

Submission status: Time Limit Exceeded

Solution code:
import re


n, q = [int(i) for i in input().split()]
s = input()
for i in range(q):
    l, r = [int(i) for i in input().split()]
    l -= 1
    substr = s[l:r]
    print(len(re.findall(r"AC", substr)))

Submission status: Time Limit Exceeded

Solution code:
def solve():
    N, Q = map(int, input().split())
    S = input()
    l = [0 for _ in range(Q)]
    r = l[:]
    for i in range(Q):
        l[i], r[i] = map(int, input().split())
    
    ac_head = []
    for i, char in enumerate(S[:-1]):
        if char == 'A' and S[i+1] == 'C':
            ac_head.append(i)
    
    for i in range(Q):
        cnt = 0
        for index in ac_head:
            if l[i] - 1 <= index <= r[i] - 2:
                cnt += 1
            elif r[i] - 2 < index:
                break
        print(cnt)


if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

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


def solve(N: int, Q: int, S: str, l: "List[int]", r: "List[int]"):
    for (li, ri) in zip(l, r):
        print(S[li - 1 : ri].count("AC"))


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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in lr:
    si = s[i[0]-1:i[1]]
    count = 0
    for j in range(len(si)-1):
        if si[j] == 'A' and si[j+1] == 'C':
            count += 1
    print(count)

Submission status: Time Limit Exceeded

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

for i in range(q):
    for j in range(lr[i][0],lr[i][1]):
        if s[j-1] == "A" and s[j] == "C":
            accnt += 1
    else:
        print(accnt)
        accnt = 0

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
    print(len(re.findall(r"AC", S[lr[i][0]-1:lr[i][1]])))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:

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

for i in range(Q):
  f, t = map(int, input().split())
  print(S[f-1:t].count("AC"))

Submission status: Time Limit Exceeded

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

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

for le, ri in lr:
    count = 0
    for a in index:
        if le-1 <= a and a + 1 <= ri - 1:
            count += 1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
# TLE(lr&ACsが重そう)
'''
ACが出現する場所を抑えておいて、
range内に何回出てくるか探索
'''
N, Q = list(map(int, input().split()))
S = list(str(input()))
questions = [tuple(map(int, input().split())) for _ in range(Q)]
ACs = [0] * N
for i in range(len(S) - 1):
    if S[i] == 'A' and S[i + 1] == 'C':
        ACs[i] = 1

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n,q=map(int,input().split())
s=input()
lis=[list(map(int,input().split())) for i in range(q)]
for i in range(q):
  print((len(s[lis[i][0]-1:lis[i][1]])-len(s[lis[i][0]-1:lis[i][1]].replace('AC','')))//2)

Submission status: Time Limit Exceeded

Solution code:
N,Q = list(map(int, input().split()))
S=list(str(input()))
ACs=[]
for i in range(len(S)-1):
    if S[i]=='A' and S[i+1]=='C':
        ACs.append(i)
ACs=set(ACs)
for q in range(Q):
    l,r = list(map(int, input().split()))
    lr=set(range(l-1,r-1))
    print(len(lr&ACs))

Submission status: Time Limit Exceeded

Solution code:
N,Q = list(map(int, input().split()))
S=list(str(input()))
ACs=[]
for i in range(len(S)-1):
    if S[i]=='A' and S[i+1]=='C':
        ACs.append(i)
ACs=set(ACs)
for q in range(Q):
    l,r = list(map(lambda x:int(x)-1, input().split()))
    lr=set(range(l,r))
    print(len(lr&ACs))

Submission status: Time Limit Exceeded

Solution code:
N,Q = list(map(int, input().split()))
S=list(str(input()))
ACs=[]
for i in range(len(S)-1):
    if S[i]=='A' and S[i+1]=='C':
        ACs.append(i)

for q in range(Q):
    l,r = list(map(lambda x:int(x)-1, input().split()))
    lr=list(range(l,r))
    print(len(set(lr)&set(ACs)))

Submission status: Time Limit Exceeded

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

lst = []

for _ in range(q):
  lst.append(list(map(int, input().split())))

for i in range(q):
  print(s[lst[i][0] - 1:lst[i][1]].count("AC"))

Submission status: Time Limit Exceeded

Solution code:
n,q = map(int,input().split())
s = input()
questions = [tuple(map(int,input().split())) for _ in range(q)]
for i in range(q):
  l,r = questions[i]
  print(s[l - 1:r].count("AC"))

Submission status: Time Limit Exceeded

Solution code:
n, q = map(int, input().split())
s = input()
lr = [map(int, input().split()) for _ in range(q)]
sub = [s[i:i + 2] == "AC" for i in range(len(s) - 1)]
acc = [sum(sub[:i]) for i in range(len(sub))] + [sum(sub)]
for l, r in lr:
  print(acc[r - 1] - acc[l - 1])

Submission status: Time Limit Exceeded

Solution code:
n,q=map(int, input().split())
s=input()
for i in range(q):
    l,r=map(int, input().split())
    w=s[l-1:r]
    print(w.count('AC'))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N,Q=map(int,input().split())
S=input()
LR=[list(map(int,input().split())) for i in range(Q)]
for i in range(Q):
  l=LR[i][0]-1
  r=LR[i][1]-1
  ans=0
  s=S[l:r+1]
  
  print(s.count("AC"))

Submission status: Time Limit Exceeded

Solution code:
N,Q=map(int,input().split())
S=input()
LR=[list(map(int,input().split())) for i in range(Q)]
I=[]
for i in range(N-1):
  if S[i:i+2]=="AC":
    I.append(i)
for i in range(Q):
  l=LR[i][0]-1
  r=LR[i][1]-1
  ans=0
  for j in range(len(I)):
    if l<=I[j] and I[j]+1<=r:
        ans+=1
    if r<I[j]:
      break
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
if sys.platform == 'ios':
	sys.stdin = open('input.txt')

input = sys.stdin.readline


n,q = map(int,input().split())
s = input()
l = [list(map(int,input().split())) for i in range(q)]

for i in range(q):
	print(s[l[i][0]-1:l[i][1]].count('AC'))

Submission status: Time Limit Exceeded

Solution code:
import sys
if sys.platform == 'ios':
	sys.stdin = open('input.txt')


n,q = map(int,input().split())
s = input()
l = [list(map(int,input().split())) for i in range(q)]

for i in range(q):
	print(s[l[i][0]-1:l[i][1]].count('AC'))

Submission status: Time Limit Exceeded

Solution code:
import re
import numpy as np
n,q=map(int,input().split())
s=input()
a=np.zeros(n+1,dtype=int)
for m in re.finditer("AC", s):
  a[m.start()+2:]+=1

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

Submission status: Time Limit Exceeded

Solution code:
import re
n,q=map(int,input().split())
s=input()
a=[m.span() for m in re.finditer("AC", s)]

for _ in range(q):
  l,r=map(int,input().split())
  ans=sum(1 for x in a if l-1<=x[0] and x[1]<=r)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,Q = map(int,input().split())
S = list(input())
num = list(range(N))
dic1 = dict(zip(num,S))
q = [list(map(lambda x: int(x)-1,input().split())) for _ in range(Q)]
dic2 = {}
for i in range(N-1):
    if dic1[i]+dic1[i+1] == "AC":
        dic2[i] = i
vl = list(dic2.values())
num = list(range(len(vl)))
dic3 = dict(zip(num,vl))
for i in range(Q):
    ans = 0
    for j in range(len(vl)):
        if q[i][0] <= dic3[j] < q[i][1]:
            ans += 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,Q = map(int,input().split())
S = list(input())
num = list(range(N))
dic1 = dict(zip(num,S))
q = [list(map(lambda x: int(x)-1,input().split())) for _ in range(Q)]
dic2 = {}
for i in range(N-1):
    if dic1[i]+dic1[i+1] == "AC":
        dic2[i] = i
vl = list(dic2.values())
for i in range(Q):
    ans = 0
    for j in range(len(dic2)):
        if q[i][0] <= vl[j] < q[i][1]:
            ans += 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,q=map(int,input().split())
s=input()
L=[]
for i in range(q):
    a,b=map(int,input().split())
    L.append(s[a-1:b])
for i in L:
      ans=0
      for j in range(len(i)-1):
            if "AC"==i[j:j+2]:
                  ans+=1
      print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,q=map(int,input().split())
s=input()
L=[]
for i in range(q):
    a,b=map(int,input().split())
    L.append(s[a-1:b])
for i in L:
      ans=0
      for j in range(len(i)-1):
            if "AC"==i[j:j+2]:
                  ans+=1
      print(ans)

Submission status: Time Limit Exceeded

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)
index = []
for i in range(n-1):
    if i != n-2 and s[i:i+2] == "AC":
        index.append(i)
    elif s[i:] == "AC":
        index.append(i)

for i in range(q):
    count = 0
    for j in range(len(index)):
        if l[i]-1 <= index[j] and index[j] + 1 <= r[i]-1:
            count += 1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
n,q = list(map(int,input().split()))
s = input()
d = [list(map(int,input().split())) for i in range(q)]

for i in d:
    print(s[i[0]-1:i[1]].count("AC"))

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
s = input()
list=[]
for i in range(Q):
    l, r = map(int, input().split())
    c=s.count('AC',l-1,r)
    list.append(c)
for ele in list:
    print(ele)

Submission status: Time Limit Exceeded

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

for i in range(n):
  a[i]=s[:i+1].count('AC')

for i in range(q):
  print(a[l[i][1]-1]-a[l[i][0]-1])

Submission status: Time Limit Exceeded

Solution code:
n,q = map(int,input().split())
s = input()
L = set()
for i in range(n-1):
    if s[i:i+2] == "AC":
        L.add(i+1)

for i in range(q):
    l,r = map(int,input().split())
    print(len([i for i in L if l <= i and i < r]))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    N, Q = map(int, input().split())
    S = input()
    ans = []
    for _ in range(Q):
        l, r = map(int, input().split())
        ans.append(S[l-1:r].count('AC'))

    for i in range(Q):
        print(ans[i])
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import time
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

N, Q = MAP()
S = input().rstrip()
LR = [MAP() for _ in range(Q)]

loc = [0]*N
#start = time.time()
for i in range(N-1):
	if S[i]+S[i+1] == 'AC': loc[i] += 1
#print(loc)
#elapsed_time = time.time() - start
#print("elapsed_time:{0}".format(elapsed_time) + "[sec]")

#start = time.time()
for l, r in LR:
	print(sum(loc[l-1:r-1]))
	#tmp = (loc[r-1]-loc[l-1])
#elapsed_time = time.time() - start
#print("elapsed_time:{0}".format(elapsed_time) + "[sec]")

Submission status: Time Limit Exceeded

Solution code:
import sys
import time
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

N, Q = MAP()
S = input().rstrip()
LR = [MAP() for _ in range(Q)]

loc = [0]*N
#start = time.time()
for i in range(N-1):
	if S[i]+S[i+1] == 'AC': loc[i] += 1
#print(loc)
#elapsed_time = time.time() - start
#print("elapsed_time:{0}".format(elapsed_time) + "[sec]")

start = time.time()
for l, r in LR:
	print(sum(loc[l-1:r-1]))
	#tmp = (loc[r-1]-loc[l-1])
elapsed_time = time.time() - start
#print("elapsed_time:{0}".format(elapsed_time) + "[sec]")

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

N, Q = MAP()
S = input().rstrip()
LR = [MAP() for _ in range(Q)]

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

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

Submission status: Time Limit Exceeded

Solution code:
n,q=map(int,input().split())
s=str(input())
r=[list(map(int,input().split())) for i in range(q)]
a=list(s)
cnt=0
for x in r:
  cnt=0
  for y in range(x[0]-1,x[1]-1):
    if((s[y]+s[y+1])=="AC"):
      cnt+=1
  print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
AC = [0 for _ in range(N)]
flag_A = False
for i, s in enumerate(input()):
    if flag_A:
        if s == 'C':
            AC[i-1] = 1
        flag_A = False

    elif s == 'A':
        flag_A = True

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

Submission status: Time Limit Exceeded

Solution code:
l=input().split()
n=int(l[0])
q=int(l[1])
s=input()
numli=[]
while q>0:
    l=input().split()
    low=int(l[0])-1
    high=int(l[1])
    temp=s[low:high]
    num=temp.count('AC')
    numli.append(num)
    q-=1
for i in numli:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
x, y = [int(x) for x in input().split()]
count=0

s=input()

for i in range(y):
    st, e = [int(x) for x in input().split()]
    b=s[st-1:e]
    
    print(b.count("AC"))
    count=0

Submission status: Time Limit Exceeded

Solution code:
x, y = map(int, input().split()) 
count=0

s=input()

for i in range(y):
    st, e = map(int, input().split())
    b=s[st-1:e]
    
    print(b.count("AC"))
    count=0

Submission status: Time Limit Exceeded

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

p=[s[i]+s[i+1]=='AC' for i in range(n-1)]

for i in range(q):
  cnt=sum(p[l[i][0]-1:l[i][1]-1])
  print(cnt)

Submission status: Time Limit Exceeded

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

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

for i in range(q):
  cnt=sum([l[i][0]<=x<l[i][1] for x in p])
  print(cnt)

Submission status: Time Limit Exceeded

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

p=[s[i]+s[i+1] for i in range(n-1)]

for i in range(q):
  print(sum([x=='AC' for x in p[l[i][0]-1:l[i][1]-1]]))

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
n,q=map(int,input().split())
s=input().replace("\n","")
for i in range(q):
    cnt=0
    l,r=map(int,input().split())
    for j in range(l,r):
        #print(s[j-1:j+1])
        if s[j-1:j+1]=="AC":
            cnt+=1
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N,Q = map(int,input().split())
S = input()
check = "AC"
for i in range(Q):
    l,r = map(int,input().split())
    cnt = S[l-1:r].count(check)
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N,Q = map(int,input().split())
S = input()
check = "AC"
for i in range(Q):
    l,r = map(int,input().split())
    T = S[l-1:r]
    cnt = T.count(check)
    print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

# 累積文字和の作成
cl = ['']
for char in S:
    cl.append(cl[-1] + char)

# クエリに対する出力
for i in range(Q):
    string = cl[ql[i][1]][ql[i][0]-1:]
    print(string.count('AC'))

Submission status: Time Limit Exceeded

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

for i in range(q):
  ans = 0
  sen = s[num[i][0]-1:num[i][1]]
  for j in range(num[i][1]-num[i][0]+1):
    if sen[j]=='A':
      if j+1<num[i][1]-num[i][0]+1:
        if sen[j+1]=='C':
          ans += 1
  print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,q=map(int,input().split())
s=input()
b=[]
x=0
for i in range(10**5):
    ind=s.find("AC",x)
    if ind==-1:
        break
    b.append(ind+1)
    x=ind+1
for i in range(q):
    cnt=0
    a=list(map(int,input().split()))
    for j in b:
        if a[0]<=j<a[1]:
            cnt+=1
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n,q=map(int,input().split())
s=input()
for i in range(q):
    a=list(map(int,input().split()))
    print(s.count('AC',a[0]-1,a[1]))

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

for i in range(q):
    print(s[lst[i][0] - 1:lst[i][1]].count('AC'))

Submission status: Time Limit Exceeded

Solution code:
import collections

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

new_S = S.replace("AC", "12")

count_one_list = [new_S[:i].count('1') for i in range(len(S))]

for i in range(Q):
    left, right = map(int, input().split())
    count_one = count_one_list[right-1] - count_one_list[left-1]
    print(count_one)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

new_S = S.replace("AC", "12")


count_one_list = [0] * len(S)
for i in range(len(S)):
    count_one_list[i] = new_S[:i].count('1')

for i in range(Q):
    left, right = map(int, input().split())
    count_one = count_one_list[right-1] - count_one_list[left-1]
    print(count_one)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

new_S = S.replace("AC", "12")
new_S_list = list(new_S)

for i in range(Q):
    left, right = map(int, input().split())
    count_one = collections.Counter(new_S_list[left-1:right-1])['1']
    print(count_one)