Problem p00348 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define MAX 3000000
int n, size;
vector<int> A;
unordered_map<int, int> mp;
vector<ll> bit;
vector<P> tree;

void update(int a, int b, P x, int k, int l, int r) {
  if (b <= l || r <= a)
    return;
  if (a <= l && r <= b) {
    tree[k] = max(tree[k], x);
  } else {
    int m = (l + r) / 2;
    update(a, b, x, k * 2 + 1, l, m);
    update(a, b, x, k * 2 + 2, m, r);
  }
}

void Upd(int a, int b, P x) {
  a += 2;
  b += 2;
  if (a < b) {
    update(a, b + 1, x, 0, 0, size);
  } else {
    update(a, size, x, 0, 0, size);
    update(0, b + 1, x, 0, 0, size);
  }
}

ll Get(int i) {
  i += 2;
  i += size - 1;
  P res = tree[i];
  while (i) {
    i = (i - 1) / 2;
    res = max(res, tree[i]);
  }
  return res.second;
}

void Add(int i, ll x) {
  i += 2;
  while (i < (int)bit.size()) {
    bit[i] += x;
    i += (i & -i);
  }
}

ll Sum(int i) {
  i += 2;
  ll res = 0;
  while (i > 0) {
    res += bit[i];
    i -= (i & -i);
  }
  return res;
}

ll Sum(int a, int b) {
  if (a < b) {
    return Sum(b) - Sum(a - 1);
  } else {
    return Sum(n) - Sum(a - 1) + Sum(b);
  }
}

ll solve() {
  vector<int> X;
  for (int i = 0; i < (int)A.size(); i++)
    X.push_back(A[i]);
  sort(X.begin(), X.end());

  size = 1;
  while (size < n + 5)
    size *= 2;
  bit.clear();
  bit.resize(size);
  tree.clear();
  tree.resize(size * 2);
  mp.clear();

  for (int i = 0; i < (int)A.size(); i++) {
    A[i] = lower_bound(X.begin(), X.end(), A[i]) - X.begin() + 1;
    Upd(i, i + 1, P(0, i));
    Add(i, 1);
    mp[A[i]] = i;
  }

  int p = -1;
  ll res = 0;
  for (int i = 1; i + 1 <= n; i++) {

    int num = mp[i];
    int target = Get(num);

    Add(num, -1);
    res += Sum(p, target);
    /*
    for(int j=0;j<n;j++){
      if(A[j]<i)cout<<'-';
      else cout<<A[j];
      cout<<' ';
    }
    cout<<endl;
    for(int j=0;j<n;j++)cout<<Get(j)<<' ';cout<<endl;

        cout<<i<<"  p="<<p<<"  target="<<target<<"  res="<<res<<endl;

    cout<<endl;
    */
    Upd(p, target, P(i, target));
    p = target;
  }
  return res;
}

int main() {
  while (cin >> n && n) {
    A.clear();
    A.resize(n);
    for (int i = 0; i < n; i++)
      cin >> A[i];
    cout << solve() << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

#define MAX 3000000
int n;
ll ans;

int solve(vector<int> t) {
  int res = 0;
  while (1) {
    int index = -1;
    for (int i = 0; i + 1 < (int)t.size(); i++) {
      if (t[i] > t[i + 1]) {
        index = i;
        break;
      }
    }
    if (index == -1)
      break;
    int num = t[index];
    t.erase(t.begin() + index);
    t.push_back(num);
    res++;
  }
  return res;
}

void input() {
  ans = 0;
  vector<int> X, t(n * n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &t[i]);
    X.push_back(t[i]);
  }
  sort(X.begin(), X.end());
  for (int i = 0; i < n; i++) {
    t[i] = lower_bound(X.begin(), X.end(), t[i]) - X.begin() + 1;
  }

  int K = 1;
  for (int i = 0; i < n; i++)
    if (t[i] == K)
      K++;

  stack<int> st;
  int cc = n;
  for (int i = 0; i < cc; i++) {

    while (!st.empty() && t[st.top()] > t[i]) {
      t[cc++] = t[st.top()];
      t[st.top()] = -1;
      ans++;
      if (t[cc - 1] == K)
        break;
      st.pop();
    }
    if (t[cc - 1] == K)
      break;
    st.push(i);
  }

  vector<int> tmp;
  for (int i = 0; i < cc; i++)
    if (t[i] != -1)
      tmp.push_back(t[i]);

  cout << ans + solve(tmp) << endl;
}

int main() {
  while (cin >> n && n) {
    input();
  }
  return 0;
}