Problem p03519 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define pb push_back
#define mp make_pair

using namespace std;

template <typename T> inline void upmin(T &x, T y) { y < x ? x = y : 0; }
template <typename T> inline void upmax(T &x, T y) { x < y ? x = y : 0; }

typedef unsigned int u32;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double lod;
typedef pair<int, int> PR;
typedef vector<int> VI;

const lod pi = acos(-1);
const int oo = 1 << 30;
const LL OO = 1e18;

const int N = 1e6;

int gi() {
  int w = 0;
  bool q = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-')
    c = getchar();
  if (c == '-')
    q = 0, c = getchar();
  while (c >= '0' && c <= '9')
    w = w * 10 + c - '0', c = getchar();
  return q ? w : -w;
}

LL a[N], b[N];
LL f[N], g[N];
#define lc (i << 1)
#define rc (lc | 1)
inline void modify(int i, int l, int r, int k) {
  if (l == r)
    f[i] = a[l], g[i] = b[l];
  else {
    int mid = (l + r) >> 1;
    k <= mid ? modify(lc, l, mid, k) : modify(rc, mid + 1, r, k);
    f[i] = max(f[lc] + f[rc], g[lc] + g[rc]);
    g[i] = max(f[lc] + g[rc], g[lc] + f[rc]);
  }
}
int main() {
  int n = gi() * 2, m = gi(), i, k;
  for (i = 1; i <= n; i++)
    a[i] = gi();
  for (i = 1; i <= n; i++)
    b[i] = gi();
  b[1] = b[n] = -OO;
  for (i = 1; i <= n; i++)
    modify(1, 1, n, i);
  while (m--) {
    k = gi(), a[k] = gi();
    if (1 < k && k < n)
      b[k] = gi();
    modify(1, 1, n, k);
    printf("%lld\n", f[1]);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define pb push_back
#define mp make_pair

using namespace std;

template <typename T> inline void upmin(T &x, T y) { y < x ? x = y : 0; }
template <typename T> inline void upmax(T &x, T y) { x < y ? x = y : 0; }

typedef unsigned int u32;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double lod;
typedef pair<int, int> PR;
typedef vector<int> VI;

const lod pi = acos(-1);
const int oo = 1 << 30;
const LL OO = 1e18;

const int N = 1e6;

int gi() {
  int w = 0;
  bool q = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-')
    c = getchar();
  if (c == '-')
    q = 0, c = getchar();
  while (c >= '0' && c <= '9')
    w = w * 10 + c - '0', c = getchar();
  return q ? w : -w;
}

LL a[N], b[N];
LL f[N], g[N];
#define lc (i << 1)
#define rc (lc | 1)
inline void modify(int i, int l, int r, int k) {
  if (l == r)
    f[i] = a[l], g[i] = b[l];
  else {
    int mid = (l + r) >> 1;
    k <= mid ? modify(lc, l, mid, k) : modify(rc, mid + 1, r, k);
    f[i] = max(f[lc] + f[rc], g[lc] + g[rc]);
    g[i] = max(f[lc] + g[rc], g[lc] + f[rc]);
  }
}
int main() {
  int n = gi() * 2, m = gi(), i, k;
  for (i = 1; i <= n; i++)
    a[i] = gi();
  for (i = 1; i <= n; i++)
    b[i] = gi();
  b[1] = b[n] = -OO;
  for (i = 1; i <= n; i++)
    modify(1, 1, n, i);
  while (m--) {
    k = gi(), a[k] = gi(), b[k] = gi();
    b[1] = b[n] = -OO;
    modify(1, 1, n, k);
    printf("%lld\n", f[1]);
  }
  return 0;
}