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

def nothing():
    noooooo = 000000

for times in range(Q):
    l, r = input().split()
    l = int(l)
    r = int(r)
    T = ""
    ACCount = 0
    #部分文字列を作りたい
    T = S[l-1:r]
    #ACの判定をしたい
    for a in range(len(T)):
        if a+1 >= len(T):
            break
        if T[a:a+2] == "AC":
            ACCount = ACCount + 1
    print(ACCount)

Submission status: Time Limit Exceeded

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

for _ in range(q):
    start, end = [int(x) for x in input().split()]
    subst = s[:end][start - 1:]

    print(subst.count("AC"))

Submission status: Time Limit Exceeded

Solution code:
h,n = list(map(int, input().split()))
st = str(input())
a = [list(map(int, input().split())) for _ in range(n)]

for a1,b1 in a:
    print(st[a1-1:b1].count("AC"))

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()
arr = []
for i in range(N-1):
  if S[i:i+2] == "AC":
    arr.append(i)
for i in range(Q):
  l, r = map(int, input().split())
  count = 0
  for j in arr:
    if j >= l-1 and j < r-1:
      count += 1
  print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect
n,q = map(int,input().split())
s = input()
li=[]
for i in range(n-1):
  if s[i] == "A" and s[i+1] == "C":
    li.append(i)
for i in range(q):
  l,r = map(int,input().split())
  l = l-1
  r = r-1
  li_2 = li[bisect.bisect_left(li,l):bisect.bisect_right(li,r-1)]
  print(len(li_2))

Submission status: Time Limit Exceeded

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

for i in lr:
    ans = 0
    ch = "B"
    for j in range(i[0]-1, i[1]):
        if s[j] == "A":
            ch = "A"
        elif ch == "A" and s[j] == "C":
            ans += 1
            ch = "B"

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,Q=map(int,input().split())
s = input()
#S=[c for c in s]
ACs=[0]*N
for i in range(1,N):
    if s[i-1] =="A" and s[i]=="C":
        ACs[i-1]=1
for quiz in range(Q):
    l,r= 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()
#S=[c for c in s]
ACs=[]
for i in range(1,N):
    if s[i-1] =="A" and s[i]=="C":
        ACs.append(i)
for quiz in range(Q):
    l,r= map(int,input().split())
    ans=0
    for ac in ACs:
        ans += 1 if (ac>=l and (ac+1)<=r) else 0
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, Q = MAP()
S = input()
L = [LIST() for _ in range(Q)]

for i in range(Q):
	a = S[L[i][0] - 1:L[i][1]]
	a_s = a.replace("AC", "#")
	print(len(a) - len(a_s))

Submission status: Time Limit Exceeded

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

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())
    s_i = s[l-1:r]
    print(s_i.count("AC"))

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())
    s_i = s[l-1:r]
    ans_i = "".join(s_i.split("AC"))
    print(
        (len(s_i) - len(ans_i)) // 2
    )

Submission status: Time Limit Exceeded

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

for i in range(Q):
    l, r = map(int, input().split())
    Left.append(l)
    Right.append(r)

def search_AC(left, right):
    String = S[left - 1 :right]
    count = 0
    
    for i in range(len(String)-1):
        if (String[i] == 'A' and String[i + 1] == 'C'):
            count += 1
        
    return (count)

for i in range(Q):
    answer = search_AC(Left[i], Right[i])
    print(answer)

Submission status: Time Limit Exceeded

Solution code:
import sys

INPUT = lambda: sys.stdin.readline().rstrip()
MAP = lambda: map(int, INPUT().split())

sys.setrecursionlimit(10 ** 9)


