Problem p03618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define INF_LL (ll)1e18
#define INF (int)1e9
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
using ll = long long;
using PII = pair<int, int>;

const double eps = 1e-10;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int rad[414514] = {};
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string a, s;
  cin >> a;
  int cnt[26] = {};
  ll count = 0;
  REP(i, a.size()) {
    count += cnt[a[i] - 'a'];
    cnt[a[i] - 'a']++;
  }
  REP(i, a.size()) {
    s = s + a[i];
    if (i != a.size() - 1)
      s += '0';
  }
  int mx = 0;
  int j = 0;
  /*
  for(int i=0;i < s.size();){
          while(0 <= i-j && i+j < s.size() && s[i-j]==s[i+j]) j++;
          rad[i] = j;
          mx = max(mx, j);
          int k;
          for( k = 1;0 <= i-k && i+k < s.size() && rad[i-k] < rad[i]-k;k++){
                  rad[i+k] = rad[i-k];
          }
          i+=k;
          j = max(j-k, 0);
  }
  ll res = 0;
  REP(i, s.size()){
          if(i%2 == 0)
                  res += max((rad[i]-1)/2, 0);
          else
                  res += max((rad[i])/2, 0);
  }
  */
  cout << (a.size()) * (a.size() - 1) / 2 - count + 1 << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define INF_LL (ll)1e18
#define INF (int)1e9
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
using ll = long long;
using PII = pair<int, int>;

const double eps = 1e-10;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int rad[414514] = {};
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string a, s;
  cin >> a;
  int cnt[26] = {};
  ll count = 0;
  REP(i, a.size()) {
    count += cnt[a[i] - 'a'];
    cnt[a[i] - 'a']++;
  }
  int mx = 0;
  int j = 0;
  /*
  for(int i=0;i < s.size();){
          while(0 <= i-j && i+j < s.size() && s[i-j]==s[i+j]) j++;
          rad[i] = j;
          mx = max(mx, j);
          int k;
          for( k = 1;0 <= i-k && i+k < s.size() && rad[i-k] < rad[i]-k;k++){
                  rad[i+k] = rad[i-k];
          }
          i+=k;
          j = max(j-k, 0);
  }
  ll res = 0;
  REP(i, s.size()){
          if(i%2 == 0)
                  res += max((rad[i]-1)/2, 0);
          else
                  res += max((rad[i])/2, 0);
  }
  */
  cout << (a.size()) * (a.size() - 1) / 2 - count + 1 << endl;
}