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

#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>

using namespace std;

const long long mod = 1000000007;

struct Cat {
  int c, w;
  int which;
  Cat() {}
  Cat(int a, int b, int x) : c(a), w(b), which(x) {}
};

bool operator<(const Cat &c1, const Cat &c2) { return c1.c < c2.c; }

vector<Cat> cats;
int N;

long long dp[2][10100];
long long tmp[10100];
int W;

void ins(Cat cat) {
  int w = cat.w, c = cat.c;
  int id = cat.which;
  //	printf("ins %d %d %d\\n",c,w,id);
  for (int i = 0; i <= W; i++) {
    tmp[i] = 0;
  }
  for (int i = 0; i <= W; i++) {
    tmp[i] = (tmp[i] + dp[id][i]) % mod;
    ;
    if (i + w <= W)
      tmp[i + w] = (tmp[i + w] + dp[id][i]) % mod;
  }
  for (int i = 0; i <= W; i++)
    dp[id][i] = tmp[i];
  //	for(int i=0;i<=W;i++) printf("%d ",dp[id][i]);
  //	printf("\\n");
}

void del(Cat cat) {
  int w = cat.w, c = cat.c;
  int id = cat.which;
  //	printf("del %d %d %d\\n",c,w,id);
  for (int i = 0; i <= W; i++)
    tmp[i] = 0;
  for (int i = 0; i <= W; i++) {
    tmp[i] = dp[id][i];
    if (i - w >= 0) {
      //	tmp[i]-=tmp[i-w];
      tmp[i] = (tmp[i] - tmp[i - w]) % mod;
    }
  }
  for (int i = 0; i <= W; i++)
    dp[id][i] = tmp[i];
  //	for(int i=0;i<=W;i++) printf("%d ",dp[id][i]);
  //	printf("\\n");
}

long long sum[2][10100];
bool check2(int D) {
  //	printf("CHECK\\n");
  for (int i = 0; i < 2; i++) {
    sum[i][0] = dp[i][0];
    for (int j = 1; j <= W; j++) {
      sum[i][j] = sum[i][j - 1] + dp[i][j];
      sum[i][j] %= mod;
    }
  }
  for (int x = 1; x <= W; x++) {
    if (dp[0][x] == 0)
      continue;
    int l = max(1, x - D);
    int r = min(W, x + D);
    //	if(sum[1][r]-sum[1][l-1]>0){
    if (sum[1][r] != sum[1][l - 1]) {
      //			printf("ok %d\\n",x);
      return true;
    }
  }
  return false;
}

bool check(int D) {
  //	printf("check %d\\n",D);
  int le = 0, ri = 0;
  for (int i = 0; i <= W; i++) {
    dp[0][i] = 0;
    dp[1][i] = 0;
  }
  dp[0][0] = 1, dp[1][0] = 1;
  for (; ri < N; ri++) {
    ins(cats[ri]);
    for (; cats[ri].c - cats[le].c > D; le++) {
      //		printf("le=%d,dif=%d\\n",le,cats[ri].c-cats[le].c);
      del(cats[le]);
    }
    bool flg = check2(D);
    if (flg)
      return true;
  }
  return false;
}

int main() {
  int NA, NB;
  scanf("%d%d%d", &NA, &NB, &W);
  for (int i = 0; i < NA; i++) {
    int m, c;
    scanf("%d%d", &m, &c);
    if (m > W)
      continue;
    cats.push_back(Cat(c, m, 0));
  }
  for (int i = 0; i < NB; i++) {
    int m, c;
    scanf("%d%d", &m, &c);
    if (m > W)
      continue;
    cats.push_back(Cat(c, m, 1));
  }
  sort(cats.begin(), cats.end());
  N = cats.size();
  int lb = -1, ub = 1010010010;
  while (ub - lb > 1) {
    int mid = (ub + lb) / 2;
    bool flg = check(mid);
    if (flg)
      ub = mid;
    else
      lb = mid;
  }
  printf("%d\\n", ub);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>

using namespace std;

const long long mod = 1000000007;

struct Cat {
  int c, w;
  int which;
  Cat() {}
  Cat(int a, int b, int x) : c(a), w(b), which(x) {}
};

bool operator<(const Cat &c1, const Cat &c2) { return c1.c < c2.c; }

vector<Cat> cats;
int N;

long long dp[2][10100];
long long tmp[10100];
int W;

void ins(Cat cat) {
  int w = cat.w, c = cat.c;
  int id = cat.which;
  //	printf("ins %d %d %d\\n",c,w,id);
  for (int i = 0; i <= W; i++) {
    tmp[i] = 0;
  }
  for (int i = 0; i <= W; i++) {
    tmp[i] += dp[id][i];
    if (i + w <= W)
      tmp[i + w] += dp[id][i];
  }
  for (int i = 0; i <= W; i++)
    dp[id][i] = tmp[i];
  //	for(int i=0;i<=W;i++) printf("%d ",dp[id][i]);
  //	printf("\\n");
}

void del(Cat cat) {
  int w = cat.w, c = cat.c;
  int id = cat.which;
  //	printf("del %d %d %d\\n",c,w,id);
  for (int i = 0; i <= W; i++)
    tmp[i] = 0;
  for (int i = 0; i <= W; i++) {
    tmp[i] = dp[id][i];
    if (i - w >= 0) {
      tmp[i] -= tmp[i - w];
    }
  }
  for (int i = 0; i <= W; i++)
    dp[id][i] = tmp[i];
  //	for(int i=0;i<=W;i++) printf("%d ",dp[id][i]);
  //	printf("\\n");
}

long long sum[2][10100];
bool check2(int D) {
  //	printf("CHECK\\n");
  for (int i = 0; i < 2; i++) {
    sum[i][0] = dp[i][0];
    for (int j = 1; j <= W; j++) {
      sum[i][j] = sum[i][j - 1] + dp[i][j];
    }
  }
  for (int x = 1; x <= W; x++) {
    if (dp[0][x] == 0)
      continue;
    int l = max(1, x - D);
    int r = min(W, x + D);
    if (sum[1][r] - sum[1][l - 1] > 0) {
      //			printf("ok %d\\n",x);
      return true;
    }
  }
  return false;
}

bool check(int D) {
  //	printf("check %d\\n",D);
  int le = 0, ri = 0;
  for (int i = 0; i <= W; i++) {
    dp[0][i] = 0;
    dp[1][i] = 0;
  }
  dp[0][0] = 1, dp[1][0] = 1;
  for (; ri < N; ri++) {
    ins(cats[ri]);
    for (; cats[ri].c - cats[le].c > D; le++) {
      //		printf("le=%d,dif=%d\\n",le,cats[ri].c-cats[le].c);
      del(cats[le]);
    }
    bool flg = check2(D);
    if (flg)
      return true;
  }
  return false;
}

int main() {
  int NA, NB;
  scanf("%d%d%d", &NA, &NB, &W);
  N = NA + NB;
  for (int i = 0; i < NA; i++) {
    int m, c;
    scanf("%d%d", &m, &c);
    if (m > W)
      continue;
    cats.push_back(Cat(c, m, 0));
  }
  for (int i = 0; i < NB; i++) {
    int m, c;
    scanf("%d%d", &m, &c);
    if (m > W)
      continue;
    cats.push_back(Cat(c, m, 1));
  }
  sort(cats.begin(), cats.end());
  int lb = -1, ub = 1010010010;
  while (ub - lb > 1) {
    int mid = (ub + lb) / 2;
    bool flg = check(mid);
    if (flg)
      ub = mid;
    else
      lb = mid;
  }
  printf("%d\\n", ub);
  return 0;
}