fork download
  1.  
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int main() {
  6. int n, q;
  7. cin >> n >> q;
  8.  
  9. string s;
  10. cin >> s;
  11.  
  12. vector<array<int, 26>> pref(n + 1);
  13. pref[0].fill(0);
  14.  
  15. for (int i = 1; i <= n; i++) {
  16. pref[i] = pref[i - 1];
  17. pref[i][s[i - 1] - 'a']++;
  18. }
  19.  
  20. while (q--) {
  21. int l, r;
  22. cin >> l >> r;
  23.  
  24. long long ans = 0;
  25.  
  26. for (int c = 0; c < 26; c++) {
  27. long long f = pref[r][c] - pref[l - 1][c];
  28. ans += f * (f + 1) / 2;
  29. }
  30.  
  31. cout << ans << '\n';
  32. }
  33.  
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0s 5320KB
stdin
6 4
abcaab
1 1 
2 5
3 6
1 6
stdout
1
5
5
10