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

def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N,Q = LI()
str = S()
# l = [list(map(int,input().split())) for i in range(N)]

# 累積和
sum = list(range(N+1))
for i in range(N):
    if i+1<N and str[i] == 'A' and str[i+1] == 'C':
        sum[i+1] = sum[i] + 1
    else:
         sum[i+1] = sum[i]



for q in range(Q):
    l,r = LI()
    l -= 1
    r -= 1
    print(sum[r]-sum[l])

Submission status: Accepted

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

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

count=[0]*n

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

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

Submission status: Accepted

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

Submission status: Accepted

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

dp = [0]*n
for i in range(1,n):
    dp[i]+=dp[i-1]
    if s[i-1:i+1]=='AC':
        dp[i]+=1
for i in range(q):
    print(dp[R[i]-1]-dp[L[i]-1])

Submission status: Accepted

Solution code:
S=lambda:input()
I=lambda:int(input())
L=lambda:list(map(int,input().split()))
LS=lambda:list(map(str,input().split()))

def main():
    import sys
    input=sys.stdin.readline
    n,q=L()
    s=S().rstrip()
    
    a=[0]*n
    tmp=0
    for i in range(n-1):
        if s[i:i+2]=='AC':
            tmp+=1
        a[i+1]=tmp
    for i in range(q):
        li,ri=L()
        print(a[ri-1]-a[li-1])

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
n,q = map(int,input().split())
s = input()
data = []
for i in range(n-1):
    if s[i:i+2] == "AC":
        data.append(i)
for i in range(q):
    l,r = map(int,input().split())
    l -= 1
    r -= 1
    j = bisect.bisect_left(data,l)
    k = bisect.bisect_left(data,r)
    print(k-j)

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
string = input()
cumsum = [0,0]
for i in range(1, len(string)):
    res = cumsum[-1]
    if string[i-1] == 'A' and string[i] == 'C':
        res += 1
    cumsum.append(res)

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N, Q = map(int, input().split())
S = list(input())
lr = [list(map(int, input().split())) for _ in range(Q)]
cnt = [0]*N
is_a = False
for i in range(N-1):
  if S[i] + S[i+1] == 'AC':
    cnt[i+1] = 1
for i in range(1, N):
  cnt[i] += cnt[i-1]

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

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n = n//a
    count += 1
  if a**count == n:
    return count
  else:
    return count+1

#############
# Main Code #
#############

N,Q = IL()
S = S()

l = [0]
c = 0
for i in range(1,N):
  if S[i-1]+S[i] == "AC":
    c += 1
  l.append(c)

for _ in range(Q):
  L,R = IL()
  P(l[R-1]-l[L-1])

Submission status: Accepted

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

a = [0] * N
c = [False] * N
for i in range(1, N):
    if S[i - 1:i + 1] == "AC":
        a[i] = a[i - 1] + 1
        c[i] = True
    else:
        a[i] = a[i - 1]

for i in range(Q):
    l, r = map(int, input().split())
    if l - 1 == 0:
        print(a[r - 1])
    elif c[l - 1] is True:
        print(a[r - 1] - a[l - 1])
    else:
        print(a[r - 1] - a[l - 1])

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def solve():
    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 _ in range(Q):
        l, r = map(int, input().split())
        print(t[r-1]-t[l-1])
        
solve()

Submission status: Accepted

Solution code:
import bisect

def solve():
    N, Q = map(int, input().split())
    S = input()
    
    index = []
    for i in range(N-1):
        if S[i:i+2] == 'AC':
            index.append(i+1)
    for _ in range(Q):
        l, r = map(int, input().split())
        print(bisect.bisect_left(index, r) - bisect.bisect_left(index, l))
        
solve()

Submission status: Accepted

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

ns = [None] * n
ns[0] = 0
nn = 0
for i in range(n - 1):
    if s[i] == 'A' and s[i + 1] == 'C':
        nn += 1
    ns[i + 1] = nn
