Problem p02873 (C++)

Buggy submission (status: Time Limit Exceeded):
// Author : Ritik Patel
// Institution: Pandit Deendayal Petroleum University

#include <bits/stdc++.h>
using namespace std;

#define ub upper_bound // first element >  val(itr)
#define lb lower_bound // first element >= val(itr)

#define pb push_back
#define sz(a) int((a).size())
#define all(c) (c).begin(), (c).end()
#define rall(a) (a).rbegin(), (a).rend()
#define tr(c, i) for (typeof((c)).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define what_is(x) cout << #x << " is: " << x << endl;
#define UNIQUE(v)                                                              \
  do {                                                                         \
    sort((v).begin(), (v).end());                                              \
    (v).erase(unique((v).begin(), (v).end()), (v).end());                      \
  } while (false)

#define bitcount(a) __builtin_popcount(a) // count set bits
#define lzcount(x)                                                             \
  __builtin_clz(x) // count leading zeros in binary representation of number
#define tzcount(x)                                                             \
  __builtin_ctz(x) // count trailing zeros in binary representation of number

#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr);

typedef long long ll;
typedef long double lld;
const ll MOD = 1e9 + 7;
const lld PI = acos(-1.0);

template <typename T> T gcd(T a, T b) {
  if (b > a)
    return gcd(b, a);
  return b == 0 ? a : gcd(b, a % b);
}
template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); }
template <typename T> T fast_power(T x, T y) {
  T ans = 1;
  while (y > 0) {
    if (y & 1LL)
      ans = (ans * x);
    y >>= 1LL;
    x = (x * x);
  }
  return ans;
}
template <typename X> inline X abs(const X &a) { return a < 0 ? -a : a; }
template <typename X> inline X sqr(const X &a) { return a * a; }
template <typename T> inline string toStr(T x) {
  stringstream st;
  st << x;
  string s;
  st >> s;
  return s;
}

// ll findMMI_fermat(ll n,ll M) {ll ans= fast_power(n,M-2,M);return ans;}//i.e
// In (a/b)%M..it calculates MMI of b wrt M,only if M is prime ll add(ll a,ll
// b,ll M)  { return mod(( mod(a,M ) + mod(b,M )),M);     } ll sub(ll a, ll b,ll
// M) { return mod((mod(a,M ) + M - mod(b,M )),M);   }
ll mult(ll a, ll b, ll M) { return (a % M * b % M) % M; }
bool isprime(ll a) {
  if (a == 2) {
    return 1;
  }
  if (!(a & 1)) {
    return 0;
  }
  for (ll i = 3; i * i <= a; i += 2) {
    if (a % i == 0) {
      return 0;
    }
  }
  return 1;
}

template <typename T>
T extgcd(T a, T b, T &x, T &y) { // when a and b are coprimes
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  T p = b / a;
  T g = extgcd(b - p * a, a, y, x);
  x -= p * y;
  return g;
}

ll inv(ll a, ll m) {
  ll x, y;
  extgcd(a, m, x, y);
  return (x + m) % m;
}
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

const int INF = 1 << 30;
const ll LINF = 1ll << 60;

/* For counting number of digits, directly do floor(log10(n)+1)
               /* -------------------------------Main
   Code------------------------------- */

int main() {
  FAST string s;
  cin >> s;
  int prev;
  if (s[0] == '<')
    prev = 0;
  else
    prev = 1;
  int done = 1;
  ll ans = 0;
  for (int i = 0; i < sz(s);) {
    int len1 = 0, len2 = 0;
    if (s[i] == '<') {
      int j = i;
      while (s[j] == '<' && j < sz(s)) {
        j++;
      }
      int k = j;
      while (s[k] == '>' && k < sz(s)) {
        k++;
      }
      len1 = j - i;
      len2 = k - j;
      if (len1 == len2) {
        ans += (ll)len1 * (len1 - 1) / 2;
        ans += (ll)len2 * (len2 + 1) / 2;
      } else {
        int mn = min(len1, len2);
        int mx = max(len1, len2);
        ans += (ll)mn * (mn - 1) / 2;
        ans += (ll)mx * (mx + 1) / 2;
      }
      i = k;
    } else {
      int j = i;
      while (s[j] == '>' && j < sz(s)) {
        j++;
      }
      int len = j - i;
      ans += (ll)len * (len + 1) / 2;
    }
  }
  cout << ans << "\n";
  return 0;
}


