Problem p03176 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, j, k, in) for (int i = j; i < k; i += in)
#define RFOR(i, j, k, in) for (int i = j; i >= k; i -= in)
#define REP(i, j) FOR(i, 0, j, 1)
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define MP make_pair
#define PB push_back
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
const double pi = acos(-1.0);
typedef pair<int, int> PII;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;

#define int long long
#define trace(x) cerr << #x << ": " << x << " " << endl;

inline int add(int a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
  return a;
}
inline int sub(int a, int b) {
  a -= b;
  if (a < 0)
    a += MOD;
  return a;
}
inline int mul(int a, int b) { return (int)((int)a * b % MOD); }

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

#define N 2 * 10000
int h[2 * 10000 + 5];
int v[2 * 10000 + 5];
int dp[2 * 10000 + 5];

int segtree[4 * (N + 1)];

int query_segtree(int si, int l, int r, int ql, int qr) {
  if (l > qr || r < ql)
    return LONG_LONG_MIN;
  if (ql <= l && qr >= r)
    return segtree[si];
  auto t1 = query_segtree(2 * si, l, (l + r) / 2, ql, qr);
  auto t2 = query_segtree(2 * si + 1, (l + r) / 2 + 1, r, ql, qr);
  return max(t1, t2);
}

void update_segtree(int si, int l, int r, int ql, int s) {
  if (l > ql || r < ql)
    return;
  if (ql == l && l == r) {
    segtree[si] = s;
    return;
  }
  update_segtree(2 * si, l, (l + r) / 2, ql, s);
  update_segtree(2 * si + 1, (l + r) / 2 + 1, r, ql, s);
  auto t1 = segtree[2 * si];
  auto t2 = segtree[2 * si + 1];
  segtree[si] = max(t1, t2);
}

signed main() {
  IOS map<int, int> ind;
  map<int, int> rmap;
  int n;
  cin >> n;
  REP(i, n)
  cin >> h[i];
  REP(i, n)
  cin >> v[i];

  int mans = 0;
  REP(i, n) {
    int ret = query_segtree(1, 1, n, 1, h[i] - 1);

    if (ret <= 0) {
      dp[i] = v[i];
      update_segtree(1, 1, n, h[i], dp[i]);
    } else {
      dp[i] = ret + v[i];
      update_segtree(1, 1, n, h[i], dp[i]);
    }
    mans = max(mans, dp[i]);
    // cout << i << " " << dp[i] << "\n";
  }
  cout << mans;
}

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, j, k, in) for (int i = j; i < k; i += in)
#define RFOR(i, j, k, in) for (int i = j; i >= k; i -= in)
#define REP(i, j) FOR(i, 0, j, 1)
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define MP make_pair
#define PB push_back
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
const double pi = acos(-1.0);
typedef pair<int, int> PII;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;

#define int long long
#define trace(x) cerr << #x << ": " << x << " " << endl;

inline int add(int a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
  return a;
}
inline int sub(int a, int b) {
  a -= b;
  if (a < 0)
    a += MOD;
  return a;
}
inline int mul(int a, int b) { return (int)((int)a * b % MOD); }

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

#define N 2 * 100000 + 1
int h[2 * 100000 + 5];
int v[2 * 100000 + 5];
int dp[2 * 100000 + 5];

int segtree[4 * (N + 1)];

int query_segtree(int si, int l, int r, int ql, int qr) {
  if (l > qr || r < ql)
    return LONG_LONG_MIN;
  if (ql <= l && qr >= r)
    return segtree[si];
  auto t1 = query_segtree(2 * si, l, (l + r) / 2, ql, qr);
  auto t2 = query_segtree(2 * si + 1, (l + r) / 2 + 1, r, ql, qr);
  return max(t1, t2);
}

void update_segtree(int si, int l, int r, int ql, int s) {
  if (l > ql || r < ql)
    return;
  if (ql == l && l == r) {
    segtree[si] = s;
    return;
  }
  update_segtree(2 * si, l, (l + r) / 2, ql, s);
  update_segtree(2 * si + 1, (l + r) / 2 + 1, r, ql, s);
  auto t1 = segtree[2 * si];
  auto t2 = segtree[2 * si + 1];
  segtree[si] = max(t1, t2);
}

signed main() {
  IOS map<int, int> ind;
  map<int, int> rmap;
  int n;
  cin >> n;
  REP(i, n)
  cin >> h[i];
  REP(i, n)
  cin >> v[i];

  int mans = 0;
  REP(i, n) {
    int ret = query_segtree(1, 1, n, 1, h[i] - 1);

    if (ret <= 0) {
      dp[i] = v[i];
      update_segtree(1, 1, n, h[i], dp[i]);
    } else {
      dp[i] = ret + v[i];
      update_segtree(1, 1, n, h[i], dp[i]);
    }
    mans = max(mans, dp[i]);
    // cout << i << " " << dp[i] << "\n";
  }
  cout << mans;
}