Problem p03176 (C++)

Buggy submission (status: Runtime Error):

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

#define int long long
#define sp ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define cps CLOCKS_PER_SEC
#define mod (int)(1000000007)
#define f first
#define s second
#define debug1(x) cerr << x << "\n"
#define debug2(x, y) cerr << x << " " << y << "\n"
#define debug3(x, y, z) cerr << x << " " << y << " " << z << "\n"
#define nl cerr << "\n";
#define pq priority_queue
#define inf 9999999999999999
#define test cerr << "abcd\n";
#define pi pair<int, int>
#define pii pair<int, pi>
#define pb push_back
#define mxn 100005
#define mxnn 2000005
int hei[mxn], tree[mxn << 2], val[mxn], dp[mxn];
void update(int i, int tl, int tr, int pos, int val) {
  if (tl > tr || tl > pos || tr < pos)
    return;
  if (tl == tr) {
    tree[i] = val;
    return;
  }
  int mid = (tl + tr) >> 1;
  if (mid >= pos)
    update(i + i, tl, mid, pos, val);
  else
    update(i + i + 1, mid + 1, tr, pos, val);
  tree[i] = max(tree[i + i], tree[i + i + 1]);
}

int query(int i, int tl, int tr, int l, int r) {
  if (tl > tr || tl > r || tr < l)
    return 0;
  if (tl >= l && tr <= r)
    return tree[i];
  int mid = (tl + tr) >> 1;
  return max(query(i + i, tl, mid, l, r), query(i + i + 1, mid + 1, tr, l, r));
}

int32_t main() {
  sp;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> hei[i];
  for (int i = 1; i <= n; i++)
    cin >> val[i];
  int mx = 0;
  for (int i = 1; i <= n; i++)
    mx = max(mx, hei[i]);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int x = hei[i];
    int a = query(1, 0, mx, 1, x);
    ans = max(ans, a + val[i]);
    update(1, 0, mx, x, a + val[i]);
  }
  cout << ans;
  return 0;
}


Fixed submission:

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

#define int long long
#define sp ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define cps CLOCKS_PER_SEC
#define mod (int)(1000000007)
#define f first
#define s second
#define debug1(x) cerr << x << "\n"
#define debug2(x, y) cerr << x << " " << y << "\n"
#define debug3(x, y, z) cerr << x << " " << y << " " << z << "\n"
#define nl cerr << "\n";
#define pq priority_queue
#define inf 9999999999999999
#define test cerr << "abcd\n";
#define pi pair<int, int>
#define pii pair<int, pi>
#define pb push_back
#define mxn 200005
#define mxnn 2000005
int hei[mxn], tree[mxn << 2], val[mxn], dp[mxn];
void update(int i, int tl, int tr, int pos, int val) {
  if (tl > tr || tl > pos || tr < pos)
    return;
  if (tl == tr) {
    tree[i] = val;
    return;
  }
  int mid = (tl + tr) >> 1;
  if (mid >= pos)
    update(i + i, tl, mid, pos, val);
  else
    update(i + i + 1, mid + 1, tr, pos, val);
  tree[i] = max(tree[i + i], tree[i + i + 1]);
}

int query(int i, int tl, int tr, int l, int r) {
  if (tl > tr || tl > r || tr < l)
    return 0;
  if (tl >= l && tr <= r)
    return tree[i];
  int mid = (tl + tr) >> 1;
  return max(query(i + i, tl, mid, l, r), query(i + i + 1, mid + 1, tr, l, r));
}

int32_t main() {
  sp;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> hei[i];
  for (int i = 1; i <= n; i++)
    cin >> val[i];
  int mx = 0;
  for (int i = 1; i <= n; i++)
    mx = max(mx, hei[i]);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int x = hei[i];
    int a = query(1, 0, mx, 1, x);
    ans = max(ans, a + val[i]);
    update(1, 0, mx, x, a + val[i]);
  }
  cout << ans;
  return 0;
}