fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define ld long double
  4. #define pii pair <int, int>
  5. #define pll pair <ll, ll>
  6. #define pci pair <char, int>
  7. #define pld pair <ld, ld>
  8. #define pb push_back
  9. #define eb emplace_back
  10. #define mp make_pair
  11. #define fi first
  12. #define se second
  13. #define cd complex <double>
  14. #define PI 3.14159265358979
  15. using namespace std;
  16. mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  17. int jump_l[200005][25], jump_r[200005][25];
  18. ll sum_l[200005][25], sum_r[200005][25];
  19. int n;
  20. string s;
  21. int l[200005], r[200005], cnt_cir[200005], max_l[200005], min_r[200005];
  22. int prefix[200005][27], suffix[200005][27];
  23. int locc[200005][27], rocc[200005][27], last[30];
  24. int cnt_cp[200005], cnt_cs[200005];
  25. pii last_suff[200005][27], last_pref[200005][27];
  26. bool comp(pii a, pii b){
  27. return a > b;
  28. }
  29. int main(){
  30. ios_base::sync_with_stdio(0);
  31. cin.tie(0); cout.tie(0);
  32. cin >> s; n = s.length();
  33. for (int i = 1; i <= n; i++){
  34. for (int c = 1; c <= 26; c++){
  35. locc[i][c] = last[c];
  36. prefix[i][c] = prefix[i - 1][c];
  37. }
  38. last[s[i - 1] - 'a' + 1] = i;
  39. for (int c = 1; c <= 26; c++) last_pref[i][c] = mp(last[c], c);
  40. sort(last_pref[i] + 1, last_pref[i] + 27, comp);
  41. prefix[i][s[i - 1] - 'a' + 1]++;
  42. for (int c = 1; c <= 26; c++) cnt_cp[i] += (prefix[i][c] != 0);
  43. l[i] = r[i] = i;
  44. }
  45. for (int c = 1; c <= 26; c++) last[c] = n + 1;
  46. for (int i = n; i >= 1; i--){
  47. for (int c = 1; c <= 26; c++){
  48. rocc[i][c] = last[c];
  49. suffix[i][c] = suffix[i + 1][c];
  50. }
  51. last[s[i - 1] - 'a' + 1] = i;
  52. for (int c = 1; c <= 26; c++) last_suff[i][c] = mp(last[c], c);
  53. sort(last_suff[i] + 1, last_suff[i] + 27);
  54. suffix[i][s[i - 1] - 'a' + 1]++;
  55. for (int c = 1; c <= 26; c++) cnt_cs[i] += (suffix[i][c] != 0);
  56. }
  57. ll ans = 0;
  58. for (int i = 1; i <= n; i++){
  59. jump_l[i][0] = n + 1;
  60. cnt_cir[i] = 0; max_l[i] = 0; min_r[i] = n + 1;
  61. for (int c = 1; c <= 26; c++){
  62. if (prefix[r[i]][c] - prefix[l[i] - 1][c]) cnt_cir[i]++;
  63. else{
  64. max_l[i] = max(max_l[i], locc[i][c]);
  65. min_r[i] = min(min_r[i], rocc[i][c]);
  66. }
  67. }
  68. }
  69. for (int cnt_c = 1; cnt_c <= cnt_cp[n]; cnt_c++){
  70. for (int i = 1; i <= n; i++){
  71. sum_r[i][0] = sum_l[i][0] = i;
  72. if (cnt_cp[i] >= cnt_c) jump_r[i][0] = max(jump_r[i][0], rocc[i][last_pref[i][cnt_c].se]);
  73. if (jump_r[i][0] == n + 1) jump_r[i][0] = n;
  74. if (cnt_cs[i] >= cnt_c) jump_l[i][0] = min(jump_l[i][0], locc[i][last_suff[i][cnt_c].se]);
  75. if (jump_l[i][0] == 0) jump_l[i][0] = 1;
  76. }
  77. for (int lay = 1; lay <= 17; lay++){
  78. for (int i = 1; i <= n; i++){
  79. if (jump_l[i][lay - 1] == 0) jump_l[i][lay] = 0;
  80. else{
  81. sum_l[i][lay] = sum_l[i][lay - 1] + sum_l[jump_l[i][lay - 1]][lay - 1];
  82. jump_l[i][lay] = jump_l[jump_l[i][lay - 1]][lay - 1];
  83. }
  84. if (jump_r[i][lay - 1] == n + 1) jump_r[i][lay] = n + 1;
  85. else{
  86. sum_r[i][lay] = sum_r[i][lay - 1] + sum_r[jump_r[i][lay - 1]][lay - 1];
  87. jump_r[i][lay] = jump_r[jump_r[i][lay - 1]][lay - 1];
  88. }
  89. }
  90. }
  91. for (int i = 1; i <= n; i++){
  92. if ((l[i] == 1) && (r[i] == n)) continue;
  93. if (cnt_cir[i] > cnt_c) continue;
  94. int cur_l = l[i], cur_r = r[i];
  95. for (int bit = 17; bit >= 0; bit--){
  96. if ((jump_r[cur_r][bit] >= min_r[i]) || (jump_l[cur_l][bit] <= max_l[i])) continue;
  97. ans -= sum_r[cur_r][bit]; ans += sum_l[cur_l][bit];
  98. ans += ((ll) (n - 1) << bit);
  99. cur_l = jump_l[cur_l][bit]; cur_r = jump_r[cur_r][bit];
  100. }
  101. ans += cur_l; ans -= cur_r; ans += (n - 1);
  102. l[i] = jump_l[cur_l][0]; r[i] = jump_r[cur_r][0];
  103. cnt_cir[i] = 0; max_l[i] = 0; min_r[i] = n + 1;
  104. for (int c = 1; c <= 26; c++){
  105. if (prefix[r[i]][c] - prefix[l[i] - 1][c]) cnt_cir[i]++;
  106. else{
  107. max_l[i] = max(max_l[i], locc[i][c]);
  108. min_r[i] = min(min_r[i], rocc[i][c]);
  109. }
  110. }
  111. }
  112. }
  113. cout << ans << "\n";
  114. }
Success #stdin #stdout 0s 7604KB
stdin
Standard input is empty
stdout
0