Problem p03519 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

set<P> A, B;
ll sumA = 0;

ll N, Q;

ll a[100005];
ll b[100005];
ll tmp = 0;

ll funcA() {
  if (A.empty())
    return 0;
  return A.begin()->first;
}

ll funcB() {
  if (B.empty())
    return 0;
  auto it = B.end();
  it--;
  return it->first;
}

int main() {
  cin >> N >> Q;
  N *= 2;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    tmp += a[i];
  }
  for (int i = 0; i < N; i++)
    cin >> b[i];

  for (int i = 1; i + 1 < N; i++) {
    P p = P(b[i] - a[i], i);
    if (p.first > 0) {
      A.insert(p);
      sumA += p.first;
    } else {
      B.insert(p);
    }
  }

  for (int i = 0; i < Q; i++) {
    ll id, na, nb;
    cin >> id >> na >> nb;
    id--;

    if (id == 0 || id == N - 1) {
      tmp -= a[id];
      a[id] = na;
      tmp += a[id];
    } else {
      tmp -= a[id];
      tmp += na;
      P p = P(b[id] - a[id], id);
      a[id] = na;
      b[id] = nb;
      P np = P(b[id] - a[id], id);

      if (A.count(p)) {
        A.erase(p);
        sumA -= p.first;
      } else {
        B.erase(p);
      }

      if (np.first > 0) {
        A.insert(np);
        sumA += np.first;
      } else {
        B.insert(np);
      }
    }
    /*
    cout<<"////"<<endl;
    for(auto p : A)cout<<p.first<<' '<<p.second<<endl;
    cout<<"////"<<endl;
    */
    if (A.size() % 2 == 0) {
      cout << tmp + sumA << endl;
    } else if (!A.empty() && !B.empty() && funcA() + funcB() >= 0) {
      //      cout<<"X"<<endl;
      cout << tmp + sumA + funcB() << endl;
    } else {
      //      cout<<"Y"<<endl;
      cout << tmp + sumA - funcA() << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

set<P> A, B;
ll sumA = 0;

ll N, Q;

ll a[200005];
ll b[200005];
ll tmp = 0;

ll funcA() {
  if (A.empty())
    return 0;
  return A.begin()->first;
}

ll funcB() {
  if (B.empty())
    return 0;
  auto it = B.end();
  it--;
  return it->first;
}

int main() {
  cin >> N >> Q;
  N *= 2;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    tmp += a[i];
  }
  for (int i = 0; i < N; i++)
    cin >> b[i];

  for (int i = 1; i + 1 < N; i++) {
    P p = P(b[i] - a[i], i);
    if (p.first > 0) {
      A.insert(p);
      sumA += p.first;
    } else {
      B.insert(p);
    }
  }

  for (int i = 0; i < Q; i++) {
    ll id, na, nb;
    cin >> id >> na >> nb;
    id--;

    if (id == 0 || id == N - 1) {
      tmp -= a[id];
      a[id] = na;
      tmp += a[id];
    } else {
      tmp -= a[id];
      tmp += na;
      P p = P(b[id] - a[id], id);
      a[id] = na;
      b[id] = nb;
      P np = P(b[id] - a[id], id);

      if (A.count(p)) {
        A.erase(p);
        sumA -= p.first;
      } else {
        B.erase(p);
      }

      if (np.first > 0) {
        A.insert(np);
        sumA += np.first;
      } else {
        B.insert(np);
      }
    }
    /*
    cout<<"////"<<endl;
    for(auto p : A)cout<<p.first<<' '<<p.second<<endl;
    cout<<"////"<<endl;
    */
    if (A.size() % 2 == 0) {
      cout << tmp + sumA << endl;
    } else if (!A.empty() && !B.empty() && funcA() + funcB() >= 0) {
      //      cout<<"X"<<endl;
      cout << tmp + sumA + funcB() << endl;
    } else {
      //      cout<<"Y"<<endl;
      cout << tmp + sumA - funcA() << endl;
    }
  }
  return 0;
}