Problem p00487 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "stdio.h"
#include <algorithm>
#include <functional>
#include <iostream>

using namespace std;

#define DEEP 19

long long int gfb[(1 << DEEP)];
long long int mkb[(1 << DEEP)];

void adb(long long int *a, int b, long long int x) {
  int n = b + 1;
  while (n != (1 << DEEP)) {
    a[n - 1] += x;
    n += (n & (-n));
  }
}

long long int wa(long long int *a, int b) {
  long long int w = 0;
  int n = b + 1;
  while (n != 0) {
    w += a[n - 1];
    n -= (n & (-n));
  }
  return w;
}

pair<long long int, int> aw[(1 << DEEP)];
int at[(1 << DEEP)];
pair<long long int, int> bw[(1 << DEEP)];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int aq, bq;
    scanf("%d %d", &aq, &bq);
    aw[i] = make_pair(aq, i);
    bw[i] = make_pair(bq, i);
  }
  sort(aw, aw + n);
  for (int i = 0; i < n; i++) {
    at[aw[i].second] = i;
  }
  sort(bw, bw + n, greater<pair<long long int, int>>());
  bw[n] = make_pair(-1, -1);
  for (int i = 0; i < (1 << DEEP); i++) {
    gfb[i] = 0;
    mkb[i] = 0;
  }

  int na = 0;
  int max = 0;
  for (int i = 0; i < n; i++) {
    adb(gfb, at[bw[i].second], aw[at[bw[i].second]].first);
    adb(mkb, at[bw[i].second], 1);
    if (bw[i].first == bw[i + 1].first) {
      continue;
    }

    long long int minf = bw[i].first;
    if (wa(gfb, na) <= minf * wa(mkb, na)) {
      while (1) {
        if (na >= n - 1) {
          if (max < wa(mkb, na) && wa(gfb, na) <= minf * wa(mkb, na)) {
            max = wa(mkb, na);
          }
          break;
        }

        if (wa(gfb, na + 1) > minf * wa(mkb, na + 1)) {
          if (max < wa(mkb, na) && wa(gfb, na) <= minf * wa(mkb, na)) {
            max = wa(mkb, na);
          }
          break;
        }
        na++;
      }
    } else {
      while (1) {
        if (na <= 0) {
          if (max < wa(mkb, na) && wa(gfb, na) <= minf * wa(mkb, na)) {
            max = wa(mkb, na);
          }
          break;
        }

        if (wa(gfb, na) <= minf * wa(mkb, na)) {
          if (max < wa(mkb, na) && wa(gfb, na) <= minf * wa(mkb, na)) {
            max = wa(mkb, na);
          }
          break;
        }
        na--;
      }
    }
  }
  printf("%d\\n", max);
  return 0;
}

Error: TLE

Fixed submission:

#include "stdio.h"
#include <algorithm>
#include <functional>
#include <iostream>

using namespace std;

#define DEEP 19

long long int gfb[(1 << DEEP)];
long long int mkb[(1 << DEEP)];

void adb(long long int *a, int b, long long int x) {
  int n = b + 1;
  while (n) {
    a[n - 1] += x;
    n -= (n & (-n));
  }
}

long long int wa(long long int *a, int b) {
  long long int w = 0;
  int n = b + 1;
  while (b != (1 << DEEP)) {
    w += a[n - 1];
    n += (n & (-n));
  }
  return w;
}

pair<long long int, int> aw[(1 << DEEP)];
int at[(1 << DEEP)];
pair<long long int, int> bw[(1 << DEEP)];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int aq, bq;
    scanf("%d %d", &aq, &bq);
    aw[i] = make_pair(aq, i);
    bw[i] = make_pair(bq, i);
  }
  sort(aw, aw + n);
  for (int i = 0; i < n; i++) {
    at[aw[i].second] = i;
  }
  sort(bw, bw + n, greater<pair<long long int, int>>());
  for (int i = 0; i < (1 << DEEP); i++) {
    gfb[i] = 0;
    mkb[i] = 0;
  }

  int na = 0;
  int max = 0;
  for (int i = 0; i < n; i++) {
    adb(gfb, at[bw[i].second], aw[at[bw[i].second]].first);
    adb(mkb, at[bw[i].second], 1);
    if (bw[i].first == bw[i + 1].first) {
      continue;
    }

    int minf = bw[i].first;
    if (wa(gfb, na) <= minf * wa(mkb, na)) {
      while (1) {
        if (na >= n - 1) {
          if (max < wa(mkb, na)) {
            max = wa(mkb, na);
          }
          break;
        }

        if (wa(gfb, na + 1) > minf * wa(mkb, na + 1)) {
          if (max < wa(mkb, na)) {
            max = wa(mkb, na);
          }
          break;
        }
        na++;
      }
    } else {
      while (1) {
        if (na <= 0) {
          if (max < wa(mkb, na)) {
            max = wa(mkb, na);
          }
          break;
        }

        if (wa(gfb, na - 1) > minf * wa(mkb, na - 1)) {
          if (max < wa(mkb, na)) {
            max = wa(mkb, na);
          }
          break;
        }
        na--;
      }
    }
  }
  printf("%d\\n", max);
  return 0;
}