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.

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::fmt::Display;
use std::io;
use std::io::BufRead;
use std::ops::Add;
use std::ops::AddAssign;

fn main() {
    let mut sc = Scanner::new(std::io::stdin());
    let n = sc.next::<usize>();
    let q = sc.next::<usize>();
    let s = sc.next::<String>().into_bytes();
    let mut ones = vec![];
    ones.push(0);
    for i in 0..n - 1 {
        let b = if s[i] == 'A' as u8 && s[i + 1] == 'C' as u8 {
            1
        } else {
            0
        };
        let o = ones[i];
        ones.push(o + b);
    }
    for _ in 0..q {
        let l = sc.next::<usize>() - 1;
        let r = sc.next::<usize>() - 1;
        println!("{}", ones[r] - ones[l]);
    }
}

////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// TEMPLATE ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

/// ```
/// use contest::vec;
/// let mut is = vec![2,3,1];
/// assert!(vec::next_permutation(&mut is));
/// assert_eq!(vec![3,1,2], is);
/// assert!(vec::next_permutation(&mut is));
/// assert_eq!(vec![3,2,1], is);
/// assert!(!vec::next_permutation(&mut is));
/// assert_eq!(vec![1,2,3], is);
///
/// let mut is = (0..3).collect();
/// let mut count = 0;
/// loop {
///     count+=1;
///     // Do something using is
///     if !vec::next_permutation(&mut is) {
///         break;
///     }
/// }
/// assert_eq!(6, count);
///
/// let mut is = vec![1,2,1];
/// assert!(vec::next_permutation(&mut is));
/// assert_eq!(vec![2,1,1], is);
/// assert!(!vec::next_permutation(&mut is));
/// assert_eq!(vec![1,1,2], is);
/// ```
pub fn next_permutation(is: &mut Vec<i32>) -> bool {
    let n = is.len();
    for i in (1..n).rev() {
        let i = i as usize;
        if is[i - 1] < is[i] {
            let mut j = n - 1;
            while is[i - 1] >= is[j] {
                j -= 1;
            }
            is.swap(i - 1, j);
            reverse(is, i, n);
            return true;
        }
    }
    is.reverse();
    false
}

fn reverse(v: &mut Vec<i32>, from_inclusive: usize, to_exclusive: usize) {
    let mut i = from_inclusive;
    let mut j = to_exclusive - 1;
    while i < j {
        v.swap(i, j);
        i += 1;
        j -= 1;
    }
}
/// Scanner
///
/// # Example
/// ```
/// use contest::io::Scanner;
/// let mut sc = Scanner::new("1 2 \n\n \r\t \n 3.5".as_bytes());
/// assert_eq!("1".to_string(), sc.next::<String>());
/// assert_eq!(2, sc.next());
/// assert_eq!(3.5, sc.next());
///
/// // To create a scanner from stdin:
/// Scanner::new(std::io::stdin());
/// ```

pub struct Scanner<R: io::Read> {
    br: io::BufReader<R>,
    // Read tokens are stored in reversed order per line.
    buf: Vec<String>,
}

pub fn new<R: io::Read>(r: R) -> Scanner<R> {
    Scanner::new(r)
}

impl<R: io::Read> Scanner<R> {
    #[inline]
    pub fn new(r: R) -> Scanner<R> {
        Scanner {
            br: io::BufReader::new(r),
            buf: vec![],
        }
    }
    #[inline]
    pub fn next<T>(&mut self) -> T
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Debug,
    {
        self.next_string()
            .map(|s| s.parse::<T>().expect("Parse failed: "))
            .expect("Unexpected EOF")
    }
    fn next_string(&mut self) -> Option<String> {
        self.buf.pop().or_else(|| match self.update() {
            true => self.next_string(),
            false => None,
        })
    }
    #[inline]
    fn update(&mut self) -> bool {
        let mut s = String::new();
        let res = self.br.read_line(&mut s);
        match res.expect("I/O error.") {
            0 => false,
            _ => {
                self.buf = s.split_whitespace().map(|x| x.to_string()).rev().collect();
                true
            }
        }
    }
}

#[test]
fn next() {
    let mut sc = new("hoge".as_bytes());
    let s: String = sc.next();
    assert_eq!("hoge", s);
}

static mut MODULO: i64 = 1_000_000_007;

#[inline]
fn modulo() -> i64 {
    unsafe { MODULO }
}

#[derive(Copy, Clone)]
pub struct Mint(pub i64);

impl Mint {
    pub fn set_mod(modulo: i32) {
        unsafe {
            MODULO = modulo as i64;
        }
    }
    pub fn value(self) -> i64 {
        self.0
    }
}

impl ::std::fmt::Display for Mint {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// ```
/// use contest::math::Mint;
/// Mint::set_mod(5);
/// let a = Mint(2);
/// let b = Mint(3);
/// assert_eq!(0, (a + b).0);
/// assert_eq!(1, (b + b).0);
/// assert_eq!(4, (a + a).0);
/// ```
impl Add for Mint {
    type Output = Mint;
    fn add(self, o: Mint) -> Mint {
        let mut x = self.0 + o.0;
        if x >= modulo() {
            x -= modulo();
        }
        Mint(x)
    }
}

/// ```
/// use contest::math::Mint;
/// Mint::set_mod(5);
/// let mut a = Mint(2);
/// a += Mint(4);
/// assert_eq!(1, a.0);
/// ```
impl AddAssign for Mint {
    fn add_assign(&mut self, o: Mint) {
        let mut x = self.0 + o.0;
        if x >= modulo() {
            x -= modulo();
        }
        self.0 = x;
    }
}

/// ```
/// use contest::math;
/// assert_eq!(3, math::inv(2,5));
/// assert_eq!(1, math::inv(1,2));
/// assert_eq!(1, math::inv(3,2));
/// ```
pub fn inv(value: i32, modulo_prime: i32) -> i32 {
    return pow_mod(value, modulo_prime - 2, modulo_prime);
}

/// ```
/// use contest::math;
/// assert_eq!(8, math::pow_mod(2,3,10));
/// assert_eq!(1, math::pow_mod(3,4,10));
/// assert_eq!(1, math::pow_mod(0,0,3));
/// assert_eq!(0, math::pow_mod(0,0,1));
/// ```
pub fn pow_mod(value: i32, exponent: i32, modulo: i32) -> i32 {
    if exponent < 0 {
        panic!("Exponent should >= 0.");
    }
    if modulo == 1 {
        return 0;
    }
    let modulo = modulo as i64;
    let mut value = value as i64;
    if value >= modulo {
        value %= modulo;
    }
    if value < 0 {
        value = value % modulo + value;
    }
    let mut res = 1;
    let mut exponent = exponent;
    while exponent > 0 {
        if (exponent & 1) == 1 {
            res = res * value % modulo;
        }
        value = value * value % modulo;
        exponent >>= 1;
    }
    res as i32
}