for i in range(q):
    l, r = map(int, input().split())
    l -= 1
    r -= 1

    print(ns[r] - ns[l])

Submission status: Accepted

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

Submission status: Accepted

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

d= {}

for k in range(n):
    d[k]=0

total = 0
for j in range(n-1):
    if s[j]=='A' and s[j+1]=='C':
        total+=1
        d[j+1]+=total
    else:
        d[j+1]=total
        
# print(d)

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

    ans.append(d[r-1] -d[l-1])


for i in ans:
    print(i)

Submission status: Accepted

Solution code:
x,y= list(map(int, input().split()))
s=input()
anslis=[0]*x
ans=0
for k in range(1,x):
    if s[k-1:k+1]=="AC":
        ans+=1
    anslis[k]+=ans

for k in  range(y):
    i,j= list(map(int, input().split()))
    i-=1
    j-=1
    print(anslis[j]-anslis[i])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
acList = [0 for n in range(N+1)]
for i in range(N):
    if("AC" == S[i:i+2]):
        acList[i+1] += acList[i] + 1
    else:
        acList[i+1] += acList[i]
for q in range(Q):
    left, right = map(int, input().split())
    print (acList[right-1] - acList[left-1])

Submission status: Accepted

Solution code:
import itertools
n, q = map(int, input().split())
s = input()
cnt = [0] * n
for i in range(1, n):
    if s[i] == 'C' and s[i - 1] == 'A':
        cnt[i] = 1