Fixed submission:
// Author : Ritik Patel
// Institution: Pandit Deendayal Petroleum University

#include <bits/stdc++.h>
using namespace std;

#define ub upper_bound // first element >  val(itr)
#define lb lower_bound // first element >= val(itr)

#define pb push_back
#define sz(a) int((a).size())
#define all(c) (c).begin(), (c).end()
#define rall(a) (a).rbegin(), (a).rend()
#define tr(c, i) for (typeof((c)).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define what_is(x) cout << #x << " is: " << x << endl;
#define UNIQUE(v)                                                              \
  do {                                                                         \
    sort((v).begin(), (v).end());                                              \
    (v).erase(unique((v).begin(), (v).end()), (v).end());                      \
  } while (false)

#define bitcount(a) __builtin_popcount(a) // count set bits
#define lzcount(x)                                                             \
  __builtin_clz(x) // count leading zeros in binary representation of number
#define tzcount(x)                                                             \
  __builtin_ctz(x) // count trailing zeros in binary representation of number

#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr);

typedef long long ll;
typedef long double lld;
const ll MOD = 1e9 + 7;
const lld PI = acos(-1.0);

template <typename T> T gcd(T a, T b) {
  if (b > a)
    return gcd(b, a);
  return b == 0 ? a : gcd(b, a % b);
}
template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); }
template <typename T> T fast_power(T x, T y) {
  T ans = 1;
  while (y > 0) {
    if (y & 1LL)
      ans = (ans * x);
    y >>= 1LL;
    x = (x * x);
  }
  return ans;
}
template <typename X> inline X abs(const X &a) { return a < 0 ? -a : a; }
template <typename X> inline X sqr(const X &a) { return a * a; }
template <typename T> inline string toStr(T x) {
  stringstream st;
  st << x;
  string s;
  st >> s;
  return s;
}

// ll findMMI_fermat(ll n,ll M) {ll ans= fast_power(n,M-2,M);return ans;}//i.e
// In (a/b)%M..it calculates MMI of b wrt M,only if M is prime ll add(ll a,ll
// b,ll M)  { return mod(( mod(a,M ) + mod(b,M )),M);     } ll sub(ll a, ll b,ll
// M) { return mod((mod(a,M ) + M - mod(b,M )),M);   }
ll mult(ll a, ll b, ll M) { return (a % M * b % M) % M; }
bool isprime(ll a) {
  if (a == 2) {
    return 1;
  }
  if (!(a & 1)) {
    return 0;
  }
  for (ll i = 3; i * i <= a; i += 2) {
    if (a % i == 0) {
      return 0;
    }
  }
  return 1;
}

template <typename T>
T extgcd(T a, T b, T &x, T &y) { // when a and b are coprimes
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  T p = b / a;
  T g = extgcd(b - p * a, a, y, x);
  x -= p * y;
  return g;
}

ll inv(ll a, ll m) {
  ll x, y;
  extgcd(a, m, x, y);
  return (x + m) % m;
}
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

const int INF = 1 << 30;
const ll LINF = 1ll << 60;

/* For counting number of digits, directly do floor(log10(n)+1)
               /* -------------------------------Main
   Code------------------------------- */

int main() {
  FAST string s;
  cin >> s;
  int prev;
  if (s[0] == '<')
    prev = 0;
  else
    prev = 1;
  int done = 1;
  ll ans = 0;
  for (int i = 0; i < sz(s);) {
    int len1 = 0, len2 = 0;
    if (s[i] == '<') {
      int j = i;
      while (s[j] == '<' && j < sz(s)) {
        j++;
      }
      int k = j;
      while (s[k] == '>' && k < sz(s)) {
        k++;
      }
      len1 = j - i;
      len2 = k - j;
      if (len1 == len2) {
        ans += (ll)len1 * (len1 - 1) / 2;
        ans += (ll)len2 * (len2 + 1) / 2;
      } else {
        int mn = min(len1, len2);
        int mx = max(len1, len2);
        ans += (ll)mn * (mn - 1) / 2;
        ans += (ll)mx * (mx + 1) / 2;
      }
      i = k;
    } else {
      int j = i;
      while (s[j] == '>' && j < sz(s)) {
        j++;
      }
      int len = j - i;
      ans += (ll)len * (len + 1) / 2;
      i = j;
    }
  }
  cout << ans << "\n";
  return 0;
}