Problem p03176 (C++)

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

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define MP make_pair
#define PB push_back
#define X first
#define Y second

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define ITER(it, a)                                                            \
  for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)((a).size())
#define FILL(a, value) memset(a, value, sizeof(a))
#define debug(a) cout << #a << " = " << a << endl;

const double PI = acos(-1.0);
const LL INF = 1e9;
const LL LINF = INF * INF;

const int MAX = 1 << 18;

LL t[MAX];
int n;
pair<PII, int> a[MAX];

void a1(int v, int tl, int tr, int pos, LL add) {
  if (tl == tr)
    t[v] += add;
  else {
    int tm = (tl + tr) >> 1;
    if (pos <= tm)
      a1(v + v, tl, tm, pos, add);
    else
      a1(v + v + 1, tm + 1, tr, pos, add);
    t[v] = max(t[v + v], t[v + v + 1]);
  }
}

LL get(int v, int tl, int tr, int l, int r) {
  if (l == tl && r == tr)
    return t[v];
  if (l > r || tl > tr)
    return 0;
  int tm = (tl + tr) >> 1;
  return max(get(v + v, tl, tm, l, min(tm, r)),
             get(v + v + 1, tm + 1, tr, max(tm + 1, l), r));
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // freopen("In.txt", "r", stdin);
  // freopen("In.txt", "w", stdout);

  cin >> n;
  FOR(i, 0, n)
  cin >> a[i].X.X, a[i].Y = i;
  FOR(i, 0, n)
  cin >> a[i].X.Y;
  sort(a, a + n);

  FOR(i, 0, n) {
    int pl = a[i].Y;
    // debug(pl);
    LL que = get(1, 0, n - 1, 0, pl);
    a1(1, 0, n - 1, pl, que + a[i].X.Y);
  }

  cout << get(1, 0, n - 1, 0, n - 1) << endl;
  cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl;
  return 0;
}


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

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define MP make_pair
#define PB push_back
#define X first
#define Y second

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define ITER(it, a)                                                            \
  for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)((a).size())
#define FILL(a, value) memset(a, value, sizeof(a))
#define debug(a) cout << #a << " = " << a << endl;

const double PI = acos(-1.0);
const LL INF = 1e9;
const LL LINF = INF * INF;

const int MAX = 1 << 20;

LL t[MAX];
int n;
pair<PII, int> a[MAX];

void a1(int v, int tl, int tr, int pos, LL add) {
  if (tl == tr)
    t[v] += add;
  else {
    int tm = (tl + tr) >> 1;
    if (pos <= tm)
      a1(v + v, tl, tm, pos, add);
    else
      a1(v + v + 1, tm + 1, tr, pos, add);
    t[v] = max(t[v + v], t[v + v + 1]);
  }
}

LL get(int v, int tl, int tr, int l, int r) {
  if (l == tl && r == tr)
    return t[v];
  if (l > r || tl > tr)
    return 0;
  int tm = (tl + tr) >> 1;
  return max(get(v + v, tl, tm, l, min(tm, r)),
             get(v + v + 1, tm + 1, tr, max(tm + 1, l), r));
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // freopen("In.txt", "r", stdin);
  // freopen("In.txt", "w", stdout);

  cin >> n;
  FOR(i, 0, n)
  cin >> a[i].X.X, a[i].Y = i;
  FOR(i, 0, n)
  cin >> a[i].X.Y;
  sort(a, a + n);

  FOR(i, 0, n) {
    int pl = a[i].Y;
    // debug(pl);
    LL que = get(1, 0, n - 1, 0, pl);
    a1(1, 0, n - 1, pl, que + a[i].X.Y);
  }

  cout << get(1, 0, n - 1, 0, n - 1) << endl;
  cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl;
  return 0;
}