Problem p03176 (C++)

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

// #include "rubo.h"
// #define mp make_pair
// #define pb push_back
#define in int
#define ll long long
#define vc vector
#define SQ(j) (j) * (j)
// #define x first
// #define y second

// #define ld long double
#define dbl long double
#define pll pair<long long, long long>
#define pii pair<int, int>
#define all(j) j.begin(), j.end()
#define loop(xxx, yyy) for (int xxx = 0; xxx < yyy; xxx++)
// #define printf(fmt, ...) (0)
// #define HOME
// #define y0 ngacaleiebinvoaeu
// #define y1 gnarpipipaigare
#define j1 adsfndnasfafoasp

#define db(x) cout << #x << " = " << x << endl
#define dbCont(x)                                                              \
  cout << #x << ": ";                                                          \
  for (auto shun : x)                                                          \
    cout << shun << ' ';                                                       \
  cout << endl;

typedef long double ld;

using namespace std;

const int N = 100 * 1000 + 123;
const int MOD = 1000 * 1000 * 1000 + 7;

int n;
map<int, int> a;
int h[N];

ll t[4 * N];

ll get_mx(int v, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr)
    return t[v];

  if (ql > r || l > qr)
    return 0;

  int mid = (l + r) / 2;
  return max(get_mx(2 * v + 1, l, mid, ql, qr),
             get_mx(2 * v + 2, mid + 1, r, ql, qr));
}

ll upd(int v, int l, int r, int pos, ll val) {
  if (l == r) {
    t[v] = val;
  } else {

    int mid = (l + r) / 2;
    if (pos <= mid)
      upd(2 * v + 1, l, mid, pos, val);
    else
      upd(2 * v + 2, mid + 1, r, pos, val);

    t[v] = max(t[2 * v + 1], t[2 * v + 2]);
  }
}

int main() {
  cin >> n;
  loop(i, n) { cin >> h[i]; }
  loop(i, n) { cin >> a[h[i]]; }

  vector<pii> v;
  loop(i, n) { v.push_back(make_pair(h[i], i)); }
  sort(all(v));

  for (auto p : v) {
    int he = p.first;
    int i = p.second;

    //        db(i);
    ll mx = get_mx(0, 0, n - 1, 0, i);

    upd(0, 0, n - 1, i, mx + a[he]);
  }

  ll res = t[0];
  cout << res << endl;

  return 0;
}


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

// #include "rubo.h"
// #define mp make_pair
// #define pb push_back
#define in int
#define ll long long
#define vc vector
#define SQ(j) (j) * (j)
// #define x first
// #define y second

// #define ld long double
#define dbl long double
#define pll pair<long long, long long>
#define pii pair<int, int>
#define all(j) j.begin(), j.end()
#define loop(xxx, yyy) for (int xxx = 0; xxx < yyy; xxx++)
// #define printf(fmt, ...) (0)
// #define HOME
// #define y0 ngacaleiebinvoaeu
// #define y1 gnarpipipaigare
#define j1 adsfndnasfafoasp

#define db(x) cout << #x << " = " << x << endl
#define dbCont(x)                                                              \
  cout << #x << ": ";                                                          \
  for (auto shun : x)                                                          \
    cout << shun << ' ';                                                       \
  cout << endl;

typedef long double ld;

using namespace std;

const int N = 200 * 1000 + 123;
const int MOD = 1000 * 1000 * 1000 + 7;

int n;
map<int, int> a;
int h[N];

ll t[4 * N];

ll get_mx(int v, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr)
    return t[v];

  if (ql > r || l > qr)
    return 0;

  int mid = (l + r) / 2;
  return max(get_mx(2 * v + 1, l, mid, ql, qr),
             get_mx(2 * v + 2, mid + 1, r, ql, qr));
}

ll upd(int v, int l, int r, int pos, ll val) {
  if (l == r) {
    t[v] = val;
  } else {

    int mid = (l + r) / 2;
    if (pos <= mid)
      upd(2 * v + 1, l, mid, pos, val);
    else
      upd(2 * v + 2, mid + 1, r, pos, val);

    t[v] = max(t[2 * v + 1], t[2 * v + 2]);
  }
}

int main() {
  cin >> n;
  loop(i, n) { cin >> h[i]; }
  loop(i, n) { cin >> a[h[i]]; }

  vector<pii> v;
  loop(i, n) { v.push_back(make_pair(h[i], i)); }
  sort(all(v));

  for (auto p : v) {
    int he = p.first;
    int i = p.second;

    //        db(i);
    ll mx = get_mx(0, 0, n - 1, 0, i);

    upd(0, 0, n - 1, i, mx + a[he]);
  }

  ll res = t[0];
  cout << res << endl;

  return 0;
}