Problem p00099 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
typedef vector<vector<int>> VVI;
const int INF = 1e9;
typedef pair<int, int> PII;

const int MAX = 1e6;
const int B = 1000;

int nums[MAX];
PII bucket[MAX / B];

int N;

void update(int n, int value) {
  nums[n] += value;

  bucket[n / B] = PII(-INF, -1);
  for (int i = (n / B) * B; i < min(N, (n / B + 1) * B); i++) {
    if (bucket[n / B].first < nums[i]) {
      bucket[n / B].first = nums[i];
      bucket[n / B].second = i;
    }
  }
}

int main() {
  int Q;
  cin >> N >> Q;

  memset(nums, 0, sizeof(nums));
  memset(bucket, 0, sizeof(bucket));

  for (int q = 0; q < Q; q++) {
    int a, v;
    cin >> a >> v;

    a--;
    update(a, v);

    PII ans(-INF, -1);
    for (int i = 0; i <= N / B; i++) {
      if (ans.first < bucket[i].first) {
        ans.first = bucket[i].first;
        ans.second = bucket[i].second;
      }
    }

    cout << ans.second + 1 << " " << ans.first << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

const int INF = 1e9;

using namespace std;

typedef pair<int, int> PII;

const int MAX = 1e8;
const int B = 1000;

int nums[MAX];
int bucket[MAX / B];

void update(int n, int value) {
  nums[n] += value;
  bucket[n / B] = max(nums[n], bucket[n / B]);
}

PII get_max(int l, int r) {
  PII res(-MAX, -1);
  int L = l / B, R = r / B;

  if (L != R) {
    for (int i = l; i < (L + 1) * B; i++) {
      if (res.first < nums[i]) {
        res.first = nums[i];
        res.second = i;
      }
    }
    for (int i = (L + 1) * B; i < R * B; i++) {
      if (res.first < nums[i]) {
        res.first = nums[i];
        res.second = i;
      }
    }
    for (int i = R * B; i < r; i++) {
      if (res.first < nums[i]) {
        res.first = nums[i];
        res.second = i;
      }
    }
  } else {
    for (int i = l; i < r; i++) {
      if (res.first < nums[i]) {
        res.first = nums[i];
        res.second = i;
      }
    }
  }
  return res;
}

int main() {
  int N, Q;
  cin >> N >> Q;

  memset(nums, 0, sizeof(nums));
  memset(bucket, 0, sizeof(bucket));

  for (int q = 0; q < Q; q++) {
    int a, v;
    cin >> a >> v;

    update(a, v);
    cout << get_max(0, N).second << " " << get_max(0, N).first << endl;
  }
}