Problem p03040 (C++)

Buggy submission (status: Runtime Error):
#include <float.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#ifdef __cplusplus
#include <bits/stdc++.h>
#endif

int comp(const void *a, const void *b) {
  return (*(int *)a > *(int *)b) - (*(int *)a < *(int *)b);
}
int compr(const void *a, const void *b) {
  return (*(int *)a < *(int *)b) - (*(int *)a > *(int *)b);
}

uint32_t nextpint(void) {
  char c = getchar();
  while (c < '0' || '9' < c)
    c = getchar();
  uint32_t x = 0;
  while ('0' <= c && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x;
}
uint64_t nextplong(void) {
  char c = getchar();
  while (c < '0' || '9' < c)
    c = getchar();
  uint64_t x = 0;
  while ('0' <= c && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x;
}
uint32_t nextstr(char *s) {
  char c = getchar();
  while (c == ' ' || c == '\n')
    c = getchar();
  uint32_t len = 0;
  while (c != ' ' && c != '\n') {
    *s++ = c;
    len++;
    c = getchar();
  }
  *s = '\0';
  return len;
}

inline int max2(const int a, const int b) { return a > b ? a : b; }
inline int min2(const int a, const int b) { return a > b ? b : a; }
inline int dif2(const int a, const int b) { return a > b ? a - b : b - a; }
inline int abs2(const int a) { return a >= 0 ? a : -a; }

std::multiset<int> r;
std::multiset<int, std::greater<int>> l;

int main(void) {
  int q = nextpint();
  int L = INT_MIN;
  int R = INT_MAX;
  l.insert(L);
  r.insert(R);
  long b = 0;
  long x = 0;
  for (int i = 0; i < q; i++) {
    int Q = nextpint();
    if (Q == 1) {
      int A, B;
      scanf("%d%d", &A, &B);
      b += B;
      if (A < L) {
        l.erase(l.find(L));
        r.insert(L);
        l.insert(A);
        l.insert(A);
        x += L - A;
        R = L;
        L = *l.begin();
      } else if (A > R) {
        r.erase(r.find(R));
        l.insert(L);
        r.insert(A);
        r.insert(A);
        x += A - R;
        L = R;
        R = *r.begin();
      } else {
        l.insert(A);
        r.insert(A);
        L = R = A;
      }
    } else {
      printf("%d %ld\n", L, x + b);
    }
  }
}


Fixed submission:
#include <float.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#ifdef __cplusplus
#include <bits/stdc++.h>
#endif

int comp(const void *a, const void *b) {
  return (*(int *)a > *(int *)b) - (*(int *)a < *(int *)b);
}
int compr(const void *a, const void *b) {
  return (*(int *)a < *(int *)b) - (*(int *)a > *(int *)b);
}

uint32_t nextpint(void) {
  char c = getchar();
  while (c < '0' || '9' < c)
    c = getchar();
  uint32_t x = 0;
  while ('0' <= c && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x;
}
uint64_t nextplong(void) {
  char c = getchar();
  while (c < '0' || '9' < c)
    c = getchar();
  uint64_t x = 0;
  while ('0' <= c && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x;
}
uint32_t nextstr(char *s) {
  char c = getchar();
  while (c == ' ' || c == '\n')
    c = getchar();
  uint32_t len = 0;
  while (c != ' ' && c != '\n') {
    *s++ = c;
    len++;
    c = getchar();
  }
  *s = '\0';
  return len;
}

inline int max2(const int a, const int b) { return a > b ? a : b; }
inline int min2(const int a, const int b) { return a > b ? b : a; }
inline int dif2(const int a, const int b) { return a > b ? a - b : b - a; }
inline int abs2(const int a) { return a >= 0 ? a : -a; }

std::multiset<int> r;
std::multiset<int, std::greater<int>> l;

int main(void) {
  int q = nextpint();
  int L = INT_MIN;
  int R = INT_MAX;
  l.insert(L);
  r.insert(R);
  long b = 0;
  long x = 0;
  for (int i = 0; i < q; i++) {
    int Q = nextpint();
    if (Q == 1) {
      int A, B;
      scanf("%d%d", &A, &B);
      b += B;
      if (A < L) {
        l.erase(l.find(L));
        r.insert(L);
        l.insert(A);
        l.insert(A);
        x += L - A;
        R = L;
        L = *l.begin();
      } else if (A > R) {
        r.erase(r.find(R));
        l.insert(R);
        r.insert(A);
        r.insert(A);
        x += A - R;
        L = R;
        R = *r.begin();
      } else {
        l.insert(A);
        r.insert(A);
        L = R = A;
      }
    } else {
      printf("%d %ld\n", L, x + b);
    }
  }
}