Problem p03176 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define md 1000000007
#define mx 1e18
#define pb push_back
#define endl '\n'
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define pii pair<ll int, ll int>
#define ff first
#define ss second

ll int n;
ll int h[200001], b[200001];
ll int dp[200001];
ll int tree[600001];

void update(ll int i, ll int v, ll int s, ll int e, ll int index) {
  if (i < s || i > e) {
    return;
  }

  if (s == e && s == i) {
    tree[index] = v;
    return;
  }

  ll int mid = (s + e) / 2;

  update(i, v, s, mid, 2 * index);
  update(i, v, mid + 1, e, 2 * index + 1);

  tree[index] = max(tree[2 * index], tree[2 * index + 1]);
}

ll int query(ll int l, ll int r, ll int s, ll int e, ll int index) {
  if (r < s || l > e) {
    return INT_MIN;
  }

  if (s >= l && e <= r) {
    return tree[index];
  }

  ll int mid = (s + e) / 2;

  return max(query(l, r, s, mid, 2 * index),
             query(l, r, mid + 1, e, 2 * index + 1));
}

int main() {
  fastio
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> n;
  for (ll int i = 0; i < n; i++)
    cin >> h[i];
  for (ll int i = 0; i < n; i++)
    cin >> b[i];

  memset(dp, 0, sizeof(dp));
  memset(tree, 0, sizeof(tree));

  ll int ans = 0;

  for (ll int i = 0; i < n; i++) {
    ll int ht = h[i];

    ll int q = query(0, ht - 1, 0, n - 1, 1);

    dp[ht] = q + b[i];

    ans = max(ans, dp[ht]);

    update(ht, q + b[i], 0, n - 1, 1);
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define md 1000000007
#define mx 1e18
#define pb push_back
#define endl '\n'
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define pii pair<ll int, ll int>
#define ff first
#define ss second

ll int n;
ll int h[200001], b[200001];
ll int dp[200001];
ll int tree[600001];

void update(ll int i, ll int v, ll int s, ll int e, ll int index) {
  if (i < s || i > e) {
    return;
  }

  if (s == e && s == i) {
    tree[index] = v;
    return;
  }

  ll int mid = (s + e) / 2;

  update(i, v, s, mid, 2 * index);
  update(i, v, mid + 1, e, 2 * index + 1);

  tree[index] = max(tree[2 * index], tree[2 * index + 1]);
}

ll int query(ll int l, ll int r, ll int s, ll int e, ll int index) {
  if (r < s || l > e) {
    return INT_MIN;
  }

  if (s >= l && e <= r) {
    return tree[index];
  }

  ll int mid = (s + e) / 2;

  return max(query(l, r, s, mid, 2 * index),
             query(l, r, mid + 1, e, 2 * index + 1));
}

int main() {
  // 	fastio
  // 	#ifndef ONLINE_JUDGE
  // 	freopen("input.txt" , "r" , stdin);
  // 	freopen("output.txt" , "w" , stdout);
  // 	#endif

  cin >> n;
  for (ll int i = 0; i < n; i++)
    cin >> h[i];
  for (ll int i = 0; i < n; i++)
    cin >> b[i];

  memset(dp, 0, sizeof(dp));
  memset(tree, 0, sizeof(tree));

  ll int ans = 0;

  for (ll int i = 0; i < n; i++) {
    ll int ht = h[i];

    ll int q = query(0, ht - 1, 0, n - 1, 1);

    dp[ht] = q + b[i];

    ans = max(ans, dp[ht]);

    update(ht, q + b[i], 0, n - 1, 1);
  }

  cout << ans << endl;

  return 0;
}