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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
typedef long long ll;
typedef vector<vector<ll>> vvl;
typedef vector<ll> vl;
typedef vector<vector<char>> vvc;
typedef vector<char> vc;
typedef pair<ll, ll> pll;
ll max(ll x, ll y) {
  if (x > y)
    return x;
  else
    return y;
}
ll min(ll x, ll y) { return x + y - max(x, y); }

ll gcd(ll x, ll y) {
  if (x > y)
    swap(x, y);
  if (y % x == 0)
    return x;
  return gcd(y - x * (y / x), x);
}

int main() {
  string s;
  cin >> s;
  int n = s.size();
  stack<char> ans;
  rep(i, n) {
    if (s[i] == 'S')
      ans.push('S');
    if (s[i] == 'T') {
      if (i == 0) {
        ans.push('T');
        continue;
      }
      char k = ans.top();
      if (k == 'S')
        ans.pop();
      if (k == 'T')
        ans.push('T');
    }
  }
  cout << ans.size() << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
typedef long long ll;
typedef vector<vector<ll>> vvl;
typedef vector<ll> vl;
typedef vector<vector<char>> vvc;
typedef vector<char> vc;
typedef pair<ll, ll> pll;
ll max(ll x, ll y) {
  if (x > y)
    return x;
  else
    return y;
}
ll min(ll x, ll y) { return x + y - max(x, y); }

ll gcd(ll x, ll y) {
  if (x > y)
    swap(x, y);
  if (y % x == 0)
    return x;
  return gcd(y - x * (y / x), x);
}

int main() {
  string s;
  cin >> s;
  int n = s.size();
  stack<char> ans;
  rep(i, n) {
    if (s[i] == 'S')
      ans.push('S');
    if (s[i] == 'T') {
      if (i == 0 || ans.empty()) {
        ans.push('T');
        continue;
      }
      char k = ans.top();
      if (k == 'S')
        ans.pop();
      if (k == 'T')
        ans.push('T');
    }
  }
  cout << ans.size() << endl;
}
Problem p03986 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
string st;
int sumt, sums, sum, i;
int main() {
  std::ios::sync_with_stdio(false);
  cin >> st;
  while (i < st.size()) {
    if (st[i] == 'S')
      sums++;
    else if (sums)
      sumt++;
    if (sums >= sumt) {
      sum += min(sums, sumt) * 2;
      sums -= min(sums, sumt);
      sumt = 0;
    }
  }
  cout << st.size() - sum << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
string st;
int sumt, sums, sum, i;
int main() {
  std::ios::sync_with_stdio(false);
  cin >> st;
  while (i < st.size()) {
    if (st[i] == 'S')
      sums++;
    else if (sums)
      sumt++;
    if (sums >= sumt) {
      sum += min(sums, sumt) * 2;
      sums -= min(sums, sumt);
      sumt = 0;
    }
    i++;
  }
  cout << st.size() - sum << endl;
  return 0;
}
Problem p03986 (C++)

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

int main() {
  string S;
  cin >> S;
  while (1) {
    int flag = 0;
    for (int i = 0; i < S.length() - 1; i++) {
      if (S.at(i) == 'S' && S.at(i + 1) == 'T') {
        // cout<<i<<"#"<<endl;
        // cout<<S<<"before"<<endl;
        S.erase(i, 1);
        S.erase(i, 1);
        i--;
        i--;
        flag = 1;
        // cout<<S<<"after"<<endl;
      }
    }

    if (flag == 0)
      break;
  }
  // cout<<S<<endl;
  cout << S.length() << endl;
  return 0;
}


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

int main() {
  string S;
  cin >> S;
  while (1) {
    int flag = 0;
    if (S.length() == 0) {
      cout << 0 << endl;
      return 0;
    }
    for (int i = 0; i < S.length() - 1; i++) {
      if (S.at(i) == 'S' && S.at(i + 1) == 'T') {
        // cout<<i<<"#"<<endl;
        // cout<<S<<"before"<<endl;
        S.erase(i, 1);
        S.erase(i, 1);
        i--;
        i--;
        flag = 1;
        // cout<<S<<"after"<<endl;
      }
    }

    if (flag == 0)
      break;
  }
  // cout<<S<<endl;
  cout << S.length() << endl;
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int main() {
  string x;
  cin >> x;
  vector<int> cnt;
  cnt.push_back(1);
  for (int i = 1; i < x.length(); i++) {
    if (x[i] == x[i - 1])
      cnt.at(cnt.size() - 1)++;
    else {
      cnt.push_back(1);
    }
  }

  int ans = x.length();
  int s = 1;
  if (x[0] == 'T')
    s = 2;
  for (; s < cnt.size(); s += 2) {
    ans -= 2 * min(cnt.at(s - 1), cnt.at(s));
    if (cnt.at(s - 1) > cnt.at(s) && s + 1 < x.length()) {
      cnt.at(s + 1) += cnt.at(s - 1) - cnt.at(s);
    }
  }

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

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

int main() {
  string x;
  cin >> x;
  vector<int> cnt;
  cnt.push_back(1);
  for (int i = 1; i < x.length(); i++) {
    if (x[i] == x[i - 1])
      cnt.at(cnt.size() - 1)++;
    else {
      cnt.push_back(1);
    }
  }

  int ans = x.length();
  int s = 1;
  if (x[0] == 'T')
    s = 2;
  for (; s < cnt.size(); s += 2) {
    ans -= 2 * min(cnt.at(s - 1), cnt.at(s));
    if (cnt.at(s - 1) > cnt.at(s) && s + 1 < cnt.size()) {
      cnt.at(s + 1) += cnt.at(s - 1) - cnt.at(s);
    }
  }

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

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define pb(x) push_back(x)
#define SORT(x) sort((x).begin(), (x).end())
#define INF 999999999

using namespace std;

#define int long long

int32_t main() {
  string str;
  cin >> str;
  int n = str.size();

  stack<int> st;
  REP(i, n) {
    if (str[i] == 'S')
      st.push(str[i]);
    else if (st.top() == 'S')
      st.pop();
    else
      st.push(str[i]);
  }

  cout << st.size() << endl;
}

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define pb(x) push_back(x)
#define SORT(x) sort((x).begin(), (x).end())
#define INF 999999999

using namespace std;

#define int long long

int32_t main() {
  string str;
  cin >> str;
  int n = str.size();

  stack<int> st;
  REP(i, n) {
    if (str[i] == 'S')
      st.push(str[i]);
    else if (st.size() != 0 && st.top() == 'S')
      st.pop();
    else
      st.push(str[i]);
  }

  cout << st.size() << endl;
}
Problem p03986 (C++)

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

int main(void) {
  string x;
  cin >> x;

  stack<char> s;
  for (int i = 0; i < x.size(); i++) {
    if (s.top() == 'T' || s.empty()) {
      s.push(x[i]);
    } else {
      if (x[i] == 'T')
        s.pop();
      else
        s.push(x[i]);
    }
  }

  cout << s.size() << endl;
}

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

int main(void) {
  string x;
  cin >> x;

  stack<char> s;
  for (int i = 0; i < x.size(); i++) {
    if (s.empty() || s.top() == 'T') {
      s.push(x[i]);
    } else {
      if (x[i] == 'T')
        s.pop();
      else
        s.push(x[i]);
    }
  }

  cout << s.size() << endl;
}
Problem p03986 (C++)

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

using namespace std;

int main() {
  string s;
  cin >> s;

  int n = (int)s.size();
  stack<char> st;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'S')
      st.push(s[i]);
    else {
      if (st.top() == 'S')
        st.pop();
      else
        st.push(s[i]);
    }
  }
  cout << st.size() << endl;
  return 0;
}


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

using namespace std;

int main() {
  string s;
  cin >> s;

  int n = (int)s.size();
  stack<char> st;
  for (int i = 0; i < n; ++i) {
    if (st.empty())
      st.push(s[i]);
    else if (s[i] == 'S')
      st.push(s[i]);
    else {
      if (st.top() == 'S')
        st.pop();
      else
        st.push(s[i]);
    }
  }
  cout << st.size() << endl;
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
using namespace std;
const int N = 1e5 + 5;
char s[N];
int res, n, sum;
int main() {
  scanf("%s", s + 1), res = n = strlen(s + 1);
  fp(i, 1, n) if (s[i] == 'S')++ sum;
  else if (sum)-- sum, res -= 2;
  printf("%d\n", res);
  return 0;
}

Fixed submission:
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
using namespace std;
const int N = 5e5 + 5;
char s[N];
int res, n, sum;
int main() {
  scanf("%s", s + 1), res = n = strlen(s + 1);
  fp(i, 1, n) if (s[i] == 'S')++ sum;
  else if (sum)-- sum, res -= 2;
  printf("%d\n", res);
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
using LL = long long;
const LL LINF = 1e18;
using namespace std;
#define COUT(v) cout << (v) << endl
#define CIN(n)                                                                 \
  int(n);                                                                      \
  cin >> (n)
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)

#define FOREACH(x, a) for (auto &(x) : (a))

#define ALL(obj) (obj).begin(), (obj).end()

#define P pair<int, int>
#define I vector<int>
#define S set<int>
#define pb(v) push_back(v)
#define V vector
#define rt return
class Edge {
public:
  int from, to, value;
  Edge(int a, int b, int c) {
    from = a;
    to = b;
    value = c;
  }
  Edge(int a, int b) {
    from = a;
    to = b;
  }
};
int main() {
  string str;
  cin >> str;
  stack<char> sta;
  for (int a = 0; a < str.size(); a++) {
    if (str.at(a) == 'S') {
      sta.push(str.at(a));
    }
    if (str.at(a) == 'T' && (sta.top() == 'T' || sta.empty())) {
      sta.push(str.at(a));
    } else if (str.at(a) == 'T' && sta.top() == 'S') {
      sta.pop();
    }
  }
  COUT(sta.size());
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
using LL = long long;
const LL LINF = 1e18;
using namespace std;
#define COUT(v) cout << (v) << endl
#define CIN(n)                                                                 \
  int(n);                                                                      \
  cin >> (n)
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)

#define FOREACH(x, a) for (auto &(x) : (a))

#define ALL(obj) (obj).begin(), (obj).end()

#define P pair<int, int>
#define I vector<int>
#define S set<int>
#define pb(v) push_back(v)
#define V vector
#define rt return
class Edge {
public:
  int from, to, value;
  Edge(int a, int b, int c) {
    from = a;
    to = b;
    value = c;
  }
  Edge(int a, int b) {
    from = a;
    to = b;
  }
};
int main() {
  string str;
  cin >> str;
  stack<char> sta;
  for (int a = 0; a < str.size(); a++) {
    if (str.at(a) == 'S') {
      sta.push(str.at(a));
    } else if (str.at(a) == 'T' && sta.empty()) {
      sta.push(str.at(a));
    } else if (str.at(a) == 'T' && sta.top() == 'T') {
      sta.push(str.at(a));
    } else if (str.at(a) == 'T' && sta.top() == 'S') {
      sta.pop();
    }
  }
  COUT(sta.size());
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  string X;
  cin >> X;
  for (int i = 0; i < X.size() - 1 && !X.empty();) {
    if (X[i] == 'S' && X[i + 1] == 'T') {
      X.erase(i, 2);
      i = 0;
    } else {
      i++;
    }
  }
  cout << X.size() << endl;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  string X;
  cin >> X;
  for (int i = 1; i < X.size(); i++) {
    if (X[i - 1] == 'S' && X[i] == 'T') {
      X.erase(i - 1, 2);
      i -= 2;
    }
  }
  cout << X.size() << endl;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>

const int MAXN = 2E3 + 10;

char s[MAXN];
int que[MAXN], tail;

int main() {
  scanf("%s", s);
  for (int i = 0; s[i]; ++i)
    if (s[i] == 'T' && tail >= 0 && que[tail] == 'S')
      --tail;
    else
      que[++tail] = s[i];
  printf("%d\n", tail);
  return 0;
}


Fixed submission:
#include <cstdio>

const int MAXN = 2E5 + 10;

char s[MAXN];
int que[MAXN], tail;

int main() {
  scanf("%s", s);
  for (int i = 0; s[i]; ++i)
    if (s[i] == 'T' && tail >= 0 && que[tail] == 'S')
      --tail;
    else
      que[++tail] = s[i];
  printf("%d\n", tail);
  return 0;
}
Problem p03986 (C++)

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

using namespace std;

#define MAXN 1010
#define INF 0x3f3f3f3f
#define mo 1000000007
typedef long long LL;

char s[MAXN];

int main() {
  // #ifdef MYCP
  //     freopen("data.in", "r", stdin);
  // #endif // MYCP

  int ans = 0, cnt = 0;
  scanf("%s", s);
  for (int i = 0; s[i]; ++i) {
    if (s[i] == 'S')
      ++cnt;
    else if (cnt)
      --cnt;
    else
      ++ans;
  }
  printf("%d\n", ans + cnt);

  return 0;
}


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

using namespace std;

#define MAXN 200010
#define INF 0x3f3f3f3f
#define mo 1000000007
typedef long long LL;

char s[MAXN];

int main() {
  // #ifdef MYCP
  //     freopen("data.in", "r", stdin);
  // #endif // MYCP

  int ans = 0, cnt = 0;
  scanf("%s", s);
  for (int i = 0; s[i]; ++i) {
    if (s[i] == 'S')
      ++cnt;
    else if (cnt)
      --cnt;
    else
      ++ans;
  }
  printf("%d\n", ans + cnt);

  return 0;
}
Problem p03986 (C++)

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

int main() {

  string s;
  cin >> s;
  stack<char> st;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'S')
      st.push(s[i]);
    else {
      if (st.top() == 'S')
        st.pop();
      else
        st.push(s[i]);
    }
  }
  cout << st.size() << endl;
  return 0;
}

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

int main() {

  string s;
  cin >> s;
  stack<char> st;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'S')
      st.push(s[i]);
    else {
      if (st.size() && st.top() == 'S')
        st.pop();
      else
        st.push(s[i]);
    }
  }
  cout << st.size() << endl;
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
char s[222222];
char st[222222], top;
int main() {
  scanf("%s", s);
  top = 0;
  for (int i = 0; s[i]; i++)
    if (top > 0 && s[i] == 'T' && st[top - 1] == 'S')
      --top;
    else
      st[top++] = s[i];
  printf("%d\n", top);
  return 0;
}

Fixed submission:
#include <stdio.h>
char s[222222];
char st[222222];
int top;
int main() {
  scanf("%s", s);
  top = 0;
  for (int i = 0; s[i]; i++)
    if (top > 0 && s[i] == 'T' && st[top - 1] == 'S')
      --top;
    else
      st[top++] = s[i];
  printf("%d\n", top);
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <string>

using namespace std;

int main() {
  char buf[262144];
  scanf("%s", buf);
  string X(buf);

  while (X.find("ST") != string::npos)
    X.erase(X.find("ST"), 2);

  printf("%llu\n", X.length());
}

Fixed submission:
#include <cstdio>
#include <string>

using namespace std;

int main() {
  char buf[262144];
  scanf("%s", buf);
  string X(buf);

  int res = 0, cur = 0;
  for (size_t i = 0; i < X.length(); ++i) {
    if (X[i] == 'S') {
      ++cur;
    } else if (X[i] == 'T') {
      --cur;
      if (cur < 0) {
        res -= cur;
        cur = 0;
      }
    }
  }
  res += cur;
  printf("%d\n", res);
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
typedef unsigned long long ull;

int N;
ull b[200001];
set<int> pos;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  for (int i = 1; i <= N; i++) {
    int a;
    cin >> a;
    b[a] = i;
  }
  pos.insert(0);
  pos.insert(N + 1);

  ull res = 0;
  ull l, r;
  for (ull i = 1; i <= N; i++) {
    auto itr = upper_bound(pos.begin(), pos.end(), b[i]);
    r = *itr;
    itr--;
    l = *itr;
    itr++;

    res += i * (b[i] - l) * (r - b[i]);
    pos.insert(itr, b[i]);
  }
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
typedef unsigned long long ull;

int N;
ull b[200001];
set<int> pos;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  for (int i = 1; i <= N; i++) {
    int a;
    cin >> a;
    b[a] = i;
  }
  pos.insert(0);
  pos.insert(N + 1);

  ull res = 0;
  ull l, r;
  for (ull i = 1; i <= N; i++) {
    auto itr = pos.upper_bound(b[i]);
    r = *itr;
    itr--;
    l = *itr;
    itr++;

    res += i * (b[i] - l) * (r - b[i]);
    pos.insert(itr, b[i]);
  }
  cout << res << endl;
  return 0;
}
Problem p03987 (C++)

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<ll, pair<ll, ll>> P3;

const ll MOD = ll(1e9 + 7);
const ll LLINF = LLONG_MAX;
const int IINF = INT_MAX;
const int MAX_N = int(1e5) + 5;
const double EPS = 1e-8;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()

ll n, a[100005];

int main() {
  cin >> n;
  REP(i, n) {
    int t;
    cin >> t;
    a[t - 1] = i;
  }
  ll ans = 0;
  set<ll> used;
  used.insert(-1);
  used.insert(n);
  REP(i, n) {
    ll l, r;
    l = a[i] - *--used.lower_bound(a[i]);
    r = *used.lower_bound(a[i]) - a[i];
    ans += (i + 1) * l * r;
    used.insert(a[i]);
  }
  cout << ans << endl;
  return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<ll, pair<ll, ll>> P3;

const ll MOD = ll(1e9 + 7);
const ll LLINF = LLONG_MAX;
const int IINF = INT_MAX;
const int MAX_N = int(1e5) + 5;
const double EPS = 1e-8;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()

ll n, a[200005];

int main() {
  cin >> n;
  REP(i, n) {
    int t;
    cin >> t;
    a[t - 1] = i;
  }
  ll ans = 0;
  set<ll> used;
  used.insert(-1);
  used.insert(n);
  REP(i, n) {
    ll l, r;
    l = a[i] - *--used.lower_bound(a[i]);
    r = *used.lower_bound(a[i]) - a[i];
    ans += (i + 1) * l * r;
    used.insert(a[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, pair<ll, ll>> P3;
const ll MOD = 1000000007;
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(1e5 + 5);
const double EPS = 1e-10;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define ALL(v) (v).begin(), (v).end()

int main() {
  ll n, a[MAX_N], p[MAX_N], ans = 0;
  cin >> n;
  REP(i, n) {
    cin >> a[i];
    p[a[i]] = i;
  }
  set<ll> v;
  v.insert(-1);
  v.insert(n);
  REP(i, n) {
    auto k = v.lower_bound(p[i + 1]);
    ll l, r;
    r = *k;
    l = *(--k);
    ans += (r - p[i + 1]) * (p[i + 1] - l) * (i + 1);
    // cout << l << " " << r << endl;
    v.insert(p[i + 1]);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, pair<ll, ll>> P3;
const ll MOD = 1000000007;
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(2e5 + 5);
const double EPS = 1e-10;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define ALL(v) (v).begin(), (v).end()

int main() {
  ll n, a[MAX_N], p[MAX_N], ans = 0;
  cin >> n;
  REP(i, n) {
    cin >> a[i];
    p[a[i]] = i;
  }
  set<ll> v;
  v.insert(-1);
  v.insert(n);
  REP(i, n) {
    auto k = v.lower_bound(p[i + 1]);
    ll l, r;
    r = *k;
    l = *(--k);
    ans += (r - p[i + 1]) * (p[i + 1] - l) * (i + 1);
    // cout << l << " " << r << endl;
    v.insert(p[i + 1]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long int ll;
#define FOR(i, a, b) for (ll i = (signed)(a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define MOD 1000000007
#define pb push_back
#define INF 93193111451418101
#define MIN -93193111451418101
#define EPS 1e-11
#define tp(a, b, c) make_tuple(a, b, c)
#define lb(a, b) lower_bound((a).begin(), (a).end(), (b))
#define ub(a, b) upper_bound((a).begin(), (a).end(), (b))
#define bitcnt(a) (ll) __builtin_popcount((a))
using namespace std;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
template <typename T> void fill_all(T &arr, const T &v) { arr = v; }
template <typename T, typename ARR> void fill_all(ARR &arr, const T &v) {
  for (auto &i : arr) {
    fill_all(i, v);
  }
}
//------------------変数-----------------------//
//-------------------関数----------------------//
ll n, a[314514], itrplace[314514];
set<ll> s;
int main() {
  cin >> n;
  REP(i, n) {
    cin >> a[i];
    itrplace[a[i]] = i + 1;
  }
  s.insert(0);
  s.insert(n + 1);
  ll cc = 0;
  REP(i, n) {
    ll center = itrplace[i + 1];
    auto itr = ub(s, center);
    auto itr2 = itr;
    itr2--;
    ll cnt = ((*itr) - center) * (center - (*(itr2)));
    cc += cnt * (i + 1);
    //  cout << cnt << endl;
    s.insert(center);
  }
  cout << cc << endl;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
#define FOR(i, a, b) for (ll i = (signed)(a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define MOD 1000000007
#define pb push_back
#define INF 93193111451418101
#define MIN -93193111451418101
#define EPS 1e-11
#define tp(a, b, c) make_tuple(a, b, c)
#define lb(a, b) lower_bound((a).begin(), (a).end(), (b))
#define ub(a, b) upper_bound((a).begin(), (a).end(), (b))
#define bitcnt(a) (ll) __builtin_popcount((a))
using namespace std;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
template <typename T> void fill_all(T &arr, const T &v) { arr = v; }
template <typename T, typename ARR> void fill_all(ARR &arr, const T &v) {
  for (auto &i : arr) {
    fill_all(i, v);
  }
}
//------------------変数-----------------------//
//-------------------関数----------------------//
ll n, a[314514], itrplace[314514];
set<ll> s;
int main() {
  cin >> n;
  REP(i, n) {
    cin >> a[i];
    itrplace[a[i]] = i + 1;
  }
  s.insert(0);
  s.insert(n + 1);
  ll cc = 0;
  REP(i, n) {
    ll center = itrplace[i + 1];
    auto itr = s.upper_bound(center);
    auto itr2 = itr;
    itr2--;
    ll cnt = ((*itr) - center) * (center - (*(itr2)));
    cc += cnt * (i + 1);
    //  cout << cnt << endl;
    s.insert(center);
  }
  cout << cc << endl;
}
Problem p03987 (C++)

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

using namespace std;
inline void prep() {
  cin.tie(0);
  cin.sync_with_stdio(0);
};
const double pi = acos(-1.);
// printf(%.12lf\n,mid);
//  __builtin_popcount(int) count 1's in binary
//  get decimal part of number
/*
double intpart;
double thiss = modf((double)(n*m)/(double)k, &intpart);
*/
// round = (int)(num+.5)
long long mod = 1000000007;

long long mymod(long long num) {
  if (num < 0 && !(num % mod == 0)) {
    if (mod == 1) {
      return 0;
    }
    return mod - abs(num) % mod;
  } else {
    return num % mod;
  }
}

/*string sentence = And I feel fine...;
    istringstream iss(sentence);
*/

double eps = .000000001;

int main() {
  prep();
  int n;
  cin >> n;
  int arr[n];
  int place[100001];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    place[arr[i]] = i;
  }
  long long res = 0;
  set<int> indexs;
  for (int i = 1; i <= n; i++) {
    int myind = place[i];
    int bottom = 0;
    int top = n - 1;
    auto lower = indexs.lower_bound(myind);
    if (lower != indexs.begin()) {
      lower--;
      bottom = *lower + 1;
    }
    auto upper = indexs.upper_bound(myind);
    if (upper != indexs.end()) {
      top = *upper - 1;
    }
    long long diff1 = myind - bottom + 1;
    long long diff2 = top - myind + 1;
    // cout << bottom << " " << top << endl;
    res += i * diff1 * diff2;
    indexs.insert(place[i]);
  }
  cout << res;
  return 0;
}

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

using namespace std;
inline void prep() {
  cin.tie(0);
  cin.sync_with_stdio(0);
};
const double pi = acos(-1.);
// printf(%.12lf\n,mid);
//  __builtin_popcount(int) count 1's in binary
//  get decimal part of number
/*
double intpart;
double thiss = modf((double)(n*m)/(double)k, &intpart);
*/
// round = (int)(num+.5)
long long mod = 1000000007;

long long mymod(long long num) {
  if (num < 0 && !(num % mod == 0)) {
    if (mod == 1) {
      return 0;
    }
    return mod - abs(num) % mod;
  } else {
    return num % mod;
  }
}

/*string sentence = And I feel fine...;
    istringstream iss(sentence);
*/

double eps = .000000001;

int main() {
  prep();
  int n;
  cin >> n;
  int arr[n];
  int place[200001];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    place[arr[i]] = i;
  }
  long long res = 0;
  set<int> indexs;
  for (int i = 1; i <= n; i++) {
    int myind = place[i];
    int bottom = 0;
    int top = n - 1;
    auto lower = indexs.lower_bound(myind);
    if (lower != indexs.begin()) {
      lower--;
      bottom = *lower + 1;
    }
    auto upper = indexs.upper_bound(myind);
    if (upper != indexs.end()) {
      top = *upper - 1;
    }
    long long diff1 = myind - bottom + 1;
    long long diff2 = top - myind + 1;
    // cout << bottom << " " << top << endl;
    res += i * diff1 * diff2;
    indexs.insert(place[i]);
  }
  cout << res;
  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (i = 0; i < n; i++)
#define loop(i, a, n) for (i = a; i < n; i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define int ll
typedef vector<int> vi;
typedef pair<int, int> pii;

signed main(void) {
  int i, j;
  int n;
  cin >> n;
  vi a(n), ord(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
    ord[a[i]] = i;
  }
  set<int> st;
  st.insert(-1);
  st.insert(n);
  ll ans = 0;
  rep(i, n) {
    st.insert(ord[i]);
    set<int>::iterator x = lower_bound(all(st), ord[i]);
    set<int>::iterator y, z;
    y = x;
    y--;
    z = x;
    z++;
    int left = *x - *y;
    int right = *z - *x;
    ans += left * right * (i + 1);
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (i = 0; i < n; i++)
#define loop(i, a, n) for (i = a; i < n; i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define int ll
typedef vector<int> vi;
typedef pair<int, int> pii;

signed main(void) {
  int i, j;
  int n;
  cin >> n;
  vi a(n), ord(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
    ord[a[i]] = i;
  }
  set<int> st;
  st.insert(-1);
  st.insert(n);
  ll ans = 0;
  rep(i, n) {
    st.insert(ord[i]);
    set<int>::iterator x = st.lower_bound(ord[i]);
    set<int>::iterator y, z;
    y = x;
    y--;
    z = x;
    z++;
    int left = *x - *y;
    int right = *z - *x;
    ans += left * right * (i + 1);
  }
  cout << ans << endl;
}
Problem p03987 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

int inva[100'000];

int main() {
  int n;
  cin >> n;
  rep(i, n) {
    int j;
    cin >> j;
    inva[j - 1] = i + 1;
  }

  int64_t result = 0;
  set<int> s;
  s.insert(0);
  s.insert(n + 1);

  rep(i, n) {
    auto ub = s.upper_bound(inva[i]);
    int R = *ub;
    ub--;
    int L = *ub;
    result += (int64_t)(i + 1) * (inva[i] - L) * (R - inva[i]);
    s.insert(inva[i]);
  }
  cout << result << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

int inva[200'000];

int main() {
  int n;
  cin >> n;
  rep(i, n) {
    int j;
    cin >> j;
    inva[j - 1] = i + 1;
  }

  int64_t result = 0;
  set<int> s;
  s.insert(0);
  s.insert(n + 1);

  rep(i, n) {
    auto ub = s.upper_bound(inva[i]);
    int R = *ub;
    ub--;
    int L = *ub;
    result += (int64_t)(i + 1) * (inva[i] - L) * (R - inva[i]);
    s.insert(inva[i]);
  }
  cout << result << endl;

  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N), pos(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    pos[A[i] - 1] = i;
  }
  set<int> st;
  st.insert(-1);
  st.insert(N);

  long long ans = 0ll;
  for (int i = 0; i < N; i++) {
    auto itr = lower_bound(st.begin(), st.end(), pos[i]);
    long long right = (*itr) - pos[i];
    --itr;
    long long left = pos[i] - (*itr);
    long long tmp = A[pos[i]] * left * right;
    ans += tmp;
    st.insert(pos[i]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N), pos(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    pos[A[i] - 1] = i;
  }
  set<int> st;
  st.insert(-1);
  st.insert(N);

  long long ans = 0ll;
  for (int i = 0; i < N; i++) {
    auto itr = st.lower_bound(pos[i]);
    long long right = (*itr) - pos[i];
    --itr;
    long long left = pos[i] - (*itr);
    long long tmp = A[pos[i]] * left * right;
    ans += tmp;
    st.insert(pos[i]);
  }
  cout << ans << endl;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <complex>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout << (s) << endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")

const ll mod = 1e9 + 7;
const ll inf = 1e18;

template <typename T> void vprint(T &V) {
  for (auto v : V) {
    cout << v << " ";
  }
  cout << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  // input
  ll N;
  cin >> N;

  vector<ll> A(N);
  vector<ll> B(N + 1);
  FOR(i, 0, N) {
    ll a;
    cin >> a;
    A[i] = a;

    B[a] = i;
  }
  // vprint(A);
  // vprint(B);

  set<ll> se;
  se.insert(-1);
  se.insert(N);

  ll sum = 0;

  FOR(a, 1, N + 1) {
    ll index = B[a];
    // br();
    // cout << "a " << a << " index " << index << endl;

    auto it = lower_bound(ALL(se), index);
    ll right = *it;
    it--;
    ll left = *it;
    // pn(left);
    // pn(right);

    sum += a * (right - index) * (index - left);

    se.insert(index);
  }

  p(sum);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout << (s) << endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")

const ll mod = 1e9 + 7;
const ll inf = 1e18;

template <typename T> void vprint(T &V) {
  for (auto v : V) {
    cout << v << " ";
  }
  cout << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  // input
  ll N;
  cin >> N;

  vector<ll> A(N);
  vector<ll> B(N + 1);
  FOR(i, 0, N) {
    ll a;
    cin >> a;
    A[i] = a;

    B[a] = i;
  }
  // vprint(A);
  // vprint(B);

  set<ll> se;
  se.insert(-1);
  se.insert(N);

  ll sum = 0;

  FOR(a, 1, N + 1) {
    ll index = B[a];
    // br();
    // cout << "a " << a << " index " << index << endl;

    // auto it = lower_bound(ALL(se), index);
    auto it = se.lower_bound(index);

    ll right = *it;
    it--;
    ll left = *it;
    // pn(left);
    // pn(right);

    sum += a * (right - index) * (index - left);

    se.insert(index);
  }

  p(sum);

  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define mod 1000000007
#define pb push_back
#define ll long long
using namespace std;
ll place[214514];
int main() {
  ll n, res = 0;
  cin >> n;
  vector<ll> a;
  set<ll> used;
  for (ll i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    a.pb(x);
    place[x] = i;
  }
  used.insert(0);
  used.insert(n + 1);
  for (ll i = 1; i <= n; i++) {
    ll cur = place[i];
    set<ll>::iterator it = upper_bound(used.begin(), used.end(), cur);
    ll rig = *it;
    it--;
    ll lef = *it;
    res += i * (rig - cur) * (cur - lef);
    used.insert(cur);
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define mod 1000000007
#define pb push_back
#define ll long long
using namespace std;
ll place[214514];
int main() {
  ll n, res = 0;
  cin >> n;
  vector<ll> a;
  set<ll> used;
  for (ll i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    a.pb(x);
    place[x] = i;
  }
  used.insert(0);
  used.insert(n + 1);
  for (ll i = 1; i <= n; i++) {
    ll cur = place[i];
    set<ll>::iterator it = used.upper_bound(cur);
    ll rig = *it;
    it--;
    ll lef = *it;
    res += i * (rig - cur) * (cur - lef);
    used.insert(cur);
  }
  cout << res << endl;
  return 0;
}
Problem p03987 (C++)

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

const int MAXN = 1e5 + 5;
int h[MAXN], ent[MAXN], sai[MAXN];
stack<int> st, sti;
unsigned long long int resp, cur, cur2;
int n;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &h[i]);

  st.push(-1);
  sti.push(0);
  for (int i = 1; i <= n + 1; i++) {
    if (i == n + 1)
      h[i] = 0;
    while (st.size() && st.top() > h[i]) {
      sai[sti.top()] = i - 1;
      st.pop();
      sti.pop();
    }
    ent[i] = sti.top() + 1;
    st.push(h[i]);
    sti.push(i);
  }

  for (int i = 1; i <= n; i++) {
    cur = h[i];
    cur *= (sai[i] - ent[i] + 1);
    cur2 = i - ent[i];
    cur2 *= sai[i] - i;
    cur2 *= h[i];
    resp += cur;
    resp += cur2;
  }

  printf("%llu\n", resp);
}


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

const int MAXN = 2e5 + 5;
int h[MAXN], ent[MAXN], sai[MAXN];
stack<int> st, sti;
unsigned long long int resp, cur, cur2;
int n;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &h[i]);

  st.push(-1);
  sti.push(0);
  for (int i = 1; i <= n + 1; i++) {
    if (i == n + 1)
      h[i] = 0;
    while (st.size() && st.top() > h[i]) {
      sai[sti.top()] = i - 1;
      st.pop();
      sti.pop();
    }
    ent[i] = sti.top() + 1;
    st.push(h[i]);
    sti.push(i);
  }

  for (int i = 1; i <= n; i++) {
    cur = h[i];
    cur *= (sai[i] - ent[i] + 1);
    cur2 = i - ent[i];
    cur2 *= sai[i] - i;
    cur2 *= h[i];
    resp += cur;
    resp += cur2;
  }

  printf("%llu\n", resp);
}
Problem p03987 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF (1 << 30)
#define INFLL (1ll << 60)
typedef pair<int, int> P;
#define MOD (1000000007ll)
#define l_ength size
#define EPS (1e-10)

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

set<ll> v;

int main(void) {
  ll n, a, i, j, p[123456], ans = 0ll, x, y;
  cin >> n;
  for (i = 0ll; i < n; ++i) {
    cin >> a;
    p[a - 1] = i;
  }
  for (i = 0ll; i < n; ++i) {
    auto q = v.upper_bound(p[i]);
    if (q == v.end()) {
      x = (n - p[i]);
    } else {
      x = *q - p[i];
    }
    if (q == v.begin()) {
      y = p[i] + 1;
    } else {
      --q;
      y = p[i] - *q;
      ++q;
    }
    ans += (i + 1) * x * y;
    v.insert(p[i]);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF (1 << 30)
#define INFLL (1ll << 60)
typedef pair<int, int> P;
#define MOD (1000000007ll)
#define l_ength size
#define EPS (1e-10)

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

set<ll> v;

int main(void) {
  ll n, a, i, j, p[225816], ans = 0ll, x, y;
  cin >> n;
  for (i = 0ll; i < n; ++i) {
    cin >> a;
    p[a - 1] = i;
  }
  for (i = 0ll; i < n; ++i) {
    auto q = v.upper_bound(p[i]);
    if (q == v.end()) {
      x = (n - p[i]);
    } else {
      x = *q - p[i];
    }
    if (q == v.begin()) {
      y = p[i] + 1;
    } else {
      --q;
      y = p[i] - *q;
      ++q;
    }
    ans += (i + 1) * x * y;
    v.insert(p[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

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

  int m[100005]; // num, pos
  rep(i, n) {
    int a;
    cin >> a;
    m[a] = i;
  }

  set<int> pos;
  pos.insert(-1);
  pos.insert(n);
  long long ans = 0;
  range(i, 1, n + 1) {
    int cur = m[i];

    auto it = pos.upper_bound(cur);
    int left = cur - *prev(it, 1);
    // show(left)
    long right = *it - cur;
    // show(right)

    ans += 1LL * left * right * i;
    // show(ans)
    pos.emplace(cur);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

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

  int m[200005]; // num, pos
  rep(i, n) {
    int a;
    cin >> a;
    m[a] = i;
  }

  set<int> pos;
  pos.insert(-1);
  pos.insert(n);
  long long ans = 0;
  range(i, 1, n + 1) {
    int cur = m[i];

    auto it = pos.upper_bound(cur);
    int left = cur - *prev(it, 1);
    // show(left)
    long right = *it - cur;
    // show(right)

    ans += 1LL * left * right * i;
    // show(ans)
    pos.emplace(cur);
  }
  cout << ans << endl;
}
Problem p03987 (C++)

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

using namespace std;

const int MAX = 1e5 + 10;

long long arr[MAX], L[MAX], R[MAX];
int n;

int main() {
  memset(L, -1, sizeof(L));
  memset(R, -1, sizeof(R));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> arr[i];
  stack<int> s;
  for (int i = 0; i < n; ++i) {
    while (s.size() > 0 && arr[s.top()] > arr[i]) {
      R[s.top()] = i - 1;
      s.pop();
    }
    if (!s.size())
      L[i] = 0;
    else
      L[i] = s.top() + 1;
    s.push(i);
  }
  for (int i = 0; i < n; ++i) {
    if (L[i] == -1)
      L[i] = 0;
    if (R[i] == -1)
      R[i] = n - 1;
  }
  long long ans = 0ll;
  for (int i = 0; i < n; ++i) {
    long long x = i - L[i] + 1ll;
    long long y = R[i] - i + 1ll;
    ans += x * y * arr[i];
  }
  return cout << ans << "\n", 0;
}

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

using namespace std;

const int MAX = 2e5 + 10;

long long arr[MAX], L[MAX], R[MAX];
int n;

int main() {
  memset(L, -1, sizeof(L));
  memset(R, -1, sizeof(R));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> arr[i];
  stack<int> s;
  for (int i = 0; i < n; ++i) {
    while (s.size() > 0 && arr[s.top()] > arr[i]) {
      R[s.top()] = i - 1;
      s.pop();
    }
    if (!s.size())
      L[i] = 0;
    else
      L[i] = s.top() + 1;
    s.push(i);
  }
  for (int i = 0; i < n; ++i) {
    if (L[i] == -1)
      L[i] = 0;
    if (R[i] == -1)
      R[i] = n - 1;
  }
  long long ans = 0ll;
  for (int i = 0; i < n; ++i) {
    long long x = i - L[i] + 1ll;
    long long y = R[i] - i + 1ll;
    ans += x * y * arr[i];
  }
  return cout << ans << "\n", 0;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>

using namespace std;

long long a[200000];
int ind_a[200000];
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < N; i++) {
    ind_a[a[i] - 1] = i;
  }

  set<int> used_inds;
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    long long left_border;
    long long right_border;
    long long cur_ind = ind_a[i];
    auto left_it = lower_bound(used_inds.begin(), used_inds.end(), cur_ind);
    auto right_it =
        lower_bound(used_inds.begin(), used_inds.end(), cur_ind + 1);

    if (left_it == used_inds.begin()) {
      left_border = -1;
    } else {
      if (left_it == used_inds.end() || *left_it > cur_ind) {
        left_it--;
      }
      left_border = *left_it;
    }
    if (right_it == used_inds.end()) {
      right_border = N;
    } else {
      right_border = *right_it;
    }
    ans +=
        (cur_ind - left_border) * (right_border - cur_ind) * ((long long)i + 1);

    // cout << i << " " << left_border << " " << right_border << endl;
    // cout << ans << endl;

    used_inds.insert(cur_ind);
  }

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


Fixed submission:
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>

using namespace std;

long long a[200000];
int ind_a[200000];
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < N; i++) {
    ind_a[a[i] - 1] = i;
  }

  set<int> used_inds;
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    long long left_border;
    long long right_border;
    long long cur_ind = ind_a[i];
    auto left_it = used_inds.lower_bound(cur_ind);
    auto right_it = used_inds.lower_bound(cur_ind + 1);

    if (left_it == used_inds.begin()) {
      left_border = -1;
    } else {
      if (left_it == used_inds.end() || *left_it > cur_ind) {
        left_it--;
      }
      left_border = *left_it;
    }
    if (right_it == used_inds.end()) {
      right_border = N;
    } else {
      right_border = *right_it;
    }
    ans +=
        (cur_ind - left_border) * (right_border - cur_ind) * ((long long)i + 1);

    // cout << i << " " << left_border << " " << right_border << endl;
    // cout << ans << endl;

    used_inds.insert(cur_ind);
  }

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

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

int main() {
  int n, b[2000];
  set<int> p;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    b[a] = i;
  }
  p.insert(0);
  p.insert(n + 1);
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    int l = *(--p.lower_bound(b[i])), r = *p.upper_bound(b[i]);
    ans += (ll)i * (b[i] - l) * (r - b[i]);
    p.insert(b[i]);
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n, b[200010];
  set<int> p;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    b[a] = i;
  }
  p.insert(0);
  p.insert(n + 1);
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    int l = *(--p.lower_bound(b[i])), r = *p.upper_bound(b[i]);
    ans += (ll)i * (b[i] - l) * (r - b[i]);
    p.insert(b[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03987 (C++)

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

int n;
int a[200010];
set<int> s;
long long ans;

int main() {
  //	cin.tie(0);
  //	ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    a[t] = i + 1;
  }
  //	for(int i=1; i<=n; ++i)
  //		cout << a[i] << "\n";
  s.insert(0);
  s.insert(n + 1);
  for (int i = 1; i <= n; ++i) {
    ans += (long long)i * (*lower_bound(s.begin(), s.end(), a[i]) - a[i]) *
           (a[i] - *prev(lower_bound(s.begin(), s.end(), a[i])));
    s.insert(a[i]);
  }
  cout << ans << "\n";
}

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

int n;
int a[200010];
set<int> s;
long long ans;

int main() {
  //	cin.tie(0);
  //	ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    a[t] = i + 1;
  }
  //	for(int i=1; i<=n; ++i)
  //		cout << a[i] << "\n";
  s.insert(0);
  s.insert(n + 1);
  for (int i = 1; i <= n; ++i) {
    auto ite = s.lower_bound(a[i]);
    ans += (long long)i * (*ite - a[i]) * (a[i] - *prev(ite));
    s.insert(a[i]);
  }
  cout << ans << "\n";
}