Problem p01777 (C++)

Buggy submission (status: Runtime Error):
/* attention to overflow */
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define dump(x) cerr << #x << " = " << x << endl

using namespace std;
const int INF = 1 << 25;
void io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
/*printf("%.9Lf\n",cf);*/
const int MOD = 1000000007;

struct st {
  vector<int> sorted;
  vector<int> ns;
};

int main() {
  io();
  int n;
  cin >> n;
  vector<st> ar;
  int bak = (int)sqrt(n);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (i % bak == 0) {
      ar.push_back(st());
    }
    ar[i / bak].ns.push_back(x);
  }
  int size = ar.size();
  for (int i = 0; i < size; i++) {
    ar[i].sorted = ar[i].ns;
    sort(ar[i].sorted.begin(), ar[i].sorted.end());
  }

  /*for (int i = 0; i < ar.size(); i++) {
          for (int j = 0; j < ar[i].ns.size(); j++) {
                  cerr << ar[i].ns[j] << ' ';
          }
          cerr << endl;
  }*/

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int l, r, e;
    cin >> l >> r >> e;
    l--, r--;
    int f = l / bak;
    int s = r / bak;

    int down = min(ar[f].ns[l % bak], ar[s].ns[r % bak]) - e;
    int up = max(ar[f].ns[l % bak], ar[s].ns[r % bak]) + e;
    dump(down);
    dump(up);
    // cerr<<1<<endl;
    int cnt = 0;
    if (f != s) {
      for (int j = l % bak; j < ar[f].ns.size(); j++) {
        if (down > ar[f].ns[j] || up < ar[f].ns[j]) {
          cnt++;
        }
      }

      for (int j = 0; j < r % bak; j++) {
        if (down > ar[s].ns[j] || up < ar[s].ns[j]) {
          cnt++;
        }
      }

      for (int j = f + 1; j < s; j++) {
        cnt +=
            bak - (upper_bound(ar[j].sorted.begin(), ar[j].sorted.end(), up) -
                   lower_bound(ar[j].sorted.begin(), ar[j].sorted.end(), down));
      }
    } else {
      for (int j = l % bak; j < r % bak; j++) {
        if (down > ar[f].ns[j] || up < ar[f].ns[j]) {
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }

  return 0;
}

Fixed submission:
/* attention to overflow */
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define dump(x) cerr << #x << " = " << x << endl

using namespace std;
const int INF = 1 << 25;
void io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
/*printf("%.9Lf\n",cf);*/
const int MOD = 1000000007;

struct st {
  vector<int> sorted;
  vector<int> ns;
};

int main() {
  io();
  int n;
  cin >> n;
  vector<st> ar;
  int bak = (int)sqrt(n);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (i % bak == 0) {
      ar.push_back(st());
    }
    ar[i / bak].ns.push_back(x);
  }
  int size = ar.size();
  for (int i = 0; i < size; i++) {
    ar[i].sorted = ar[i].ns;
    sort(ar[i].sorted.begin(), ar[i].sorted.end());
  }

  /*for (int i = 0; i < ar.size(); i++) {
          for (int j = 0; j < ar[i].ns.size(); j++) {
                  cerr << ar[i].ns[j] << ' ';
          }
          cerr << endl;
  }*/

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int l, r, e;
    cin >> l >> r >> e;
    l--, r--;
    int f = l / bak;
    int s = r / bak;

    int down = min(ar[f].ns[l % bak], ar[s].ns[r % bak]) - e;
    int up = max(ar[f].ns[l % bak], ar[s].ns[r % bak]) + e;
    // dump(down);
    // dump(up);
    // cerr<<1<<endl;
    int cnt = 0;
    if (f != s) {
      for (int j = l % bak; j < ar[f].ns.size(); j++) {
        if (down > ar[f].ns[j] || up < ar[f].ns[j]) {
          cnt++;
        }
      }

      for (int j = 0; j < r % bak; j++) {
        if (down > ar[s].ns[j] || up < ar[s].ns[j]) {
          cnt++;
        }
      }

      for (int j = f + 1; j < s; j++) {
        cnt +=
            bak - (upper_bound(ar[j].sorted.begin(), ar[j].sorted.end(), up) -
                   lower_bound(ar[j].sorted.begin(), ar[j].sorted.end(), down));
      }
    } else {
      for (int j = l % bak; j < r % bak; j++) {
        if (down > ar[f].ns[j] || up < ar[f].ns[j]) {
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }

  return 0;
}