def main():
    N, Q = MAP()
    S = INPUT()
    L = [1 if S[i:i+2] == "AC" else 0 for i in range(N-1)]

    ans = []
    for i in range(Q):
        l, r = MAP()
        ans.append(sum(L[l-1:r-1]))

    print(*ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

INPUT = lambda: sys.stdin.readline().rstrip()
MAP = lambda: map(int, INPUT().split())

sys.setrecursionlimit(10 ** 9)


def main():
    N, Q = MAP()
    S = INPUT()
    ans = []

    for i in range(Q):
        l, r = MAP()

        cnt = 0
        for j in range(l, r):
            if S[j-1:j+1] == "AC": cnt += 1

        ans.append(cnt)

    print(*ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
f = lambda : map(int,input().split())
n, q = f()
s = input()
for _ in range(q):
  l, r = f()
  ans = 0
  for i in range(l-1,r-1):
    if s[i:i+2] == 'AC':
      ans += 1
  print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
f = lambda : map(int,input().split())
n, q = f()
s = input()
for _ in range(q):
  l, r = f()
  print(s[l-1:r].count('AC'))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import re

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

pattern = "AC"

for i in li:
  s = S[i[0]-1:i[1]]
  count = 0
  
  for index, k in enumerate(s):
    if index == len(s) - 1:
      break

    if k == 'A' and s[index+1] == 'C':
      count += 1

  print(count)

Submission status: Time Limit Exceeded

Solution code:
import re

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

pattern = "AC"

for i in li:
  s = S[i[0]-1:i[1]]
  result = re.findall(pattern, s, re.S)
  print(len(result))

Submission status: Time Limit Exceeded

Solution code:
N,Q=map(int,input().split())
S=input()
for _ in range(Q):
    l,r=map(int,input().split())
    lr_S=S[l-1:r]
    n=r-l
    ans=0
    for i in range(n):
        if lr_S[i]=='A':
            if lr_S[i+1]=='C':
                ans+=1
    print(ans)

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    l,r=map(int,input().split())
    l-=1
    r-=1
    ans=0
    for ac in AC:
        if l<=ac<r:
            ans+=1
        if ac>=r:
            break
    print(ans)

Submission status: Time Limit Exceeded

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

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()
l, r = [0] * Q, [0] * Q
for i in range(Q):
    l[i], r[i] = map(int, input().split())
for i in range(Q):
    print(S[l[i] - 1:r[i]].count("AC"))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
S=list(input())
list=[0]*N
for i in range(N-1):
    if S[i] == "A" and S[i+1] == "C":
        list[i]+=1
count=0
for j in range(Q):
    l, r = map(int, input().split())
    print(sum(list[l-1:r-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())
	x = s[l-1:r]
	ans = x.count("AC")
	print (ans)

Submission status: Time Limit Exceeded

Solution code:
import re

N, Q = [int(i) for i in input().split()]
S = input()
ans = []
for i in range(Q):
    l, r = [int(i) for i in input().split()]
    l -= 1
    s = S[l:r]
    ret = re.findall(r"AC", s)
    ans.append(ret)
for a in ans:
    print(len(a))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc122/tasks/abc122_c
import numpy as np

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

locations = []
for i in range(len(S) - 1):
    if S[i] == 'A' and S[i + 1] == 'C':
        locations.append(i)
locations = np.array(locations)
# print('locations', locations)

t = []
for i in range(len(S) + 1):
    t.append(np.count_nonzero(locations < i))
# print(t)
for l, r in lr:
    print(t[r] - t[l])

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc122/tasks/abc122_c
import numpy as np

N, Q = map(int, input().split())
N = int(N)
S = input()
lr = []
for _ in range(Q):
    l, r = map(int, input().split())
    lr.append((l - 1, r - 1))

t = []
for i in range(1, len(S) + 1):
    subset = S[:i]
    t.append(subset.count('AC'))
for l, r in lr:
    print(t[r] - t[l])

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc122/tasks/abc122_c
import numpy as np

N, Q = map(int, input().split())
N = int(N)
S = input()
lr = []
for _ in range(Q):
    l, r = map(int, input().split())
    lr.append((l - 1, r - 1))
locations = []
for i in range(N - 1):
    if S[i] == 'A' and S[i + 1] == 'C':
        locations.append(i)
locations = np.array(locations)
# print(locations)

for l, r in lr:
    subset = locations[(l <= locations) & (locations < r)]
    # print(l, r, subset)
    print(len(subset))

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc122/tasks/abc122_c
import numpy as np

N, Q = map(int, input().split())
N = int(N)
S = input()
lr = []
for _ in range(Q):
    l, r = map(int, input().split())
    lr.append((l - 1, r - 1))
locations = []
for i in range(N - 1):
    if S[i] == 'A' and S[i + 1] == 'C':
        locations.append(i)
locations = np.array(locations)
# print(locations)

for l, r in lr:
    subset = locations[l <= locations]
    subset = subset[subset < r]
    # print(l, r, subset)
    print(len(subset))

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc122/tasks/abc122_c
import itertools

N, Q = map(int, input().split())
N = int(N)
S = input()
lr = []
for _ in range(Q):
    l, r = map(int, input().split())
    lr.append((l - 1, r - 1))
locations = []
for i in range(N - 1):
    if S[i] == 'A' and S[i + 1] == 'C':
        locations.append(i)
# print(locations)

for l, r in lr:
    subset = list(itertools.filterfalse(lambda x: x < l, locations))
    subset = list(itertools.filterfalse(lambda x: x > r, subset))
    # print(l, r, subset)
    num_substring = len(subset)
    # print('num_substring', num_substring)
    if num_substring == 0:
        print(0)
        continue
    if subset[-1] == r:
        print(num_substring - 1)
    else:
        print(num_substring)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import re

ENKINUM, N = map(int, input().split())
ENKI = input()
P = [input().split() for i in range(N)]
for hoge in P:
  context = ENKI[int(hoge[0])-1:int(hoge[1])]
  result = re.findall('AC', context)
  print(len(result))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

index = [0] *(N+1)

for i in range(N-2):
  if S[i:i+2] == 'AC':
    index[i] = 1

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N,Q = map(int,input().split())
S = list(input())
ls = [list(map(int,input().split())) for i in range(Q)]
ac = [0]*N
for i in range(1,N):
    if S[i-1] == "A":
        if S[i] == "C":
            ac[i] += 1
ans = 0
for i in range(Q):
    print(sum(ac[ls[i][0]:ls[i][1]]))

Submission status: Time Limit Exceeded

Solution code:
n,q =map(int,input().split())
s=input()
p=[[int(j) for j in input().split()] for _ in range(q)]
for _ in range(q):
    print(s[p[_][0]-1:p[_][1]].count("AC"))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

for l, r in l_r_list:
    ans = S[l-1:r].count('AC')
    print(ans)

Submission status: Time Limit Exceeded

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

ls=[0]

Spre=S[0]
for i in range(1,N):
    
    Si=S[i]
    if Spre=='A' and Si=='C':
        ls.append(1)
    else:
        ls.append(0)
    Spre=Si

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


if __name__ == '__main__':
    main()

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

import re

for _ in range(Q):
  l, r = map(int, input().split())
  r = re.findall('AC', S[l-1:r])
  print(len(r))

Submission status: Time Limit Exceeded

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

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

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

flag = [0] * (n - 1)

for i in range(n - 1):
	if s[i:i+2] == 'AC':
		flag[i] = 1
	
for j in range(q):
	flgp = flag[ls[j][0] - 1:ls[j][1] - 1]
	print(sum(flgp))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

    dec_a = [0] * (N)
    for x in range(1, N):
        if S[x] == 'C' and S[x-1] == 'A':
            S[x-1] = 'a'
        dec_a[x] = S[:x+1].count('a')


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

main()

Submission status: Time Limit Exceeded

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

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

    dec_a = [0] * (N)
    for x in range(1, N):
        if S[x] == 'C' and S[x-1] == 'A':
            S[x-1] = 'a'
        dec_a[x] = S[:x+1].count('a')


    L = [0] * Q
    R = [0] * Q
    for i in range(Q):
        L[i], R[i] = map(int, input().split())

    for j in range(Q):
        print(dec_a[R[j]-1] - dec_a[L[j]-1])

main()

Submission status: Time Limit Exceeded

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

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

    for x in range(len(S) - 1):
        if S[x] == 'A' and S[x+1] == 'C':
            S[x] = 'a'
    dec_a = S.count('a')


    L = [0] * Q
    R = [0] * Q
    for i in range(Q):
        L[i], R[i] = map(int, input().split())

    for j in range(Q):
        print(S[L[j]-1:R[j]-1].count('a'))

main()

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)]
for i in range(q):
    print(s[lr[i][0]-1:lr[i][1]].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)]

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

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

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

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

for l, r in lr:
    print(sum(f[l-1:r-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())
    print(S[l-1: r].count("AC"))

Submission status: Time Limit Exceeded

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

c=[0]
for i in range(n-1):
  if s[i]+s[i+1]=='AC':
    c.append(1)
  else:
    c.append(0)
  
cs=[sum(c[:i+1]) for i in range(n)]
#print(cs)
for _ in range(q):
  l,r=map(int,input().split())
  print(cs[r-1]-cs[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())
    t=s[l-1:r]
    print(t.count('AC'))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, q = map(int, input().split())
s = input()
len_s = len(s)
ans = []
for i in range(q):
    l, r = map(int, input().split())
    tmp_len = len(s[l-1:r].replace("AC", ""))
    res = ((r-l+1) - tmp_len) / 2
    ans.append(int(res))
for num in ans:
    print(num)

Submission status: Time Limit Exceeded

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

before = ''
i = 0
num = 0
data_list = {}
for s in S:
    if before == 'A' and s == 'C':
        num += 1
    before = s
    data_list[i] = num
    i += 1

ans = ''
for q in range(Q):
    r,l = map(int, input().split())
    ans += str(data_list[l-1]-data_list[r-1])+'\n'

print(ans.rstrip())

Submission status: Time Limit Exceeded

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

before = ''
i = 0
num = 0
data_list = [0]*N
for s in S:
    if before == 'A' and s == 'C':
        num += 1
    before = s
    data_list[i] = num
    i += 1

ans = ''
for q in range(Q):
    r,l = map(int, input().split())
    ans += str(data_list[l-1]-data_list[r-1])+'\n'

print(ans.rstrip())

Submission status: Time Limit Exceeded

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

ans = ''
for q in range(Q):
    l,r = map(int, input().split())
    s = S[l-1:r]
    ans += str(len(s.split('AC'))-1)+'\n'

print(ans.rstrip())

Submission status: Time Limit Exceeded

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

for i in range(Q):
    print(S[l[i]-1:r[i]].count('AC'))

Submission status: Time Limit Exceeded

Solution code:
N,Q=map(int,input().split())
S=input()
A=[list(map(int,input().split())) for i in range(Q)]
X=[0]*(N+1)
for i in range(N-1):
  if S[i]=='A':
    if S[i+1]=='C':
      X[i+1]=1
for i in range(Q):
  a=A[i][0]
  b=A[i][1]
  print(sum(X[a:b]))

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=str(input())
l=[]
r=[]
for i in range(q):
    a,b=map(int,input().split())
    l.append(a)#文字列の最初
    r.append(b)#文字列の最後

ans=[]

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

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


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

Submission status: Time Limit Exceeded

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

for i in range(q):
    d=s[l[i]-1:r[i]]
    print(d.count("AC"))

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

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

Submission status: Time Limit Exceeded

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

n,q=map(int,readline().split())
s=readline().decode('utf-8')
lst=map(int,read().split())
lr=list(zip(lst,lst))
for l,r in lr:
  print((r-l+1-len(s[l-1:r].replace('AC','')))//2)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque

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

    _S=deque(S)
    T=""
    for i in range(N):
        #T=S[0:i+1]#i=0で一字含む, 
        T+=_S.popleft()
        cumsum[i]=T.count("AC")#cumsum[i]:i+1文字以下でACを含むか
    
    cumsum=[0]+cumsum#cumsum[i]:i文字以下でACを含むか
    for j in range(Q):
        print(cumsum[R[j][1]]-cumsum[R[j][0]])

from io import StringIO
import unittest


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

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

Submission status: Time Limit Exceeded

Solution code:
import sys

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

    cumsum=[0 for i in range(N)]
    for i in range(N):
        T=S[0:i+1]#i=0で一字含む, 
        cumsum[i]=T.count("AC")#cumsum[i]:i+1文字以下でACを含むか
    
    cumsum=[0]+cumsum#cumsum[i]:i文字以下でACを含むか
    for j in range(Q):
        print(cumsum[R[j][1]]-cumsum[R[j][0]])

from io import StringIO
import unittest


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

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

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
n,q=map(int,input().split())
s=input()
a=len(s)
l=[['O',0]]*a
z=0
i=0
while i<a-1:
    if s[i]=='A' and s[i+1]=='C':
        z+=1
        l[i],l[i+1]=['A',z],['C',z]
        i+=2
    else:
        i+=1
for _ in range(q):
    a,b=0,0
    x,y=map(int,input().split())
    for i in range(x-1,y):
        if l[i][0]=='A':
            a=l[i][1]
            break
    for i in range(y-1,x-2,-1):
        if l[i][0]=='C':
            b=l[i][1]
            break
    if a>0 and b>0:
        print(b-a+1)
    else:
        print(0)

Submission status: Time Limit Exceeded

Solution code:
n,q=map(int,input().split())
s=input()
a=len(s)
l=[0]*a
for i in range(a-2):
    if s[i]=='A' and s[i+1]=='C':
        l[i],l[i+1]=1,2
for _ in range(q):
    x,y=map(int,input().split())
    z=0
    if l[x-1]==1 and l[x]==2:
        z+=1
    if l[y-2]==1 and l[y-1]==2:
        z+=1
    z+=sum(l[x+1:y-2])//2
    print(z)

Submission status: Time Limit Exceeded

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

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()
for i in range(q):
    l, r = map(int, input().split())
    S_ = S[l-1:r]
    print(S_.count("AC"))