Problem p02919 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;

class Timer {
  std::chrono::high_resolution_clock::time_point m_start;

public:
  Timer() : m_start(std::chrono::high_resolution_clock::now()) {}

  ~Timer() {
    auto end = std::chrono::high_resolution_clock::now();
    auto duration_s = std::chrono::duration<double>(end - m_start).count();
    cerr << "Completed in " << duration_s << endl;
  }
};

template <class c> struct rge {
  c b, e;
};
template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; }
template <class c> auto dud(c *x) -> decltype(cerr << *x, 0);
template <class c> char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  template <class c>
  typename enable_if<sizeof dud<c>(0) != 1, debug &>::type operator<<(c i) {
    cerr << boolalpha << i;
    return *this;
  }
  template <class c>
  typename enable_if<sizeof dud<c>(0) == 1, debug &>::type operator<<(c i) {
    return *this << range(begin(i), end(i));
  }
  template <class c, class b> debug &operator<<(pair<b, c> d) {
    return *this << "(" << d.first << ", " << d.second << ")";
  }
  template <class c> debug &operator<<(rge<c> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
      *this << ", " + 2 * (it == d.b) << *it;
    return *this << "]";
  }
#else
  template <class c> debug &operator<<(const c &) { return *this; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

using ll = long long;

const int INF = 1e9 + 5;

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

#include <iostream>

void solve() {
  ll n;
  cin >> n;
  vector<int> p(n);
  unordered_map<int, int> mp;

  for (int i = 0; i < n; ++i) {
    cin >> p[i];
  }

  ll sum = 0;

  if (/*is_sorted(p.begin(), p.end()) || */ is_sorted(p.rbegin(), p.rend())) {
    cout << n * (n - 1) * (2 * n - 1) / 6 << endl;
    return;
  }

  Timer timer;
  for (int i = 0; i < n; ++i) {
    ll a = 0, b = 0, c = 0, d = 0;
    bool ar = false, dr = false;
    int counter = i;
    while (--counter >= 0) {
      if (p[counter] > p[i]) {
        ar = true;
        break;
      }
      ++a;
    }
    while (--counter >= 0) {
      if (p[counter] > p[i]) {
        break;
      }
      ++b;
    }
    counter = i;
    while (++counter < n) {
      if (p[counter] > p[i]) {
        dr = true;
        break;
      }
      ++c;
    }
    while (++counter < n) {
      if (p[counter] > p[i]) {
        break;
      }
      ++d;
    }
    if (ar) {
      sum += (b + 1) * (c + 1) * ll(p[i]);
    }
    if (dr) {
      sum += (a + 1) * (d + 1) * ll(p[i]);
    }
  }

  cout << sum << endl;
}

int main() {
  FAST int t = 1;
  // cin >> t;

  for (int i = 0; i < t; ++i) {
    solve();
  }
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;

class Timer {
  std::chrono::high_resolution_clock::time_point m_start;

public:
  Timer() : m_start(std::chrono::high_resolution_clock::now()) {}

  ~Timer() {
    auto end = std::chrono::high_resolution_clock::now();
    auto duration_s = std::chrono::duration<double>(end - m_start).count();
    cerr << "Completed in " << duration_s << endl;
  }
};

template <class c> struct rge {
  c b, e;
};
template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; }
template <class c> auto dud(c *x) -> decltype(cerr << *x, 0);
template <class c> char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  template <class c>
  typename enable_if<sizeof dud<c>(0) != 1, debug &>::type operator<<(c i) {
    cerr << boolalpha << i;
    return *this;
  }
  template <class c>
  typename enable_if<sizeof dud<c>(0) == 1, debug &>::type operator<<(c i) {
    return *this << range(begin(i), end(i));
  }
  template <class c, class b> debug &operator<<(pair<b, c> d) {
    return *this << "(" << d.first << ", " << d.second << ")";
  }
  template <class c> debug &operator<<(rge<c> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
      *this << ", " + 2 * (it == d.b) << *it;
    return *this << "]";
  }
#else
  template <class c> debug &operator<<(const c &) { return *this; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

using ll = long long;

const int INF = 1e9 + 5;

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

#include <iostream>

void solve() {
  ll n;
  cin >> n;
  vector<int> p(n);
  unordered_map<int, int> mp;

  for (int i = 0; i < n; ++i) {
    cin >> p[i];
  }

  ll sum = 0;

  if (is_sorted(p.begin(), p.end()) || is_sorted(p.rbegin(), p.rend())) {
    cout << n * (n - 1) * (2 * n - 1) / 6 << endl;
    return;
  }

  Timer timer;
  for (int i = 0; i < n; ++i) {
    ll a = 0, b = 0, c = 0, d = 0;
    bool ar = false, dr = false;
    int counter = i;
    while (--counter >= 0) {
      if (p[counter] > p[i]) {
        ar = true;
        break;
      }
      ++a;
    }
    while (--counter >= 0) {
      if (p[counter] > p[i]) {
        break;
      }
      ++b;
    }
    counter = i;
    while (++counter < n) {
      if (p[counter] > p[i]) {
        dr = true;
        break;
      }
      ++c;
    }
    while (++counter < n) {
      if (p[counter] > p[i]) {
        break;
      }
      ++d;
    }
    if (ar) {
      sum += (b + 1) * (c + 1) * ll(p[i]);
    }
    if (dr) {
      sum += (a + 1) * (d + 1) * ll(p[i]);
    }
  }

  cout << sum << endl;
}

int main() {
  FAST int t = 1;
  // cin >> t;

  for (int i = 0; i < t; ++i) {
    solve();
  }
}