Problem p03618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using P = pair<long long, long long>;
#define rep(i, n) for (long long i = 0; i < (long long)n; i++)
#define FOR(i, a, b) for (long long i = a; i < b; i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((long long)(x).size())
#define COUT(x) cout << x << endl
#define PB(x) push_back(x)
#define MP make_pair
#define vint vector<int>
#define vvint vector<vector<int>>
#define vstr vector<string>
#define SUM(x) accumulate(x.begin(), x.end(), 0)
#define MAX(x) *max_element(x.begin(), x.end())
#define MIN(x) *min_element(x.begin(), x.end())
#define couty cout << 'Y' << 'e' << 's' << endl
#define coutn cout << 'N' << 'o' << endl
#define coutY cout << 'Y' << 'E' << 'S' << endl
#define coutN cout << 'N' << 'O' << endl
#define yn(x) cout << (x ? "Yes" : "No") << endl
#define YN(x) cout << (x ? "YES" : "NO") << endl
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
const long long dx[4] = {1, 0, -1, 0};
const long long dy[4] = {0, 1, 0, -1};
const long long INF = 1e12;
const long long MOD = 1e9 + 7;
// cout<<fixed<<setprecision(15)<<

signed main() {
  string s;
  cin >> s;
  int n = s.size();
  int res = 1;
  rep(i, n - 1) {
    FOR(j, i + 1, n) {
      if (s[i] != s[j])
        res++;
    }
  }
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using P = pair<long long, long long>;
#define rep(i, n) for (long long i = 0; i < (long long)n; i++)
#define FOR(i, a, b) for (long long i = a; i < b; i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((long long)(x).size())
#define COUT(x) cout << x << endl
#define PB(x) push_back(x)
#define MP make_pair
#define vint vector<int>
#define vvint vector<vector<int>>
#define vstr vector<string>
#define SUM(x) accumulate(x.begin(), x.end(), 0)
#define MAX(x) *max_element(x.begin(), x.end())
#define MIN(x) *min_element(x.begin(), x.end())
#define couty cout << 'Y' << 'e' << 's' << endl
#define coutn cout << 'N' << 'o' << endl
#define coutY cout << 'Y' << 'E' << 'S' << endl
#define coutN cout << 'N' << 'O' << endl
#define yn(x) cout << (x ? "Yes" : "No") << endl
#define YN(x) cout << (x ? "YES" : "NO") << endl
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
const long long dx[4] = {1, 0, -1, 0};
const long long dy[4] = {0, 1, 0, -1};
const long long INF = 1e12;
const long long MOD = 1e9 + 7;
// cout<<fixed<<setprecision(15)<<

signed main() {
  string s;
  cin >> s;
  int n = s.size();
  int res = (n) * (n - 1) / 2 + 1;
  map<char, int> map;
  rep(i, n) map[s[i]]++;
  // rep(i,n) res-=map[s[i]]*(map[s[i]]-1)/2;
  for (int i = 'a'; i <= 'z'; i++) {
    char c = i;
    // cout<<c<<' '<<map[c]<<endl;
    res -= map[c] * (map[c] - 1) / 2;
  }
  cout << res << endl;
}