Problem p00487 (C++) — buggy submission, verdict: Runtime Error

// 初めてのBIT(本当
// テストケース28でWAする。(最適解より大きい値が出る)
#include <iostream>
#include <stdlib.h>
#include <vector>
#define int long long
using namespace std;

struct Bug {
  int a, b;
} bug[300001];
int n;
int a, b;
vector<int> uofB[100002];

signed compare(const void *s1, const void *s2) {
  return ((Bug *)s1)->a - ((Bug *)s2)->a;
}

class BIT {
public:
  int bit[600000]; // bit[i] = …
  BIT() {
    for (int i = 0; i <= 500000; i++)
      bit[i] = 0;
  }
  void add(int a, int w) {
    for (int x = a; x <= 500000; x += x & -x)
      bit[x] += w;
  }
  int sum(int a) {
    int ret = 0;
    for (int x = a; x > 0; x -= x & -x)
      ret += bit[x];
    return ret;
  }
  void insert(int a, int w) {
    int res = sum(a) - sum(a - 1);
    w = w - res;
    add(a, w);
  }
  int solvesum(int l, int r) { return sum(r) - sum(l - 1); }
};
BIT segfoo, segins;

signed main() {
  int i;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a >> b;
    bug[i].a = a;
    bug[i].b = b;
  }
  qsort(bug + 1, n, sizeof(bug[1]), compare);
  // cout << endl;
  // int debug = 0;
  for (i = 1; i <= n; i++) {
    // debug += bug[i].a;
    // cout << bug[i].a << " " << bug[i].b << " " << debug << endl;
    uofB[bug[i].b + 1].push_back(i);
    segins.insert(i, 1);
    segfoo.insert(i, bug[i].a);
  }

  int minB;
  int ans = 0;
  for (minB = 0; minB < 100002; minB++) {
    for (i = 0; i < uofB[minB].size(); i++) {
      segfoo.insert(uofB[minB][i], 0); // 微生物uofB[Minb][i]を無効化
      segins.insert(uofB[minB][i], 0);
    }
    /*cout << endl;
    cout << "Union = { ";
    for( i = 1; i <= n; i++ ){
            if( segins.solvesum(i,i) ){
                    cout << i << " ";
            }
    }
    cout << "}" << endl;*/

    int st = 1, ed = n + 1, medi;
    int sumFoo, k;
    while (st <= ed) {
      medi = st + ed;
      medi /= 2;
      sumFoo = segfoo.solvesum(1, medi);
      k = segins.solvesum(1, medi);
      // cout << "sumFoo = " << sumFoo << " minB = " << minB << " k = " << k <<
      // " medi = " << medi << endl;
      if (sumFoo > k * minB) {
        ed = medi - 1;
      } else {
        ans = max(ans, k);
        if (segfoo.solvesum(1, medi + 1) > segins.solvesum(1, medi + 1) * minB)
          break;
        else
          st = medi + 1;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

// 初めてのBIT(本当
#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;

struct Bug {
  int a, b;
} bug[30000];
int n;
int a, b;
vector<int> uofB[100000];

int compare(const void *s1, const void *s2) {
  return ((Bug *)s1)->a - ((Bug *)s2)->a;
}

class BIT {
public:
  int bit[500000]; // bit[i] = …
  BIT() {
    for (int i = 0; i < 500000; i++)
      bit[i] = 0;
  }
  void add(int a, int w) {
    for (int x = a; x <= 500000; x += x & -x)
      bit[x] += w;
  }
  int sum(int a) {
    int ret = 0;
    for (int x = a; x > 0; x -= x & -x)
      ret += bit[x];
    return ret;
  }
  void insert(int a, int w) {
    int res = sum(a) - sum(a - 1);
    w = w - res;
    add(a, w);
  }
  int solvesum(int l, int r) { return sum(r) - sum(l - 1); }
};
BIT segfoo, segins;

int main() {
  int i;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a >> b;
    bug[i].a = a;
    bug[i].b = b;
    uofB[b].push_back(i);
  }
  qsort(bug + 1, n, sizeof(bug[1]), compare);
  for (i = 1; i <= n; i++) {
    segins.insert(i, 1);
    segfoo.insert(i, bug[i].a);
  }

  int minB;
  int ans = -1;
  for (minB = 0; minB < 100001; minB++) {
    for (i = 0; i < uofB[minB].size(); i++) {
      segfoo.insert(uofB[minB][i], 0); // 微生物uofb[Minb][i]を無効化
      segins.insert(uofB[minB][i], 0);
    }
    int st = 1, ed = n + 1, medi;
    int sumFoo, k;
    while (st <= ed) {
      medi = st + ed;
      medi /= 2;
      sumFoo = segfoo.solvesum(1, medi);
      k = segins.solvesum(1, medi);
      if (sumFoo > k * minB) {
        ed = medi - 1;
      } else {
        if (segfoo.solvesum(1, medi + 1) > segins.solvesum(1, medi + 1) * minB)
          break;
        else
          st = medi + 1;
      }
    }
    ans = max(ans, k);
  }
  cout << ans << endl;
  return 0;
}