prithivMLmods/Coder-Stat huggingface dataset:
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
#include <algorithm>
#include <set>
#include <vector>
#define mod 998244353
#define int long long int
int32_t main() {
  int n;
  cin >> n;
  int j, arr[n + 1];
  for (j = 1; j <= n; j++)
    cin >> arr[j];
  int answer = 0;
  int position[n + 1];
  for (j = 1; j <= n; j++) {
    position[arr[j]] = j;
  }
  set<int> s;
  s.insert(0);
  s.insert(n + 1);
  for (j = 1; j <= n; j++) {
    int x = position[j];
    auto it = upper_bound(s.begin(), s.end(), x);
    int right = *it;
    right--;
    it--;
    int left = *it;
    left++;
    answer += j * (x - left + 1) * (right - x + 1);
    s.insert(x);
  }
  cout << answer << endl;
}


Fixed submission:
#include <iostream>
using namespace std;
#include <algorithm>
#include <set>
#include <vector>
#define mod 998244353
#define int long long int
int32_t main() {
  int n;
  cin >> n;
  int j, arr[n + 1];
  for (j = 1; j <= n; j++)
    cin >> arr[j];
  int answer = 0;
  int position[n + 1];
  for (j = 1; j <= n; j++) {
    position[arr[j]] = j;
  }
  set<int> s;
  s.insert(0);
  s.insert(n + 1);
  for (j = 1; j <= n; j++) {
    int x = position[j];
    auto it = s.upper_bound(x);
    int right = *it;
    right--;
    it--;
    int left = *it;
    left++;
    answer += j * (x - left + 1) * (right - x + 1);
    s.insert(x);
  }
  cout << answer << endl;
}
Problem p03987 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e9;
const ll MOD = 1e9 + 7;

#define REP(i, n) for (int i = 0; i < n; i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define REP2(i, n, k) for (int i = 0; i < n; i += k)

