fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. string a;
  31. vector<vector<int>> qr;
  32.  
  33. vector<int> consistency(int n, int q){
  34.  
  35. vector<vector<int>> mp(n+1, vector<int>(26, 0));
  36.  
  37. for(int i=0; i<n; i++){
  38. mp[i+1] = mp[i];
  39. mp[i+1][a[i] - 'a']++;
  40. }
  41.  
  42. vector<int> ans;
  43.  
  44. for(auto& qq : qr){
  45. int L = qq[0];
  46. int R = qq[1];
  47.  
  48. int val = 0;
  49. for(int j=0; j<26; j++){
  50.  
  51. int ct = mp[R][j] - mp[L-1][j];
  52.  
  53. val += (ct * (ct+1))/2;
  54. }
  55. ans.push_back(val);
  56.  
  57. }
  58.  
  59. return ans;
  60.  
  61. }
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77. vector<int> practice(int n, int q){
  78.  
  79.  
  80. }
  81.  
  82.  
  83.  
  84.  
  85.  
  86. void solve() {
  87.  
  88. int n, q;
  89. cin>> n >> q;
  90.  
  91. cin >> a;
  92.  
  93. qr.resize(q);
  94. for(int i=0; i<q; i++) {
  95. int x, y;
  96. cin >> x >> y;
  97. qr[i] = {x,y};
  98. }
  99.  
  100. auto ans = consistency(n, q);
  101.  
  102. for(auto& t : ans) cout << t << " "; cout << endl;
  103.  
  104.  
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111. int32_t main() {
  112. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  113.  
  114. int t = 1;
  115. // cin >> t;
  116. while (t--) {
  117. solve();
  118. }
  119.  
  120. return 0;
  121. }
Success #stdin #stdout 0s 5312KB
stdin
6 4
abcaab
1 1
2 5
3 6
1 6
stdout
1 5 5 10