Problem p03519 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>

using namespace std;
#define MAXN 100000
#define ll long long
#define INFLL 0x3f3f3f3f3f3f3f3fll
#define rint register int
#define gc() getchar()
inline int read(rint ans = 0, rint sgn = ' ', rint ch = gc()) {
  for (; ch < '0' || ch > '9'; sgn = ch, ch = gc())
    ;
  for (; ch >= '0' && ch <= '9'; (ans *= 10) += ch - '0', ch = gc())
    ;
  return sgn - '-' ? ans : -ans;
}
#define BUF_SIZE 1000000
#define _END fwrite(_Ob, 1, _O - _Ob, stdout), _O = _Ob
#define Outc(a) (*_O++ = a)
char _Ob[BUF_SIZE + 5], *_O = _Ob, _Os[25], *_Ot;
template <typename T> inline void Out(T x) {
  if (!x) {
    Outc('0');
    return;
  }
  if (x < 0)
    Outc('-'), x = -x;
  for (_Ot = _Os; x; *_Ot++ = x % 10 + '0', x /= 10)
    ;
  for (; _Ot != _Os; Outc(*--_Ot))
    ;
  if (_O - _Ob >= BUF_SIZE - 50)
    _END;
}
int n, q, a[MAXN + 5], b[MAXN + 5];
multiset<int> Pos, Neg;
multiset<int>::iterator it;
ll base, sum;
int main() {
  n = read() << 1, q = read();
  for (rint i = 1; i <= n; base += (a[i++] = read()))
    ;
  for (rint i = 1; i <= n; b[i++] = read())
    ;
  for (rint i = 2; i < n; b[i] >= a[i] ? sum += b[i] - a[i],
            Pos.insert(b[i] - a[i])    : Neg.insert(b[i] - a[i]), i++)
    ;
  for (rint p; q--;
       Out(base + (Pos.size() & 1
                       ? max(sum + (Neg.size() ? *--Neg.end() : INFLL),
                             sum - *Pos.begin())
                       : sum)),
       Outc('\n')) {
    p = read(), base -= a[p],
    p > 1 &&p < n ? it = (b[p] >= a[p]          ? sum -= b[p] - a[p],
                          Pos.find(b[p] - a[p]) : Neg.find(b[p] - a[p])),
    (b[p] >= a[p] ? Pos : Neg).erase(it), 0 : 0;
    a[p] = read(), b[p] = read(), base += a[p],
    p > 1 && p < n ? b[p] >= a[p] ? sum += b[p] - a[p],
    Pos.insert(b[p] - a[p]) : Neg.insert(b[p] - a[p]), 0 : 0;
  }
  _END;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>

using namespace std;
#define MAXN 200000
#define ll long long
#define INFLL 0x3f3f3f3f3f3f3f3fll
#define rint register int
#define gc() getchar()
inline int read(rint ans = 0, rint sgn = ' ', rint ch = gc()) {
  for (; ch < '0' || ch > '9'; sgn = ch, ch = gc())
    ;
  for (; ch >= '0' && ch <= '9'; (ans *= 10) += ch - '0', ch = gc())
    ;
  return sgn - '-' ? ans : -ans;
}
#define BUF_SIZE 1000000
#define _END fwrite(_Ob, 1, _O - _Ob, stdout), _O = _Ob
#define Outc(a) (*_O++ = a)
char _Ob[BUF_SIZE + 5], *_O = _Ob, _Os[25], *_Ot;
template <typename T> inline void Out(T x) {
  if (!x) {
    Outc('0');
    return;
  }
  if (x < 0)
    Outc('-'), x = -x;
  for (_Ot = _Os; x; *_Ot++ = x % 10 + '0', x /= 10)
    ;
  for (; _Ot != _Os; Outc(*--_Ot))
    ;
  if (_O - _Ob >= BUF_SIZE - 50)
    _END;
}
int n, q, a[MAXN + 5], b[MAXN + 5];
multiset<int> Pos, Neg;
multiset<int>::iterator it;
ll base, sum;
int main() {
  n = read() << 1, q = read();
  for (rint i = 1; i <= n; base += (a[i++] = read()))
    ;
  for (rint i = 1; i <= n; b[i++] = read())
    ;
  for (rint i = 2; i < n; b[i] >= a[i] ? sum += b[i] - a[i],
            Pos.insert(b[i] - a[i])    : Neg.insert(b[i] - a[i]), i++)
    ;
  for (rint p; q--;
       Out(base + (Pos.size() & 1
                       ? max(sum + (Neg.size() ? *--Neg.end() : INFLL),
                             sum - *Pos.begin())
                       : sum)),
       Outc('\n')) {
    p = read(), base -= a[p],
    p > 1 &&p < n ? it = (b[p] >= a[p]          ? sum -= b[p] - a[p],
                          Pos.find(b[p] - a[p]) : Neg.find(b[p] - a[p])),
    (b[p] >= a[p] ? Pos : Neg).erase(it), 0 : 0;
    a[p] = read(), b[p] = read(), base += a[p],
    p > 1 && p < n ? b[p] >= a[p] ? sum += b[p] - a[p],
    Pos.insert(b[p] - a[p]) : Neg.insert(b[p] - a[p]), 0 : 0;
  }
  _END;
  return 0;
}