Problem p02919 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <fstream>
#include <iostream>

#include "bits/stdc++.h"

#define REP(i, x, y) for (int i = (x); i < (y); i++)
#define RREP(i, x, y) for (int i = (y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
// #define int long long

using namespace std;

// conversion
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

#define dump(x) cout << #x << " = " << (x) << endl
#define debug(x)                                                               \
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl

typedef long long ll;

const ll MOD = 1e9 + 7;

class ESecondSum {
public:
  void solve(std::istream &in, std::ostream &out) {
    int N;
    in >> N;
    vector<int> P(N);
    REP(i, 0, N) in >> P[i];

    vector<pair<int, int>> pIndex(N);
    REP(i, 0, N) pIndex[i] = {P[i], i};
    sort(all(pIndex), greater<pair<int, int>>());

    ll answer = 0;
    set<int> processed;
    REP(i, 0, N) {
      const int target = pIndex[i].first;
      const int targetIndex = pIndex[i].second;
      const auto itrRight = upper_bound(all(processed), targetIndex);
      const auto itrLeft =
          itrRight != processed.begin() && processed.size() != 0
              ? prev(itrRight)
              : processed.end();
      if (itrRight != processed.end()) {
        const auto itrRight2 = next(itrRight);
        ll left = itrLeft != processed.end() ? targetIndex - *itrLeft
                                             : targetIndex + 1;
        ll right = itrRight2 != processed.end() ? *itrRight2 - *itrRight
                                                : N - *itrRight;
        answer += left * right * target;
      }
      if (itrLeft != processed.end()) {
        const auto itrLeft2 =
            itrLeft != processed.begin() && processed.size() != 0
                ? prev(itrLeft)
                : processed.end();
        ll left =
            itrLeft2 != processed.end() ? *itrLeft - *itrLeft2 : (*itrLeft) + 1;
        ll right = itrRight != processed.end() ? *itrRight - targetIndex
                                               : N - targetIndex;
        answer += left * right * target;
      }
      processed.insert(targetIndex);
    }
    out << answer << "\n";
  }
};

int main() {
  ESecondSum solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Fixed submission:
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <fstream>
#include <iostream>

#include "bits/stdc++.h"

#define REP(i, x, y) for (int i = (x); i < (y); i++)
#define RREP(i, x, y) for (int i = (y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
// #define int long long

using namespace std;

// conversion
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

#define dump(x) cout << #x << " = " << (x) << endl
#define debug(x)                                                               \
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl

typedef long long ll;

const ll MOD = 1e9 + 7;

class ESecondSum {
public:
  void solve(std::istream &in, std::ostream &out) {
    int N;
    in >> N;
    vector<int> P(N);
    REP(i, 0, N) in >> P[i];

    vector<pair<int, int>> pIndex(N);
    REP(i, 0, N) pIndex[i] = {P[i], i};
    sort(all(pIndex), greater<pair<int, int>>());

    ll answer = 0;
    set<int> processed;
    REP(i, 0, N) {
      const int target = pIndex[i].first;
      const int targetIndex = pIndex[i].second;
      const auto itrRight = processed.upper_bound(targetIndex);
      const auto itrLeft =
          itrRight != processed.begin() && processed.size() != 0
              ? prev(itrRight)
              : processed.end();
      if (itrRight != processed.end()) {
        const auto itrRight2 = next(itrRight);
        ll left = itrLeft != processed.end() ? targetIndex - *itrLeft
                                             : targetIndex + 1;
        ll right = itrRight2 != processed.end() ? *itrRight2 - *itrRight
                                                : N - *itrRight;
        answer += left * right * target;
      }
      if (itrLeft != processed.end()) {
        const auto itrLeft2 =
            itrLeft != processed.begin() && processed.size() != 0
                ? prev(itrLeft)
                : processed.end();
        ll left =
            itrLeft2 != processed.end() ? *itrLeft - *itrLeft2 : (*itrLeft) + 1;
        ll right = itrRight != processed.end() ? *itrRight - targetIndex
                                               : N - targetIndex;
        answer += left * right * target;
      }
      processed.insert(targetIndex);
    }
    out << answer << "\n";
  }
};

int main() {
  ESecondSum solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}