Problem p03519 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for (llint(x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for (llint(x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define mod 998244353
using namespace std;
typedef pair<llint, llint> P;
typedef pair<llint, P> E;

llint n, Q;
llint a[200005], b[200005];
multiset<llint> posS, negS;
llint psum;

llint del(multiset<llint> &S, llint x) {
  auto it = S.lower_bound(x);
  if (it == S.end() || *it != x)
    return 0;
  S.erase(it);
  return x;
}

llint calc() {
  if (posS.size() % 2 == 0)
    return psum;
  llint ret = psum - *posS.begin();
  ret = max(ret, psum + *negS.rbegin());
  return ret;
}

int main(void) {
  cin >> n >> Q;
  for (int i = 1; i <= 2 * n; i++)
    cin >> a[i];
  for (int i = 1; i <= 2 * n; i++)
    cin >> b[i];

  llint ans = 0;
  for (int i = 1; i <= 2 * n; i++) {
    ans += a[i];
    if (i > 1 && i < 2 * n) {
      if (b[i] - a[i] >= 0)
        posS.insert(b[i] - a[i]), psum += b[i] - a[i];
      else
        negS.insert(b[i] - a[i]);
    }
  }

  llint p, x, y;
  for (int q = 1; q <= Q; q++) {
    cin >> p >> x >> y;
    ans -= a[p];
    if (p > 1 && p < 2 * n) {
      psum -= del(posS, b[p] - a[p]);
      del(negS, b[p] - a[p]);
    }
    a[p] = x, b[p] = y;
    ans += a[p];
    if (b[p] - a[p] >= 0)
      posS.insert(b[p] - a[p]), psum += b[p] - a[p];
    else
      negS.insert(b[p] - a[p]);

    cout << ans + calc() << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for (llint(x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for (llint(x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define mod 998244353
using namespace std;
typedef pair<llint, llint> P;
typedef pair<llint, P> E;

llint n, Q;
llint a[200005], b[200005];
multiset<llint> posS, negS;
llint psum;

llint del(multiset<llint> &S, llint x) {
  auto it = S.lower_bound(x);
  if (it == S.end() || *it != x)
    return 0;
  S.erase(it);
  return x;
}

llint calc() {
  if (posS.size() % 2 == 0)
    return psum;
  llint ret = psum - *posS.begin();
  ret = max(ret, psum + *negS.rbegin());
  return ret;
}

int main(void) {
  cin >> n >> Q;
  for (int i = 1; i <= 2 * n; i++)
    cin >> a[i];
  for (int i = 1; i <= 2 * n; i++)
    cin >> b[i];

  llint ans = 0;
  for (int i = 1; i <= 2 * n; i++) {
    ans += a[i];
    if (i > 1 && i < 2 * n) {
      if (b[i] - a[i] >= 0)
        posS.insert(b[i] - a[i]), psum += b[i] - a[i];
      else
        negS.insert(b[i] - a[i]);
    }
  }

  llint p, x, y;
  for (int q = 1; q <= Q; q++) {
    cin >> p >> x >> y;
    ans -= a[p];
    if (p > 1 && p < 2 * n) {
      psum -= del(posS, b[p] - a[p]);
      del(negS, b[p] - a[p]);
    }
    a[p] = x, b[p] = y;
    ans += a[p];
    if (p > 1 && p < 2 * n) {
      if (b[p] - a[p] >= 0)
        posS.insert(b[p] - a[p]), psum += b[p] - a[p];
      else
        negS.insert(b[p] - a[p]);
    }
    cout << ans + calc() << endl;
  }

  return 0;
}