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 JavaScript (node.js v5.12) (judge verdict: Time Limit Exceeded)

!function(n){var t={};function e(r){if(t[r])return t[r].exports;var u=t[r]={i:r,l:!1,exports:{}};return n[r].call(u.exports,u,u.exports,e),u.l=!0,u.exports}e.m=n,e.c=t,e.d=function(n,t,r){e.o(n,t)||Object.defineProperty(n,t,{enumerable:!0,get:r})},e.r=function(n){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(n,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(n,"__esModule",{value:!0})},e.t=function(n,t){if(1&t&&(n=e(n)),8&t)return n;if(4&t&&"object"==typeof n&&n&&n.__esModule)return n;var r=Object.create(null);if(e.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:n}),2&t&&"string"!=typeof n)for(var u in n)e.d(r,u,function(t){return n[t]}.bind(null,u));return r},e.n=function(n){var t=n&&n.__esModule?function(){return n.default}:function(){return n};return e.d(t,"a",t),t},e.o=function(n,t){return Object.prototype.hasOwnProperty.call(n,t)},e.p="",e(e.s=2)}([function(n,t){n.exports=require("fs")},function(n,t){n.exports=require("readline")},function(n,t,e){"use strict";e.r(t);var r={A_DoubleHelix:function(n){switch(n[0]){case"A":return["T"];case"T":return["A"];case"C":return["G"];case"G":return["C"];default:throw new Error("invalid string")}},B_ATCoder:function(n){for(var t=n[0],e=0,r=0,u=0;r<t.length;r+=1)o=t[r],"A"===o||"T"===o||"C"===o||"G"===o?u+=1:u>e&&(e=u,u=0);var o;return u>e&&(e=u),[String(e)]},C_GeT_AC:function(n){var t=n[0].split(" ").map(function(n){return Number(n)}),e=(t[0],t[1],n[1]),r=n.slice(2).map(function(n){return n.split(" ").map(function(n){return Number(n)})}),u=e.split("").map(function(n,t,e){return"A"===n&&e.length>t+1&&"C"===e[t+1]});return r.map(function(n){var t=n[0],e=n[1];return u.slice(t-1,e-1).filter(function(n){return!0===n}).length}).map(function(n){return String(n)})}},u=!0,o=!1;function i(n){var t=function(n){for(var t=[],e=0;e<n.length-1;e+=1)n[e]===u&&n[e+1]===o&&t.push(e);return t}(n);return 0===t.length?0:1+Math.max.apply(Math,t.map(function(t){return i(function(n,t){var e=n.concat();return e[t]=!e[t],e[t+1]=!e[t+1],e}(n,t))}))}function c(n){return n.length<2?0:Math.max.apply(Math,n.map(function(t,e){var r=n.filter(function(n,t){return t!==e});return Math.max.apply(Math,r.map(function(n,e){return function(n,t){var e=n+t;return 0==(e&e-1)}(t,n)?c(r.filter(function(n,t){return t!==e}))+1:0}))}))}var a={AIrreversibleOperation:function(n){return[i(n[0].split("").map(function(n){return"B"===n}))]},BPowerOfTwo:function(n){Number(n[0]);var t=function(n){var t=n.filter(function(n){return n%2==1}),e=n.filter(function(n){return n%2==0});return{odds:t,evens:e}}(n[1].split(" ").map(function(n){return Number(n)})),e=t.odds,r=t.evens;return[c(e)+c(r)]}},l=e(0),f=e.n(l),s=e(1);function p(){process.stdin.resume(),process.stdin.setEncoding("utf8");var n=[],t=s.createInterface({input:process.stdin,output:process.stdout});return t.on("line",function(t){n.push(t)}),new Promise(function(e){t.on("close",function(){e(n)})})}var h=function(n,t,e,r){return new(e||(e=Promise))(function(u,o){function i(n){try{a(r.next(n))}catch(n){o(n)}}function c(n){try{a(r.throw(n))}catch(n){o(n)}}function a(n){n.done?u(n.value):new e(function(t){t(n.value)}).then(i,c)}a((r=r.apply(n,t||[])).next())})},d=function(n,t){var e,r,u,o,i={label:0,sent:function(){if(1&u[0])throw u[1];return u[1]},trys:[],ops:[]};return o={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function c(o){return function(c){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;i;)try{if(e=1,r&&(u=2&o[0]?r.return:o[0]?r.throw||((u=r.return)&&u.call(r),0):r.next)&&!(u=u.call(r,o[1])).done)return u;switch(r=0,u&&(o=[2&o[0],u.value]),o[0]){case 0:case 1:u=o;break;case 4:return i.label++,{value:o[1],done:!1};case 5:i.label++,r=o[1],o=[0];continue;case 7:o=i.ops.pop(),i.trys.pop();continue;default:if(!(u=(u=i.trys).length>0&&u[u.length-1])&&(6===o[0]||2===o[0])){i=0;continue}if(3===o[0]&&(!u||o[1]>u[0]&&o[1]<u[3])){i.label=o[1];break}if(6===o[0]&&i.label<u[1]){i.label=u[1],u=o;break}if(u&&i.label<u[2]){i.label=u[2],i.ops.push(o);break}u[2]&&i.ops.pop(),i.trys.pop();continue}o=t.call(n,i)}catch(n){o=[6,n],r=0}finally{e=u=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,c])}}};function b(n){return h(this,void 0,void 0,function(){var t,e,r,u,o,i;return d(this,function(c){switch(c.label){case 0:return 1===n.length?[2,n]:(t=n.slice(0,Math.ceil(n.length/2)),e=n.slice(Math.ceil(n.length/2)),[4,b(t)]);case 1:return r=c.sent(),[4,b(e)];case 2:u=c.sent(),o=[],c.label=3;case 3:return r.length>0&&u.length>0?[4,v(r[0],u[0])]:[3,5];case 4:return(i=c.sent()?r.shift():u.shift())&&o.push(i),[3,3];case 5:return o.push.apply(o,r.concat(u)),[2,o]}})})}function v(n,t){return h(this,void 0,void 0,function(){return d(this,function(e){switch(e.label){case 0:return console.log("? "+n+" "+t),[4,f.a.readFileSync("/dev/stdin","utf8")];case 1:return[2,"<"===e.sent()]}})})}var y={beginner_contest_122:r,GrandContest029:a,interactiveSorting:function(n){return h(this,void 0,void 0,function(){var t,e,r;return d(this,function(u){switch(u.label){case 0:return t=n[0].split(" ").map(function(n){return Number(n)}),e=t[0],t[1],r="A".charCodeAt(0),[4,b(Array.apply(null,new Array(e)).map(function(n,t){return String.fromCharCode(r+t)}))];case 1:return[2,["! "+u.sent().join("")]]}})})},welcomeToAtCoder:function(n){var t=Number(n[0]),e=n[1].split(" ").map(function(n){return Number(n)});return[t+e[0]+e[1]+" "+n[2]]}},m=function(n,t,e,r){return new(e||(e=Promise))(function(u,o){function i(n){try{a(r.next(n))}catch(n){o(n)}}function c(n){try{a(r.throw(n))}catch(n){o(n)}}function a(n){n.done?u(n.value):new e(function(t){t(n.value)}).then(i,c)}a((r=r.apply(n,t||[])).next())})},g=function(n,t){var e,r,u,o,i={label:0,sent:function(){if(1&u[0])throw u[1];return u[1]},trys:[],ops:[]};return o={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function c(o){return function(c){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;i;)try{if(e=1,r&&(u=2&o[0]?r.return:o[0]?r.throw||((u=r.return)&&u.call(r),0):r.next)&&!(u=u.call(r,o[1])).done)return u;switch(r=0,u&&(o=[2&o[0],u.value]),o[0]){case 0:case 1:u=o;break;case 4:return i.label++,{value:o[1],done:!1};case 5:i.label++,r=o[1],o=[0];continue;case 7:o=i.ops.pop(),i.trys.pop();continue;default:if(!(u=(u=i.trys).length>0&&u[u.length-1])&&(6===o[0]||2===o[0])){i=0;continue}if(3===o[0]&&(!u||o[1]>u[0]&&o[1]<u[3])){i.label=o[1];break}if(6===o[0]&&i.label<u[1]){i.label=u[1],u=o;break}if(u&&i.label<u[2]){i.label=u[2],i.ops.push(o);break}u[2]&&i.ops.pop(),i.trys.pop();continue}o=t.call(n,i)}catch(n){o=[6,n],r=0}finally{e=u=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,c])}}};!function(){m(this,void 0,void 0,function(){var n;return g(this,function(t){switch(t.label){case 0:return[4,p()];case 1:return n=t.sent(),y.beginner_contest_122.C_GeT_AC(n).forEach(function(n){return console.log(n)}),[2]}})})}()}]);
//# sourceMappingURL=main.js.map