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)

// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let mut s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

struct SegTree {
    data: Vec<i64>,
}

impl SegTree {
    fn id() -> i64 {
        0
    }

    fn op(a: i64, b: i64) -> i64 {
        a + b
    }

    fn new(v: Vec<i64>) -> SegTree {
        let mut n = v.len().next_power_of_two();
        let mut data = vec![Self::id(); n * 2];
        for (i, x) in v.into_iter().enumerate() {
            data[n + i] = x;
        }
        for i in (1..n).rev() {
            data[i] = Self::op(data[i * 2], data[i * 2 + 1]);
        }
        SegTree { data: data }
    }

    // update data[n] to x
    fn update(&mut self, n: usize, v: i64) {
        let mut n = n + self.data.len() / 2;
        self.data[n] = v;
        while n > 0 {
            n = n / 2;
            self.data[n] = Self::op(self.data[n * 2], self.data[n * 2 + 1]);
        }
    }

    // query for data[l, r)
    fn query(&mut self, l: usize, r: usize) -> i64 {
        let mut l = l + self.data.len() / 2;
        let mut r = r + self.data.len() / 2;
        let mut vl = Self::id();
        let mut vr = Self::id();
        while l < r {
            if l % 2 == 1 {
                vl = Self::op(vl, self.data[l]);
            }
            l = (l + 1) / 2;

            if r % 2 == 1 {
                vr = Self::op(self.data[r - 1], vr);
            }
            r = r / 2;
        }
        Self::op(vl, vr)
    }
}

fn main() {
    input! {
        n: usize, q: usize,
        s: chars,
        lr: [(usize, usize); q]
    }

    let mut v = vec![0; n + 1];
    for i in 1..s.len() {
        if s[i - 1] == 'A' && s[i] == 'C' {
            v[i + 1] = 1;
        }
    }

    let mut t = SegTree::new(v);
    for i in 0..q {
        let ans = t.query(lr[i].0 + 1, lr[i].1 + 1);
        println!("{}", ans);
    }
}