cnt = list(itertools.accumulate(cnt))
for _ in range(q):
    l, r = map(int, input().split())
    print(cnt[r - 1] - cnt[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 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 = map(int,input().split())
s = list(input())
check = [0]*n
const = 0
for i in range(n-1):
    check[i] = const
    if s[i] == 'A' and s[i+1] == 'C':
        check[i+1] = const + 1
        const += 1
        i += 1
check[-1] = const
for i in range(q):
    l,r = map(int,input().split())
    print(check[r-1]-check[l-1])

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
s = str(input())
l = [-1] * q
r = [-1] * q
for i in range(q):
    l[i], r[i] = map(int, input().split())
sum = [0] * (n+1)
flag_a = False
for i in range(1, n+1):
    if s[i-1] == 'A':
        flag_a = True
        sum[i] = sum[i-1]
    elif s[i-1] == 'C' and flag_a:
        flag_a = False
        sum[i] = sum[i-1] + 1
    else:
        flag_a = False
        sum[i] = sum[i-1]
for i in range(q):
    print(sum[r[i]] - sum[l[i]])

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
N, Q = map(int, input().split())
S = input()
def int_(num_str):
    return int(num_str) - 1

ans = 0
ACleft = []
ACright = []
for i in range(N-1):
    if S[i] == "A" and S[i+1] == "C":
        ACleft.append(i)
        ACright.append(i+1)
for _ in range(Q):
    l, r = map(int_, input().split())
    ans = bisect_left(ACleft, r) - bisect_left(ACleft, l)
    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n, q = (int(x) for x in 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 = (int(x) for x in 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:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
# from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint

INF = float("inf")

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

def I():
    return int(input())

def MI():
    return map(int,input().split())

def RI():
    return list(map(int,input().split()))

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

def MS():
    return input().split()

def RS():
    return list(input().split())

def CS(n):
    return [ input() for _ in range(n) ]

def LS(n):
    return [ list(input().split()) for _ in range(n) ]

n,q = MI()
S = S()
lr = LI(q)

ls = [0]*n
count = 0
for i in range(1,n):
    if S[i]!="C":
        ls[i] = count
    else:
        if S[i-1]=="A":
            count += 1
            ls[i] = count
        else:
            ls[i] = count
# print(ls)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,Q=map(int, input().split())
S=input()
list=[0]*N
#計算量が多いので、さきに文字列のACの数を数えておく。
for i in range(1,N):
  if S[i-1:i+1]=="AC":
    list[i]=list[i-1]+1
  else:
    list[i]=list[i-1]
for i in range(Q):
  l,r=map(int,input().split())
  print(list[r-1]-list[l-1])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split(' '))
S = input()
queries = tuple(tuple(map(int, input().split(' '))) for _ in range(Q))

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

for left, right in queries:
    print(cumsum[right - 1] - cumsum[left - 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 i in range(q):
    l, r = map(int, input().split())
    print(t[r - 1] - t[l - 1])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import itertools
import math
import fractions
import functools
import copy
n, q = map(int, input().split())
s = input()
l = []
r = []
for i in range(q):
    li, ri = map(int, input().split())
    li -= 1
    ri -= 1
    l.append(li)
    r.append(ri)

count_list = [0] * n
amount = 0
for i in range(1, n):
    if s[i-1] == "A" and s[i] == "C":
        amount += 1
        count_list[i] = amount
    else:
        count_list[i] = amount


count = 0


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

Submission status: Accepted

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

def main():
    n, q = map(int, input().split())
    s = input()
    t = [0]*(n + 1)

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

main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ac = [0 for _ in range(N+1)]

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

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

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

Submission status: Accepted

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

ansLis = [0]
tmp = 0
flgA = False
for i in range(N):
    if flgA and S[i]=="C":
        tmp += 1
    ansLis.append(tmp)
    flgA = (S[i] == "A")
# print(ansLis)

ans = []
for _ in range(Q):
    l,r = map(int,input().split())
    num = ansLis[r] - ansLis[l]
    # if S[l-2] == "A" and S[l-1] == "C":
    #     num = max(0, num-1)
    ans.append(num)

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

Submission status: Accepted

Solution code:
n,q=map(int,input().split())
s=str(input())
a=[0]*n
s=list(s)
for i in range(1,n):
  temp=0
  if s[i]=="C" and s[i-1]=="A":
    temp=1
  a[i]=a[i-1]+temp
for i in range(0,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()
lis=[]
for i in range(q):
    lis.append(list(map(int, input().split())))
idx=0
acs=[0]*n
for _ in s:
    if s[idx]=="A" and s[idx+1]=="C":
        acs[idx+1] = 1
        idx+=1
    idx+=1
    if idx>=(len(s)-1):
        break
cnt=0
#print(acs)
for i,el in enumerate(acs):
    if el!=0:
        cnt+=el
        acs[i]=cnt
    else:
        acs[i]=cnt
#print(acs)
for i in lis:
    print(acs[i[1]-1]-acs[i[0]-1])

Submission status: Accepted

Solution code:
import sys
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def main():
    #n = int(stdin.readline().rstrip())
    #l = list(map(int, stdin.readline().rstrip().split()))
    n,q = map(int, stdin.readline().rstrip().split())
    s = str(stdin.readline().rstrip())
    #word = [stdin.readline().rstrip() for _ in range(n)]
    #number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
    #zeros = [[0] * w for i in range(h)]
    cum = [0]*(n)
    cum[0] = 0
    for i in range(1,n):
        if s[i-1:i+1] == "AC":
            cum[i] = cum[i-1] + 1
        else:
            cum[i] = cum[i-1]
    for i in range(q):
        l,r = map(int, stdin.readline().rstrip().split())
        print(cum[r-1] - cum[l-1])

main()

Submission status: Accepted

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

import sys
# import math
# from string import ascii_lowercase, ascii_upper_case, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference
# from fractions import gcd                    # for Python 3.4 (previous contest @AtCoder)


def main():
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79...e+308
    # inf = 2 ** 64 - 1               # (for fast JIT compile in PyPy) 1.84...e+19
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    n, q = mi()
    s = input()
    query = [lmi() for _ in range(q)]
    AC_A_memo = []
    for i in range(n - 1):
        if s[i] == 'A' and s[i + 1] == 'C':
            AC_A_memo.append(i)

    for l, r in query:
        # s[l-1]...s[r-1]
        # AC_A_memo のうち、l-1 以上 r-2 以下のものの個数が答え
        print(bisect_right(AC_A_memo, r-2) - bisect_left(AC_A_memo, l-1))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

L = [0]*n
for i in range(1,n):
    if s[i-1] == "A" and s[i] == "C":
        L[i] = L[i-1] + 1
    else:
        L[i] = L[i-1]

for v in lr:
    print(L[v[1]-1] - L[v[0]-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N, Q = map(int, input().split())
S = input()
LR = []
for i in range(Q):
    l, r = map(int, input().split())
    LR.append([l,r])

A = [0]

cnt = 0
for i in range(1, len(S)):
    if S[i-1:i+1] == 'AC':
        cnt += 1
    A.append(cnt)

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = int(input())
n, q  = map(int, input().split())
s  = input()
#  = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

nac = []
c = 0
for i, s_ in enumerate(s):
    if s[i] == 'C':
        if s[i-1] == 'A':
            c += 1

    nac.append(c)

#print(nac)

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

Submission status: Accepted

Solution code:
import bisect

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

AC_l_list = []
AC_r_list = []

for i in range(N - 1):
    if S[i] == 'A' and S[i + 1] == 'C':
        AC_l_list.append(i)
        AC_r_list.append(i + 1)
        
        
for l, r in lr_list:
    i = bisect.bisect_left(AC_l_list, l - 1)
    j = bisect.bisect_right(AC_r_list, r - 1)
    print(j - i)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    # n, q = map(int, input().split())
    # s = input()
    # # l = list()
    # # r = list()
    # s_list = list()
    # # s_tuple = tuple()
    # for _ in range(q):
    #     l_i, r_i = map(int, input().split())
    #     # l.append(l_i)
    #     # r.append(r_i)
    #     s_list.append(s[l_i - 1:r_i])
    #     # s_tuple = s_tuple + tuple(s[l_i - 1:r_i])
    # 
    # s_tuple = tuple(s_list)
    # 
    # for i in s_tuple:
    #     print(i.count('AC'))
    N, Q = map(int, input().split())
    S = input()
    cnt = [0] * (N + 1)
    for i in range(N):
        cnt[i + 1] = cnt[i] + (1 if S[i:i+2] == 'AC' else 0)
    for i in range(Q):
        l, r = map(int, input().split())
        print(cnt[r - 1] - cnt[l - 1])


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

from collections import deque
N,Q = (int(x) for x in input().split())
S = list(input())
d = deque(S)
cum = [0] * N
count = 0
t1 = d.popleft()
for i in range(1,N):
    t2 = d.popleft()
    if t1 == 'A' and t2 == 'C':
        count += 1
    cum[i] = count
    t1 = t2
for i in range(Q):
    l,r = (int(x) for x in input().split())
    print(cum[r-1]-cum[l-1])

Submission status: Accepted

Solution code:
from itertools import accumulate
N, Q = map(int, input().split())
S = input()
counts = [[0] * N for _ in range(2)]
for i, c in enumerate(zip(S[:-1], S[1:])):
    counts[i & 1][i] = int(c == ("A", "C"))
for i in range(2):
    counts[i] = list(accumulate(reversed(counts[i])))[::-1]
for _ in range(Q):
    l, r = map(lambda n: int(n) - 1, input().split())
    print(counts[0][l] - counts[0][r] + counts[1][l] - counts[1][r])

Submission status: Accepted

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

for i in range(len(s)-1):
  if s[i]+s[i+1]=="AC":
    ans[i+1]=1
ansr=list(itertools.accumulate(ans))

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

Submission status: Accepted

Solution code:
n,q=(map(int,input().split()))
s=input()
count=0
c=[0]*n
for i in range(1,n):
        if s[i-1]=='A':
            if s[i]=='C':
               count+=1
        c[i]=count
for _ in range(q):
    l,r=(map(int,input().split())) 
    print(c[r-1]-c[l-1])