Problem p00099 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;

struct DATA {
  int player, wakasagi;
};

int n, q, a, v, top_player, cnt = 0;
int wakasagi_sum[1000001];
DATA data[1000001];

void heep_in(int);
int heep_out(void);

int main() {

  cin >> n >> q;

  for (int i = 0; i < 1000001; i++) {
    data[i].wakasagi = -1;
    data[i].player = 1e9;
    wakasagi_sum[i] = 0;
  }
  for (int i = 0; i < q; i++) {

    cin >> a >> v;

    wakasagi_sum[a] += v;

    heep_in(a);

    top_player = heep_out();

    cout << top_player << " " << wakasagi_sum[top_player] << endl;
  }

  return 0;
}
void heep_in(int a) {

  int number;

  number = ++cnt;

  data[cnt].player = a;
  data[cnt].wakasagi = wakasagi_sum[a];

  while (1) {

    if (number == 1)
      break;

    if (data[number].wakasagi > data[number / 2].wakasagi) {
      swap(data[number].wakasagi, data[number / 2].wakasagi);
      swap(data[number].player, data[number / 2].player);
    } else if (data[number].wakasagi == data[number / 2].wakasagi &&
               data[number].player < data[number / 2].player) {
      swap(data[number].player, data[number / 2].player);
    } else {
      break;
    }

    number /= 2;
  }
}
int heep_out(void) {

  int number, i;
  number = cnt;

  if (data[1].wakasagi == wakasagi_sum[data[1].player]) {
    return data[1].player;
  } else {

    data[1].wakasagi = data[cnt].wakasagi;
    data[1].player = data[cnt].player;
    i = 1;

    while (1) {

      if (data[i * 2].wakasagi > data[i * 2 + 1].wakasagi ||
          (data[i * 2].wakasagi == data[i * 2 + 1].wakasagi &&
           data[i * 2].player <= data[i * 2 + 1].player)) {

        if (data[i * 2].wakasagi > data[i].wakasagi) {
          swap(data[i].wakasagi, data[i * 2].wakasagi);
          swap(data[i].player, data[i * 2].player);
          i = i * 2;
        } else if (data[i * 2].wakasagi == data[i].wakasagi &&
                   data[i * 2].player <= data[i].player) {
          swap(data[i].player, data[i * 2].player);
          i = i * 2;
        }

      } else if (data[i * 2].wakasagi < data[i * 2 + 1].wakasagi ||
                 (data[i * 2].wakasagi == data[i * 2 + 1].wakasagi &&
                  data[i * 2].player > data[i * 2 + 1].player)) {

        if (data[i * 2 + 1].wakasagi > data[i].wakasagi) {
          swap(data[i].wakasagi, data[i * 2 + 1].wakasagi);
          swap(data[i].player, data[i * 2 + 1].player);
          i = i * 2 + 1;
        } else if (data[i * 2 + 1].wakasagi == data[i].wakasagi &&
                   data[i * 2 + 1].player <= data[i].player) {
          swap(data[i].player, data[i * 2 + 1].player);
          i = i * 2 + 1;
        } else
          break;

      } else {
        break;
      }

      if (i * 2 > cnt)
        break;
    }
    cnt--;
  }
  return heep_out();
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

struct DATA {
  int player, wakasagi;
};

int n, q, a, v, top_player, cnt = 0;
int wakasagi_sum[1000001];
DATA data[1000001];

void heep_in(int);
int heep_out(void);

int main() {

  cin >> n >> q;

  for (int i = 0; i < 1000001; i++) {
    data[i].wakasagi = -1;
    data[i].player = 1e9;
    wakasagi_sum[i] = 0;
  }
  for (int i = 0; i < q; i++) {

    cin >> a >> v;

    wakasagi_sum[a] += v;

    heep_in(a);

    top_player = heep_out();

    cout << top_player << " " << wakasagi_sum[top_player] << endl;
  }

  return 0;
}
void heep_in(int a) {

  int number;

  number = ++cnt;

  data[cnt].player = a;
  data[cnt].wakasagi = wakasagi_sum[a];

  while (1) {

    if (number == 1)
      break;

    if (data[number].wakasagi > data[number / 2].wakasagi) {
      swap(data[number].wakasagi, data[number / 2].wakasagi);
      swap(data[number].player, data[number / 2].player);
    } else if (data[number].wakasagi == data[number / 2].wakasagi &&
               data[number].player < data[number / 2].player) {
      swap(data[number].player, data[number / 2].player);
    } else {
      break;
    }

    number /= 2;
  }
}
int heep_out(void) {

  int number, i;
  number = cnt;

  if (data[1].wakasagi == wakasagi_sum[data[1].player]) {
    return data[1].player;
  } else {

    data[1].wakasagi = data[cnt].wakasagi;
    data[1].player = data[cnt].player;
    i = 1;

    while (1) {

      if (data[i * 2].wakasagi > data[i * 2 + 1].wakasagi ||
          (data[i * 2].wakasagi == data[i * 2 + 1].wakasagi &&
           data[i * 2].player <= data[i * 2 + 1].player)) {

        if (data[i * 2].wakasagi > data[i].wakasagi) {
          swap(data[i].wakasagi, data[i * 2].wakasagi);
          swap(data[i].player, data[i * 2].player);
          i = i * 2;
        } else if (data[i * 2].wakasagi == data[i].wakasagi &&
                   data[i * 2].player <= data[i].player) {
          swap(data[i].player, data[i * 2].player);
          i = i * 2;
        } else
          break;

      } else if (data[i * 2].wakasagi < data[i * 2 + 1].wakasagi ||
                 (data[i * 2].wakasagi == data[i * 2 + 1].wakasagi &&
                  data[i * 2].player > data[i * 2 + 1].player)) {

        if (data[i * 2 + 1].wakasagi > data[i].wakasagi) {
          swap(data[i].wakasagi, data[i * 2 + 1].wakasagi);
          swap(data[i].player, data[i * 2 + 1].player);
          i = i * 2 + 1;
        } else if (data[i * 2 + 1].wakasagi == data[i].wakasagi &&
                   data[i * 2 + 1].player <= data[i].player) {
          swap(data[i].player, data[i * 2 + 1].player);
          i = i * 2 + 1;
        } else
          break;

      } else {
        break;
      }

      if (i * 2 > cnt)
        break;
    }
    cnt--;
  }
  return heep_out();
}