#include <bits/stdc++.h>
using namespace std;
struct piii {
  long long first, second, th;
  bool operator<(const piii &b) {
    if (first != b.first) return first < b.first;
    if (second != b.second) return second < b.second;
    return th < b.th;
  }
};
const int maxn = 100005;
int n, k, q, block;
int l[maxn], r[maxn], a[maxn], t[maxn], p[maxn], pos[maxn][3];
long long s[maxn], ans[maxn], cnt[maxn * 3];
long long res;
vector<long long> num;
void input() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> t[i];
  int val;
  for (int i = 1; i <= n; i++) {
    cin >> val;
    if (t[i] == 1)
      s[i] = s[i - 1] + val;
    else
      s[i] = s[i - 1] - val;
  }
  cin >> q;
  for (int i = 0; i < q; i++) cin >> l[i] >> r[i];
}
bool cmp(const int &a, const int &b) {
  if (l[a] / block == l[b] / block) return r[a] < r[b];
  return l[a] < l[b];
}
void init() {
  block = sqrt(n);
  for (int i = 0; i < q; i++) p[i] = i;
  sort(p, p + q, cmp);
  for (int i = 0; i <= n; i++) {
    num.push_back(s[i]);
    num.push_back(s[i] + k);
    num.push_back(s[i] - k);
  }
  sort(num.begin(), num.end());
  num.resize(unique(num.begin(), num.end()) - num.begin());
  for (int i = 0; i <= n; i++) {
    pos[i][0] = lower_bound(num.begin(), num.end(), s[i]) - num.begin();
    pos[i][1] = lower_bound(num.begin(), num.end(), s[i] + k) - num.begin();
    pos[i][2] = lower_bound(num.begin(), num.end(), s[i] - k) - num.begin();
  }
}
void pp() {
  int ll = -1, rr = 1;
  cnt[pos[0][0]]++;
  for (int i = 0; i < q; i++) {
    l[p[i]]--;
    while (ll < l[p[i]] - 1) {
      ll++;
      cnt[pos[ll][0]]--;
      res -= cnt[pos[ll][1]];
    }
    while (ll >= l[p[i]]) {
      res += cnt[pos[ll][1]];
      cnt[pos[ll][0]]++;
      ll--;
    }
    while (rr > r[p[i]] + 1) {
      rr--;
      cnt[pos[rr][0]]--;
      res -= cnt[pos[rr][2]];
    }
    while (rr <= r[p[i]]) {
      res += cnt[pos[rr][2]];
      cnt[pos[rr][0]]++;
      rr++;
    }
    ans[p[i]] = res;
  }
}
int main() {
  cout << setprecision(2) << fixed;
  ios::sync_with_stdio(false), cin.tie(0);
  input();
  init();
  pp();
  for (int i = 0; i < q; i++) cout << ans[i] << endl;
  return 0;
}
