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 Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.util.*;

public class Main {
	int N, Q;
	String S;
	int[] l, r;
	List<Integer> acIndexes;
	int[] memo;

	Main() {
		Scanner in = new Scanner(System.in);
		String[] tokens = in.nextLine().split(" ");
		N = Integer.parseInt(tokens[0]);
		Q = Integer.parseInt(tokens[1]);
		S = in.nextLine();
		l = new int[Q];
		r = new int[Q];
		for (int i = 0; i < Q; ++i) {
			tokens = in.nextLine().split(" ");
			l[i] = Integer.parseInt(tokens[0]);
			r[i] = Integer.parseInt(tokens[1]);
		}
		in.close();
	}

	void calc() {
		this.memo = new int[N];
		Arrays.fill(this.memo, 0);
		this.acIndexes = new ArrayList<>();
		int count = 0;
		for (int i = 0; i < S.length() - 1; ++i) {
			if (S.charAt(i) == 'A' && S.charAt(i + 1) == 'C') {
				++count;
				Arrays.fill(this.memo, i, this.memo.length, count);
				this.acIndexes.add(i);
				++i;
			}
		}
		/*
		for (int i = 0; i < this.memo.length; ++i) {
			System.out.println(" " + this.memo[i]);
		}*/

		for (int i = 0; i < Q; ++i) {
			int left = 0;
			if (l[i] == 1) {
				left = 0;
			} else {
				left = this.memo[l[i] - 2];
			}
			int right = this.memo[r[i] - 2];
			// int left = findRight(this.acIndexes, l[i] - 1);
			// int right = findRight(this.acIndexes, r[i] - 1);
			// System.out.println(left + ", " + right);
			int result = right - left;
			System.out.println(result);
		}

	}

	/**
	 * 最終的に，indexesのうちval以上のものの個数を返す
	 * 
	 * @param indexes
	 * @param val
	 * @return
	 */
	int findRight(List<Integer> indexes, int val) {
		if (indexes.isEmpty()) {
			return 0;
		} else if (indexes.size() == 1) {
			int i = indexes.get(0);
			if (i >= val) {
				return 1;
			} else {
				return 0;
			}
		}
		if (indexes.get(0) >= val) {
			return indexes.size();
		}
		if (indexes.get(indexes.size() - 1) < val) {
			return 0;
		}
		int midIndex = indexes.size() / 2;
		int midI = indexes.get(midIndex);
		if (midI >= val) {
			// midI -- size()まではval以上である
			int result = indexes.size() - midIndex;
			result += findRight(indexes.subList(0, midIndex), val);
			return result;
		} else {
			return findRight(indexes.subList(midIndex, indexes.size()), val);
		}
	}

	public static void main(String[] args) {
		Main ins = new Main();
		ins.calc();
	}

}