int main() {
  ll n, a[20005], b[20005], ans = 0;
  cin >> n;
  REP(i, n) {
    cin >> a[i];
    b[a[i]] = i;
  }

  set<int> st;
  st.insert(-1), st.insert(n);
  for (int i = 1; i <= n; i++) {
    auto r = st.upper_bound(b[i]);
    auto l = r;
    l--;
    // cout << *l << " " << *r << ' ' << b[i] << endl;
    st.insert(b[i]);
    ans += 1LL * (*r - b[i]) * (b[i] - *l) * i;
    // cout << ans << endl;
  }

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e9;
const ll MOD = 1e9 + 7;

#define REP(i, n) for (int i = 0; i < n; i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define REP2(i, n, k) for (int i = 0; i < n; i += k)

int main() {
  ll n, a[200005], b[200005], ans = 0;
  cin >> n;
  REP(i, n) {
    cin >> a[i];
    b[a[i]] = i;
  }

  set<int> st;
  st.insert(-1), st.insert(n);
  for (int i = 1; i <= n; i++) {
    auto r = st.upper_bound(b[i]);
    auto l = r;
    l--;
    // cout << *l << " " << *r << ' ' << b[i] << endl;
    st.insert(b[i]);
    ans += 1LL * (*r - b[i]) * (b[i] - *l) * i;
    // cout << ans << endl;
  }

  cout << ans << endl;
}
Problem p03987 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 10;

#define LD (t << 1)
#define RD (t << 1 | 1)

ll sum[N << 2], tag[N << 2];

void pushup(int t) { sum[t] = sum[LD] + sum[RD]; }

void pushnow(int t, int l, int r, int vl) {
  sum[t] = 1ll * (r - l + 1) * vl;
  tag[t] = vl;
}

void pushdown(int t, int l, int r) {
  if (tag[t]) {
    int mid = (l + r) >> 1;
    pushnow(LD, l, mid, tag[t]);
    pushnow(RD, mid + 1, r, tag[t]);
    tag[t] = 0;
  }
}

void modify(int t, int l, int r, int ql, int qr, int vl) {
  if (ql > qr)
    return;
  if (ql <= l && r <= qr) {
    pushnow(t, l, r, vl);
    return;
  }
  int mid = (l + r) >> 1;
  if (ql <= mid) {
    modify(LD, l, mid, ql, qr, vl);
  }
  if (qr > mid) {
    modify(RD, mid + 1, r, ql, qr, vl);
  }
  pushup(t);
}

int n, a[N], st[N], tp;

int main() {
  scanf("%d", &n);
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    while (tp && a[st[tp]] >= a[i]) {
      tp--;
    }
    modify(1, 1, n, st[tp] + 1, i, a[i]);
    ans += sum[1];
    st[++tp] = i;
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e6 + 10;

#define LD (t << 1)
#define RD (t << 1 | 1)

ll sum[N << 2], tag[N << 2];

void pushup(int t) { sum[t] = sum[LD] + sum[RD]; }

void pushnow(int t, int l, int r, int vl) {
  sum[t] = 1ll * (r - l + 1) * vl;
  tag[t] = vl;
}

void pushdown(int t, int l, int r) {
  if (tag[t]) {
    int mid = (l + r) >> 1;
    pushnow(LD, l, mid, tag[t]);
    pushnow(RD, mid + 1, r, tag[t]);
    tag[t] = 0;
  }
}

void modify(int t, int l, int r, int ql, int qr, int vl) {
  if (ql > qr)
    return;
  if (ql <= l && r <= qr) {
    pushnow(t, l, r, vl);
    return;
  }
  int mid = (l + r) >> 1;
  if (ql <= mid) {
    modify(LD, l, mid, ql, qr, vl);
  }
  if (qr > mid) {
    modify(RD, mid + 1, r, ql, qr, vl);
  }
  pushup(t);
}

int n, a[N], st[N], tp;

int main() {
  scanf("%d", &n);
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    while (tp && a[st[tp]] >= a[i]) {
      tp--;
    }
    modify(1, 1, n, st[tp] + 1, i, a[i]);
    ans += sum[1];
    st[++tp] = i;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 5e15;

int main() {
  ll n;
  cin >> n;
  vec a(n + 1);
  for (ll i = 1; i <= n; i++) {
    ll tmp;
    cin >> tmp;
    a[tmp] = i;
  }
  set<ll> pre;
  pre.insert(0);
  pre.insert(n + 1);
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    auto it = upper_bound(pre.begin(), pre.end(), a[i]);
    ans += i * (*it - a[i]) * (a[i] - *(--it));
    pre.insert(a[i]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 5e15;

int main() {
  ll n;
  cin >> n;
  vec a(n + 1);
  for (ll i = 1; i <= n; i++) {
    ll tmp;
    cin >> tmp;
    a[tmp] = i;
  }
  set<ll> pre;
  pre.insert(0);
  pre.insert(n + 1);
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    auto it = pre.upper_bound(a[i]);
    ans += i * (*it - a[i]) * (a[i] - *(--it));
    pre.insert(a[i]);
  }
  cout << ans << endl;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main() {
  int n;
  cin >> n;
  vector<int> a(n), l(n), r(n);
  for (auto &&w : a)
    cin >> w;
  for (int i = 0; i < n; i++) {
    l[i] = i;
    while (l[i] > 0 && a[l[i] - 1] > a[i]) {
      l[i] = l[l[i] - 1];
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    r[i] = i;
    while (r[i] < n - 1 && a[r[i] + 1] > a[i]) {
      r[i] = r[l[i] + 1];
    }
  }
  int ans = 0ll;
  for (int i = 0; i < n; i++) {
    ans += a[i] * (i - l[i] + 1) * (r[i] - i + 1);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main() {
  int n;
  cin >> n;
  vector<int> a(n), l(n), r(n);
  for (auto &&w : a)
    cin >> w;
  for (int i = 0; i < n; i++) {
    l[i] = i;
    while (l[i] > 0 && a[l[i] - 1] > a[i]) {
      l[i] = l[l[i] - 1];
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    r[i] = i;
    while (r[i] < n - 1 && a[r[i] + 1] > a[i]) {
      r[i] = r[r[i] + 1];
    }
  }
  int ans = 0ll;
  for (int i = 0; i < n; i++) {
    ans += a[i] * (i - l[i] + 1) * (r[i] - i + 1);
  }
  cout << ans << endl;
}
Problem p03987 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int DIM = 1e1 + 5;

vector<int> stk;
int arr[DIM], lef[DIM], rig[DIM];

int main(void) {

  int n;
  cin >> n;

  for (int i = 1; i <= n; i++)
    cin >> arr[i];

  stk.assign(1, 0);
  for (int i = 1; i <= n; i++) {
    while (arr[i] <= arr[stk.back()])
      stk.pop_back();

    lef[i] = stk.back() + 1;
    stk.push_back(i);
  }

  stk.assign(1, n + 1);
  for (int i = n; i >= 1; i--) {
    while (arr[i] <= arr[stk.back()])
      stk.pop_back();

    rig[i] = stk.back() - 1;
    stk.push_back(i);
  }

  long long ans = 0;
  for (int i = 1; i <= n; i++)
    ans += 1LL * arr[i] * (i - lef[i] + 1) * (rig[i] - i + 1);

  cout << ans << endl;
  return 0;
}

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

const int DIM = 2e5 + 5;

vector<int> stk;
int arr[DIM], lef[DIM], rig[DIM];

int main(void) {

  int n;
  cin >> n;

  for (int i = 1; i <= n; i++)
    cin >> arr[i];

  stk.assign(1, 0);
  for (int i = 1; i <= n; i++) {
    while (arr[i] <= arr[stk.back()])
      stk.pop_back();

    lef[i] = stk.back() + 1;
    stk.push_back(i);
  }

  stk.assign(1, n + 1);
  for (int i = n; i >= 1; i--) {
    while (arr[i] <= arr[stk.back()])
      stk.pop_back();

    rig[i] = stk.back() - 1;
    stk.push_back(i);
  }

  long long ans = 0;
  for (int i = 1; i <= n; i++)
    ans += 1LL * arr[i] * (i - lef[i] + 1) * (rig[i] - i + 1);

  cout << ans << endl;
  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define ull unsigned long long
#define F first
#define S second
#define eps 1e-6
#define mp make_pair
const long long INF = 1e18 + 2;
const int MX = 1002;
int N, M, k;
vector<char> v;
int par[MX];
ll sz[MX];
int f(int x) {
  if (x == par[x])
    return x;
  return par[x] = f(par[x]);
}
void mer(int a, int b) {
  a = f(a);
  b = f(b);
  if (a == b)
    return;
  par[b] = a;
  sz[a] += sz[b];
}
pair<int, int> arr[MX];
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &arr[i].F);
    arr[i].S = i;
    arr[i].F = -arr[i].F;
    par[i] = i;
    sz[i] = 0;
  }
  ll an = 0;
  sort(arr + 1, arr + 1 + N);
  for (int i = 1; i <= N; i++) {
    int j = arr[i].S;
    arr[i].F *= -1;
    int L = 1, R = 1;
    if (j > 1 && sz[f(j - 1)])
      L = sz[f(j - 1)] + 1;
    if (j < N && sz[f(j + 1)])
      R = sz[f(j + 1)] + 1;
    an += (ll)arr[i].F * L * R;
    //        cout<<L<<" "<<R<<"\n";
    sz[j] = 1;
    if (j > 1 && sz[f(j - 1)])
      mer(j, j - 1);
    if (j < N && sz[f(j + 1)])
      mer(j, j + 1);
  }
  cout << an;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define ull unsigned long long
#define F first
#define S second
#define eps 1e-6
#define mp make_pair
const long long INF = 1e18 + 2;
const int MX = 200002;
int N, M, k;
vector<char> v;
int par[MX];
ll sz[MX];
int f(int x) {
  if (x == par[x])
    return x;
  return par[x] = f(par[x]);
}
void mer(int a, int b) {
  a = f(a);
  b = f(b);
  if (a == b)
    return;
  par[b] = a;
  sz[a] += sz[b];
}
pair<int, int> arr[MX];
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &arr[i].F);
    arr[i].S = i;
    arr[i].F = -arr[i].F;
    par[i] = i;
    sz[i] = 0;
  }
  ll an = 0;
  sort(arr + 1, arr + 1 + N);
  for (int i = 1; i <= N; i++) {
    int j = arr[i].S;
    arr[i].F *= -1;
    int L = 1, R = 1;
    if (j > 1 && sz[f(j - 1)])
      L = sz[f(j - 1)] + 1;
    if (j < N && sz[f(j + 1)])
      R = sz[f(j + 1)] + 1;
    an += (ll)arr[i].F * L * R;
    //        cout<<L<<" "<<R<<"\n";
    sz[j] = 1;
    if (j > 1 && sz[f(j - 1)])
      mer(j, j - 1);
    if (j < N && sz[f(j + 1)])
      mer(j, j + 1);
  }
  cout << an;
}
Problem p03987 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
const int M = 2e5 + 5;
int n, a[M], lft[M], rght[M];

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  memset(lft, -1, sizeof(lft));
  memset(rght, -1, sizeof(rght));
  stack<int> s;
  for (int i = 1; i <= n; i++) {
    while (!s.empty() && a[i] < a[s.top()]) {
      rght[s.top()] = i - 1;
      s.pop();
    }
    s.push(i);
  }
  while (!s.empty()) {
    rght[s.top()] = n;
    s.pop();
  }
  for (int i = n; i >= 1; i--) {
    while (!s.empty() && a[i] < a[s.top()]) {
      lft[s.top()] = i + 1;
      s.pop();
    }
    s.push(i);
  }
  while (!s.empty()) {
    lft[s.top()] = 1;
    s.pop();
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans += (i - lft[i] + 1) * (rght[i] - i + 1) * a[i];
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
const int M = 2e5 + 5;
int n, a[M], lft[M], rght[M];

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  memset(lft, -1, sizeof(lft));
  memset(rght, -1, sizeof(rght));
  stack<int> s;
  for (int i = 1; i <= n; i++) {
    while (!s.empty() && a[i] < a[s.top()]) {
      rght[s.top()] = i - 1;
      s.pop();
    }
    s.push(i);
  }
  while (!s.empty()) {
    rght[s.top()] = n;
    s.pop();
  }
  for (int i = n; i >= 1; i--) {
    while (!s.empty() && a[i] < a[s.top()]) {
      lft[s.top()] = i + 1;
      s.pop();
    }
    s.push(i);
  }
  while (!s.empty()) {
    lft[s.top()] = 1;
    s.pop();
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans += (i - lft[i] + 1) * (rght[i] - i + 1) * a[i];
  cout << ans;
  return 0;
}
Problem p03987 (C++)

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

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define FOR(k, m, n) for (ll(k) = (m); (k) < (n); (k)++)
#define REP(i, n) FOR((i), 0, (n))
#define WAITING(str)                                                           \
  int str;                                                                     \
  std::cin >> str;
#define DEBUGING(str) cout << #str << " " str << endl

constexpr int INF = (1 << 30);
constexpr ll INFL = (1ll << 60);
constexpr ll MOD = 1000000007; // 10^9+7

int main() {
  ll N;
  cin >> N;
  vector<ll> a(N);
  REP(i, N) cin >> a[i];

  priority_queue<pll, vector<pll>, greater<pll>> pq;
  REP(i, N) pq.push({a[i], i});

  ll res = 0;
  set<ll> st;
  st.insert(-1);
  st.insert(N);
  while (!pq.empty()) {
    auto p = pq.top();
    pq.pop();
    ll times = p.first;
    ll now = p.second;

    auto itr = lower_bound(st.begin(), st.end(), now);
    ll aft = *itr - now;
    --itr;
    ll bef = now - *itr;

    res += bef * aft * times;
    st.insert(now);
  }
  cout << res << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define FOR(k, m, n) for (ll(k) = (m); (k) < (n); (k)++)
#define REP(i, n) FOR((i), 0, (n))
#define WAITING(str)                                                           \
  int str;                                                                     \
  std::cin >> str;
#define DEBUGING(str) cout << #str << " " str << endl

constexpr int INF = (1 << 30);
constexpr ll INFL = (1ll << 60);
constexpr ll MOD = 1000000007; // 10^9+7

int main() {
  ll N;
  cin >> N;
  vector<ll> a(N);
  REP(i, N) cin >> a[i];

  priority_queue<pll, vector<pll>, greater<pll>> pq;
  REP(i, N) pq.push({a[i], i});

  ll res = 0;
  set<ll> st;
  st.insert(-1);
  st.insert(N);
  while (!pq.empty()) {
    auto p = pq.top();
    pq.pop();
    ll times = p.first;
    ll now = p.second;

    auto itr = st.lower_bound(now);
    ll aft = *itr - now;
    --itr;
    ll bef = now - *itr;

    res += bef * aft * times;
    st.insert(now);
  }
  cout << res << endl;
  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ctype.h>
#include <stdio.h>
#define y second
#define x first
#define lsb(x) (x & -x)
#define pb push_back
#define l(x) x << 1
#define r(x) (x << 1) | 1
#define non(x) ((x) > N ? (x)-N : (x) + N)
#define y second
const int MOD = 1000000007; // LOL
const int MAXM = 210 * 210;
const int NMAX = 6100;
const int HP = 73;
const int SQRT = 350;
using namespace std;
const double eps = 1e-5;
const double PI = acos(-1.0);
typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<LD, LD> PLD;
typedef vector<int> VI;
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, 1, -1, 0};

int A[200666];
PII sortme[200666];
LL rs;
int n;
set<int> S1, S2;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  // freopen("input.in", "rt", stdin);
  // freopen("output.out", "wt", stdout);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> A[i];
    sortme[i] = {A[i], i};
  }
  sort(sortme + 1, sortme + 1 + n);
  S1.insert(0);
  S1.insert(n + 1);
  S2.insert(0);
  S2.insert(-n - 1);
  for (int i = 1; i <= n; ++i) {
    int poz = sortme[i].y;
    int l = -*(upper_bound(S2.begin(), S2.end(), -poz));
    int r = *(upper_bound(S1.begin(), S1.end(), poz));
    rs += 1LL *
          (1LL * (poz - l - 1) * (r - poz - 1) + 1 + (r - poz - 1) +
           (poz - l - 1)) *
          i;
    // cout<<l<<" "<<r<<" "<<poz<<" "<<rs<<"\n";
    S2.insert(-poz);
    S1.insert(poz);
  }
  cout << rs;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ctype.h>
#include <stdio.h>
#define y second
#define x first
#define lsb(x) (x & -x)
#define pb push_back
#define l(x) x << 1
#define r(x) (x << 1) | 1
#define non(x) ((x) > N ? (x)-N : (x) + N)
#define y second
const int MOD = 1000000007; // LOL
const int MAXM = 210 * 210;
const int NMAX = 6100;
const int HP = 73;
const int SQRT = 350;
using namespace std;
const double eps = 1e-5;
const double PI = acos(-1.0);
typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<LD, LD> PLD;
typedef vector<int> VI;
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, 1, -1, 0};

int A[200666];
PII sortme[200666];
LL rs;
int n;
set<int> S1, S2;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  // freopen("input.in", "rt", stdin);
  // freopen("output.out", "wt", stdout);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> A[i];
    sortme[i] = {A[i], i};
  }
  sort(sortme + 1, sortme + 1 + n);
  S1.insert(0);
  S1.insert(n + 1);
  S2.insert(0);
  S2.insert(-n - 1);
  for (int i = 1; i <= n; ++i) {
    int poz = sortme[i].y;
    int l = -*S2.upper_bound(-poz);
    int r = *S1.upper_bound(poz);
    rs += 1LL *
          (1LL * (poz - l - 1) * (r - poz - 1) + 1 + (r - poz - 1) +
           (poz - l - 1)) *
          i;
    // cout<<l<<" "<<r<<" "<<poz<<" "<<rs<<"\n";
    S2.insert(-poz);
    S1.insert(poz);
  }
  cout << rs;
  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef long int li;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef pair<ll, ll> P;
#define PI 3.14159265358979
#define MOD 998244353
const int MAX = 510000;

int main() {
  ll N;
  cin >> N;
  vector<ll> a(N);
  vector<ll> b(N);
  rep(i, N) {
    cin >> a[i];
    b[a[i] - 1ll] = i;
  }
  set<ll> s;
  s.insert(-1ll);
  s.insert(N);
  ll ans = 0;
  rep(i, N) {
    s.insert(b[i]);
    auto t = lower_bound(s.begin(), s.end(), b[i]);
    t--;
    auto mi = *t;
    t++;
    t++;
    auto ma = *t;
    ans += (i + 1ll) * (ma - b[i]) * (b[i] - mi);
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef long int li;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef pair<ll, ll> P;
#define PI 3.14159265358979
#define MOD 998244353
const int MAX = 510000;

int main() {
  ll N;
  cin >> N;
  vector<ll> a(N);
  vector<ll> b(N);
  rep(i, N) {
    cin >> a[i];
    b[a[i] - 1ll] = i;
  }
  set<ll> s;
  s.insert(-1ll);
  s.insert(N);
  ll ans = 0;
  rep(i, N) {
    s.insert(b[i]);
    auto t = s.lower_bound(b[i]);
    t--;
    auto mi = *t;
    t++;
    t++;
    auto ma = *t;
    ans += (i + 1ll) * (ma - b[i]) * (b[i] - mi);
  }
  cout << ans;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int main() {
  int n;
  cin >> n;
  set<int> se;
  se.insert(-1);
  se.insert(n);
  vi in(n);
  rep(i, n) cin >> in[i];
  vi c(n);
  rep(i, n) c[in[i] - 1] = i;
  ll out = 0;
  rep(i, n) {
    set<int>::iterator it = lower_bound(all(se), c[i]);
    ll a = *it - c[i];
    it--;
    ll b = c[i] - *it;
    out += a * b * in[c[i]];
    se.insert(c[i]);
  }
  cout << out << endl;
}
/*int main(){
        int n,m,s;
        while(cin>>n>>m>>s,n+m+s){
                vvi in(n,vi(3));
                rep(i,n)rep(j,3)cin>>in[i][j];
                vvp area(n);
                rep(i,n)rep(j,n){

                }
        }
}//*/


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int main() {
  int n;
  cin >> n;
  set<int> se;
  se.insert(-1);
  se.insert(n);
  vi in(n);
  rep(i, n) cin >> in[i];
  vi c(n);
  rep(i, n) c[in[i] - 1] = i;
  ll out = 0;
  rep(i, n) {
    set<int>::iterator it = se.lower_bound(c[i]);
    ll a = *it - c[i];
    it--;
    ll b = c[i] - *it;
    out += a * b * in[c[i]];
    se.insert(c[i]);
  }
  cout << out << endl;
}
/*int main(){
        int n,m,s;
        while(cin>>n>>m>>s,n+m+s){
                vvi in(n,vi(3));
                rep(i,n)rep(j,3)cin>>in[i][j];
                vvp area(n);
                rep(i,n)rep(j,n){

                }
        }
}//*/
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "algorithm"
#include "bitset"
#include "cmath"
#include "functional"
#include "iostream"
#include "map"
#include "queue"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "vector"

#define lp(n) for (int i = 0; i < n; i++)
#define LP(n, i) for (int i = 0; i < n; i++)
#define mod 1000000007
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define nyan "(=^･ω･^=)"
typedef long long ll;
using namespace std;

ll n, a[200010], mem, ans;
set<ll> st;

int main() {
  cin >> n;
  lp(n) {
    cin >> mem;
    a[mem - 1] = i;
  }
  ans = (a[0] + 1) * (n - a[0]);
  st.insert(a[0]);
  lp(n - 1) {
    auto it = st.end();
    it--;
    if (a[i + 1] < *st.begin()) {
      ans += (a[i + 1] + 1) * (*st.begin() - a[i + 1]) * (i + 2);
    } else if (*it < a[i + 1]) {
      ans += (a[i + 1] - *it) * (n - a[i + 1]) * (i + 2);
    } else {
      auto it2 = lower_bound(st.begin(), st.end(), a[i + 1]);
      auto it3 = it2;
      it2--;
      ans += (a[i + 1] - *it2) * (*it3 - a[i + 1]) * (i + 2);
    }
    st.insert(a[i + 1]);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include "algorithm"
#include "bitset"
#include "cmath"
#include "functional"
#include "iostream"
#include "map"
#include "queue"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "vector"

#define lp(n) for (int i = 0; i < n; i++)
#define LP(n, i) for (int i = 0; i < n; i++)
#define mod 1000000007
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define nyan "(=^･ω･^=)"
typedef long long ll;
using namespace std;

ll n, a[200010], mem, ans;
set<ll> st;

int main() {
  cin >> n;
  lp(n) {
    cin >> mem;
    a[mem - 1] = i;
  }
  ans = (a[0] + 1) * (n - a[0]);
  st.insert(a[0]);
  lp(n - 1) {
    auto it = st.end();
    it--;
    if (a[i + 1] < *st.begin()) {
      ans += (a[i + 1] + 1) * (*st.begin() - a[i + 1]) * (i + 2);
    } else if (*it < a[i + 1]) {
      ans += (a[i + 1] - *it) * (n - a[i + 1]) * (i + 2);
    } else {
      auto it2 = st.lower_bound(a[i + 1]);
      auto it3 = it2;
      it2--;
      ans += (a[i + 1] - *it2) * (*it3 - a[i + 1]) * (i + 2);
    }
    st.insert(a[i + 1]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

typedef long long LL;
typedef pair<LL, LL> PLL;
typedef vector<LL> VLL;
#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPN(i, a, n) FOR((i), (a), (a) + (n))
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define PB push_back
#define MP make_pair
#define OUT(a) cout << (a) << endl;

// vector<LL> d(n+2,0),revd(n+2,0);
int r[200020] = {}, l[200020] = {}, n;
void dp(int a[], vector<LL> &d) {
  REPN(i, 1, n) {
    for (int j = i - 1; j >= 0; --j) {
      if (d[j] < d[i]) {
        a[i] = j;
        break;
      }
      if (a[j] != 0) {
        j = a[j] + 1;
        continue;
      }
    }
  }
  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  LL ans = 0;
  // int n;
  cin >> n;
  vector<LL> d(n + 2, 0), revd(n + 2, 0);
  // int r[200020]={},l[200020]={};
  REP(i, n) {
    cin >> d[i + 1];
    revd[n - i] = d[i + 1];
  }

  dp(l, d);
  dp(r, revd);

  REPN(i, 1, n) { ans += d[i] * (i - l[i]) * ((n + 1 - r[n + 1 - i]) - i); }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

typedef long long LL;
typedef pair<LL, LL> PLL;
typedef vector<LL> VLL;
#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPN(i, a, n) FOR((i), (a), (a) + (n))
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define PB push_back
#define MP make_pair
#define OUT(a) cout << (a) << endl;

// vector<LL> d(n+2,0),revd(n+2,0);
int r[200020] = {}, l[200020] = {}, n;
void dp(int a[], vector<LL> &d) {
  REPN(i, 1, n) {
    for (int j = i - 1; j >= 0; --j) {
      if (d[j] < d[i]) {
        a[i] = j;
        break;
      }
      j = a[j] + 1;
      continue;
    }
  }
  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  LL ans = 0;
  // int n;
  cin >> n;
  vector<LL> d(n + 2, 0), revd(n + 2, 0);
  // int r[200020]={},l[200020]={};
  REP(i, n) {
    cin >> d[i + 1];
    revd[n - i] = d[i + 1];
  }

  dp(l, d);
  dp(r, revd);

  REPN(i, 1, n) { ans += d[i] * (i - l[i]) * ((n + 1 - r[n + 1 - i]) - i); }
  cout << ans << endl;

  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

int p[100010], sz[100010];

int find(int x) { return x == p[x] ? x : p[x] = find(p[x]); }

void merge(int x, int y) {
  int px = find(x), py = find(y);
  if (px == py) {
    return;
  }
  p[px] = py;
  sz[py] += sz[px];
}

void MAIN() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    p[i] = i;
    sz[i] = 1;
  }
  std::vector<int> idx(n + 1), arr(n + 1);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &arr[i + 1]);
    idx[arr[i + 1]] = i + 1;
  }
  long long ans = 0;
  for (int i = n; i > 0; --i) {
    int l = 1, r = 1;
    if (idx[i] > 1 && arr[idx[i] - 1] > i) {
      l += sz[find(arr[idx[i] - 1])];
      merge(arr[idx[i] - 1], i);
    }
    if (idx[i] < n && arr[idx[i] + 1] > i) {
      r += sz[find(arr[idx[i] + 1])];
      merge(i, arr[idx[i] + 1]);
    }
    ans += l * 1ll * r * 1ll * i;
  }
  printf("%lld\n", ans);
}

int main() {
  MAIN();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

int p[200010], sz[200010];

int find(int x) { return x == p[x] ? x : p[x] = find(p[x]); }

void merge(int x, int y) {
  int px = find(x), py = find(y);
  if (px == py) {
    return;
  }
  p[px] = py;
  sz[py] += sz[px];
}

void MAIN() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    p[i] = i;
    sz[i] = 1;
  }
  std::vector<int> idx(n + 1), arr(n + 1);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &arr[i + 1]);
    idx[arr[i + 1]] = i + 1;
  }
  long long ans = 0;
  for (int i = n; i > 0; --i) {
    int l = 1, r = 1;
    if (idx[i] > 1 && arr[idx[i] - 1] > i) {
      l += sz[find(arr[idx[i] - 1])];
      merge(arr[idx[i] - 1], i);
    }
    if (idx[i] < n && arr[idx[i] + 1] > i) {
      r += sz[find(arr[idx[i] + 1])];
      merge(i, arr[idx[i] + 1]);
    }
    ans += l * 1ll * r * 1ll * i;
  }
  printf("%lld\n", ans);
}

int main() {
  MAIN();
  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define all(a) (a).begin(), (a).end()
#define ll long long

set<int> c;
int main() {
  int N;
  cin >> N;
  c.insert(-1);
  c.insert(N);
  int a[N], b[N];
  ll cnt = 0, cp;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    b[a[i] - 1] = i;
  }
  for (int i = 0; i < N; i++) {
    auto s = upper_bound(all(c), b[i]);
    cp = (*s - b[i]);
    s--;
    cp *= (b[i] - *s);
    cnt += (i + 1) * cp;
    c.insert(b[i]);
  }
  cout << cnt << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define all(a) (a).begin(), (a).end()
#define ll long long

set<int> c;
int main() {
  int N;
  cin >> N;
  c.insert(-1);
  c.insert(N);
  int a[N], b[N];
  ll cnt = 0, cp;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    b[a[i] - 1] = i;
  }
  for (int i = 0; i < N; i++) {
    auto s = c.upper_bound(b[i]);
    cp = (*s - b[i]);
    s--;
    cp *= (b[i] - *s);
    cnt += (i + 1) * cp;
    c.insert(b[i]);
  }
  cout << cnt << endl;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
// https://atcoder.jp/contests/agc005/tasks/agc005_b
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define FOR(i, a, b) for (int64_t i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
int main() {
  ll N;
  cin >> N;
  vector<ll> an(N);
  for (auto &&a : an) {
    cin >> a;
  }

  ll ans = 0;
  for (ll n = N - 1; n >= 0; --n) {
    ll tmp = INT64_MAX;
    for (ll i = n; i >= 0; --i) {
      tmp = min(an.at(i), tmp);
      ans += tmp;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
// https://atcoder.jp/contests/agc005/tasks/agc005_b
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define FOR(i, a, b) for (int64_t i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
int main() {
#if LOCAL & 01
  std::ifstream in("./test/sample-3.in"); // input.txt
  std::cin.rdbuf(in.rdbuf());
#endif
  ll N;
  cin >> N;
  vector<ll> an(N + 1);
  vector<ll> pos(N + 1); // aは1..Nを並べ替えたもの
  FOR(i, 1, N + 1) {
    ll a;
    cin >> a;
    an.at(i) = a;
    pos.at(a) = i; // 場所
  }

  ll ans = 0;
  set<ll> used{0, N + 1}; // placeがはいる
  // set
  // 追加はO(logN),値はsortされてはいるので、2分探索できる、sortされた順番でiteratorで取得する
  FOR(a, 1, N + 1) {                     // aを1から
    auto now_pos = pos[a];               // 今の場所
    auto it = used.lower_bound(now_pos); // 以上,upper_bound(より上)でもよい
    auto l = *it; // 数字が小さい順のplaceを入れていって、その左右をみていく
    auto r = *(--it);
    ans += a * (r - now_pos) * (now_pos - l);
    used.insert(pos[a]);
  }
  cout << ans << endl;
  return 0;
}

// tleする
int tle_main() {
  ll N;
  cin >> N;
  vector<ll> an(N);
  for (auto &&a : an) {
    cin >> a;
  }

  ll ans = 0;
  for (ll n = N - 1; n >= 0; --n) {
    ll tmp = INT64_MAX;
    for (ll i = n; i >= 0; --i) {
      tmp = min(an.at(i), tmp);
      ans += tmp;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
int d[200001];
int main(void) {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    d[a] = i;
  }
  ll ans = 0;
  set<ll> s;
  s.insert(-1), s.insert(n);
  for (int i = 0; i < n; i++) {
    auto cnt = lower_bound(s.begin(), s.end(), d[i + 1]);
    ll l = *cnt;
    cnt--;
    ll r = *cnt;
    ans += (i + 1) * (l - d[i + 1]) * (d[i + 1] - r);
    s.insert(d[i + 1]);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
int d[200001];
int main(void) {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    d[a] = i;
  }
  ll ans = 0;
  set<ll> s;
  s.insert(-1), s.insert(n);
  for (int i = 0; i < n; i++) {
    auto cnt = s.lower_bound(d[i + 1]);
    ll l = *cnt;
    cnt--;
    ll r = *cnt;
    ans += (i + 1) * (l - d[i + 1]) * (d[i + 1] - r);
    s.insert(d[i + 1]);
  }
  cout << ans << endl;
}
Problem p03987 (C++)

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define all(x) (x).begin(), (x).end()
#define foreach(u, v) for (auto(u) : (v))
#define pb push_back
#define mp make_pair
#define mt make_tuple

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;

const int inf = 1e9;
const ll linf = 1LL << 60;
const ll mod = 1e9 + 7;
const double eps = 1e-9;

/*
 */

int main() {
  int n;
  cin >> n;

  vi a(n), o(n);
  rep(i, n) {
    cin >> a[i];
    o[a[i] - 1] = i;
  }

  set<int> s;
  s.insert(-1);
  s.insert(n);

  ll ans = 0;
  rep(i, n) {
    auto itr = lower_bound(all(s), o[i]);
    ll r = *itr - o[i];
    itr--;
    ll l = o[i] - *itr;
    ans += a[o[i]] * l * r;
    s.insert(o[i]);
  }

  cout << ans << endl;

  return 0;
}


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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define all(x) (x).begin(), (x).end()
#define foreach(u, v) for (auto(u) : (v))
#define pb push_back
#define mp make_pair
#define mt make_tuple

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;

const int inf = 1e9;
const ll linf = 1LL << 60;
const ll mod = 1e9 + 7;
const double eps = 1e-9;

/*
 */

int main() {
  int n;
  cin >> n;

  vi a(n), o(n);
  rep(i, n) {
    cin >> a[i];
    o[a[i] - 1] = i;
  }

  set<int> s;
  s.insert(-1);
  s.insert(n);

  ll ans = 0;
  rep(i, n) {
    auto itr = s.lower_bound(o[i]);
    ll r = *itr - o[i];
    itr--;
    ll l = o[i] - *itr;
    ans += a[o[i]] * l * r;
    s.insert(o[i]);
  }

  cout << ans << endl;

  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long int LL;
typedef LL ll;
typedef string str;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef stringstream strs;

#define X first
#define Y second
#define PB push_back
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i >= b; i--)
#define smax(a, b) a = max(a, b)
#define smin(a, b) a = min(a, b)
#define SZ(a) ((ll)a.size())
#define ER(a) cout << #a << ' ' << a << endl
#define LB(a, n, x) (lower_bound(a, (a) + (n), x) - (a))
#define RET(x)                                                                 \
  {                                                                            \
    cout << x;                                                                 \
    exit(0);                                                                   \
  }
const ll M = 1e2 + 5, LG = 19, SM = 4000 + 5, inf = 1e18 + 1;
ll mod = 1e9 + 7;

ll n;
ll p[M], pv[M];
ll res = 0;

int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  set<ll> se;
  se.insert(-1);
  se.insert(n);
  For(i, 0, n) {
    cin >> p[i];
    p[i]--;
    pv[p[i]] = i;
  }
  For(i, 0, n) {
    auto ite = se.upper_bound(pv[i]);
    ll y = *ite;
    ll x = *(--ite);
    res += (y - pv[i]) * (pv[i] - x) * (i + 1);
    se.insert(pv[i]);
  }
  RET(res);
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long int LL;
typedef LL ll;
typedef string str;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef stringstream strs;

#define X first
#define Y second
#define PB push_back
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i >= b; i--)
#define smax(a, b) a = max(a, b)
#define smin(a, b) a = min(a, b)
#define SZ(a) ((ll)a.size())
#define ER(a) cout << #a << ' ' << a << endl
#define LB(a, n, x) (lower_bound(a, (a) + (n), x) - (a))
#define RET(x)                                                                 \
  {                                                                            \
    cout << x;                                                                 \
    exit(0);                                                                   \
  }
const ll M = 2e5 + 5, LG = 19, SM = 4000 + 5, inf = 1e18 + 1;
ll mod = 1e9 + 7;

ll n;
ll p[M], pv[M];
ll res = 0;

int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  set<ll> se;
  se.insert(-1);
  se.insert(n);
  For(i, 0, n) {
    cin >> p[i];
    p[i]--;
    pv[p[i]] = i;
  }
  For(i, 0, n) {
    auto ite = se.upper_bound(pv[i]);
    ll y = *ite;
    ll x = *(--ite);
    res += (y - pv[i]) * (pv[i] - x) * (i + 1);
    se.insert(pv[i]);
  }
  RET(res);
}
Problem p03987 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define int long long
int n, a[30001], l[30001], r[30001], deq[30001];

signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  deq[0] = 0;
  int top = 0;
  for (int i = 1; i <= n; i++) {
    while (top > 0 && a[deq[top]] >= a[i])
      top--;
    l[i] = deq[top] + 1;
    top++;
    deq[top] = i;
  }
  deq[0] = n + 1;
  top = 0;
  for (int i = n; i >= 1; i--) {
    while (top > 0 && a[deq[top]] >= a[i])
      top--;
    r[i] = deq[top] - 1;
    top++;
    deq[top] = i;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += (i - l[i] + 1) * (r[i] - i + 1) * a[i];
  }
  cout << ans;
}

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

#define int long long
int n, a[200001], l[200001], r[200001], deq[200001];

signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  deq[0] = 0;
  int top = 0;
  for (int i = 1; i <= n; i++) {
    while (top > 0 && a[deq[top]] >= a[i])
      top--;
    l[i] = deq[top] + 1;
    top++;
    deq[top] = i;
  }
  deq[0] = n + 1;
  top = 0;
  for (int i = n; i >= 1; i--) {
    while (top > 0 && a[deq[top]] >= a[i])
      top--;
    r[i] = deq[top] - 1;
    top++;
    deq[top] = i;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += (i - l[i] + 1) * (r[i] - i + 1) * a[i];
  }
  cout << ans;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
#include <utility>
#include <vector>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<pair<int, int>> A;
  int a;
  for (int i = 0; i < N; ++i) {
    cin >> a;
    A.push_back(make_pair(a, i));
  }
  sort(A.begin(), A.end());
  set<int> s;
  s.insert(-1);
  s.insert(N);
  long long int ans = 0LL;
  for (int i = 0; i < N; ++i) {
    int id = A[i].second;
    set<int>::iterator u = upper_bound(s.begin(), s.end(), id), l = u;
    --l;
    ans += (long long int)(*u - id) * (id - (*l)) * A[i].first;
    s.insert(id);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <utility>
#include <vector>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<pair<int, int>> A;
  int a;
  for (int i = 0; i < N; ++i) {
    cin >> a;
    A.push_back(make_pair(a, i));
  }
  sort(A.begin(), A.end());
  set<int> s;
  s.insert(-1);
  s.insert(N);
  long long int ans = 0LL;
  for (int i = 0; i < N; ++i) {
    int id = A[i].second;
    s.insert(id);
    set<int>::iterator u = s.find(id), l = u;
    ++u;
    --l;
    ans += (long long int)(*u - id) * (id - (*l)) * A[i].first;
    s.insert(id);
  }
  cout << ans << endl;
  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define ll long long

using namespace std;

const int N_MAX = 200002;
const int BITS = 18;

int n;

int p[N_MAX];

int rm[N_MAX][BITS];

int rmq(int l, int r) {
  int lg = r - l + 1;
  int b = 0;
  while ((1 << (b + 1)) <= lg)
    b++;
  return min(rm[l][b], rm[r - (1 << b) + 1][b]);
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> p[i];
  for (int i = 1; i <= n; i++)
    rm[i][0] = p[i];
  for (int b = 1; b < BITS; b++)
    for (int i = 1; i <= n; i++)
      rm[i][b] = min(rm[i][b - 1], rm[i + (1 << (b - 1))][b - 1]);
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    int posl, posr;
    int l, r;
    l = 1;
    r = i;
    while (l < r) {
      int mid = (l + r) / 2;
      if (rmq(mid, i) < p[i])
        l = mid + 1;
      else
        r = mid;
    }
    posl = l;
    l = i;
    r = n;
    while (l < r) {
      int mid = (l + r + 1) / 2;
      if (rmq(i, mid) < p[i])
        r = mid - 1;
      else
        l = mid;
    }
    posr = r;
    ans += 1LL * (i - posl + 1) * (posr - i + 1) * p[i];
  }
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define ll long long

using namespace std;

const int N_MAX = 200002;
const int BITS = 18;

int n;

int p[N_MAX];

int rm[N_MAX][BITS];

int rmq(int l, int r) {
  int lg = r - l + 1;
  int b = 0;
  while ((1 << (b + 1)) <= lg)
    b++;
  return min(rm[l][b], rm[r - (1 << b) + 1][b]);
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> p[i];
  for (int i = 1; i <= n; i++)
    rm[i][0] = p[i];
  for (int b = 1; b < BITS; b++)
    for (int i = 1; i <= n; i++)
      if (i + (1 << b) - 1 <= n)
        rm[i][b] = min(rm[i][b - 1], rm[i + (1 << (b - 1))][b - 1]);
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    int posl, posr;
    int l, r;
    l = 1;
    r = i;
    while (l < r) {
      int mid = (l + r) / 2;
      if (rmq(mid, i) < p[i])
        l = mid + 1;
      else
        r = mid;
    }
    posl = l;
    l = i;
    r = n;
    while (l < r) {
      int mid = (l + r + 1) / 2;
      if (rmq(i, mid) < p[i])
        r = mid - 1;
      else
        l = mid;
    }
    posr = r;
    ans += 1LL * (i - posl + 1) * (posr - i + 1) * p[i];
  }
  cout << ans << "\n";
  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
typedef long long ll;
using namespace std;

const ll MAX = 200000;
const ll INF = 1001001001;
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;

ll N, ans = 0, tmp = 0;
vector<ll> A, FV, BV;
set<ll> F, B;

signed main() {
  cin >> N;
  A.resize(N);
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    a--;
    A[a] = i;
  }
  F.insert(-1);
  F.insert(N);
  for (int i = 0; i < N; i++) {
    set<ll>::iterator itr = lower_bound(F.begin(), F.end(), A[i]);
    BV.push_back(*itr);
    itr--;
    FV.push_back(*itr);
    F.insert(A[i]);
  }
  for (int i = 0; i < N; i++) {
    // ans += 1+COM(BV[i]-FV[i]-1, 2)-COM(A[i]-FV[i]-1, 2)-COM(A[i]-BV[i]-1, 2);
    tmp = 1 + max(0LL, BV[i] - FV[i] - 1) * max(0LL, BV[i] - FV[i] - 2) / 2 -
          max(0LL, A[i] - FV[i] - 1) * max(0LL, A[i] - FV[i] - 2) / 2 -
          max(0LL, BV[i] - 1 - A[i]) * max(0LL, BV[i] - 2 - A[i]) / 2;
    ans += (i + 1) * tmp;
    // cout << ans << endl;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
typedef long long ll;
using namespace std;

const ll MAX = 200000;
const ll INF = 1001001001;
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;

ll N, ans = 0, tmp = 0;
vector<ll> A, FV, BV;
set<ll> F, B;

signed main() {
  cin >> N;
  A.resize(N);
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    a--;
    A[a] = i;
  }
  F.insert(-1);
  F.insert(N);
  for (int i = 0; i < N; i++) {
    set<ll>::iterator itr = F.lower_bound(A[i]);
    BV.push_back(*itr);
    itr--;
    FV.push_back(*itr);
    F.insert(A[i]);
  }
  for (int i = 0; i < N; i++) {
    // ans += 1+COM(BV[i]-FV[i]-1, 2)-COM(A[i]-FV[i]-1, 2)-COM(A[i]-BV[i]-1, 2);
    tmp = 1 + max(0LL, BV[i] - FV[i] - 1) * max(0LL, BV[i] - FV[i] - 2) / 2 -
          max(0LL, A[i] - FV[i] - 1) * max(0LL, A[i] - FV[i] - 2) / 2 -
          max(0LL, BV[i] - 1 - A[i]) * max(0LL, BV[i] - 2 - A[i]) / 2;
    ans += (i + 1) * tmp;
    // cout << ans << endl;
  }
  cout << ans << endl;
  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
#define int long long
int t[234567];
set<int> s;
signed main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    t[a] = i;
  }
  int ans = 0;
  s.insert(0);
  s.insert(n + 1);
  for (int i = 1; i <= n; i++) {
    auto itr = upper_bound(s.begin(), s.end(), t[i]);
    ans += i * (*itr - t[i]) * (t[i] - *(--itr));
    s.insert(t[i]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
#define int long long
int t[234567];
set<int> s;
signed main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    t[a] = i;
  }
  int ans = 0;
  s.insert(0);
  s.insert(n + 1);
  for (int i = 1; i <= n; i++) {
    auto itr = s.upper_bound(t[i]);
    ans += i * (*itr - t[i]) * (t[i] - *(--itr));
    s.insert(t[i]);
  }
  cout << ans << endl;
}