#include <bits/stdc++.h>
using namespace std;
int n, c = 0, q, t[100005];
long long k, a[100005], qs[100005];
long long lo[100005], hi[100005];
long long ans[100005], cnt[100005];
map<long long, int> m;
struct query {
  int id, l, r;
  bool operator<(query q) const { return r < q.r; }
};
vector<query> v[325];
int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &t[i]);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (t[i] == 2) {
      a[i] *= -1;
    }
    qs[i] = qs[i - 1] + a[i];
  }
  for (int i = 0; i <= n; i++) {
    m[qs[i]] = 0;
  }
  for (auto u : m) {
    m[u.first] = ++c;
  }
  for (int i = 0; i <= n; i++) {
    lo[i] = m[qs[i] - k];
    hi[i] = m[qs[i] + k];
    qs[i] = m[qs[i]];
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    v[(l - 1) / 320].push_back({i, l - 1, r});
  }
  for (int i = 0; i < 320; i++) {
    memset(cnt, 0, sizeof cnt);
    sort(v[i].begin(), v[i].end());
    int l = 320 * i;
    int r = l - 1;
    long long x = 0;
    for (auto u : v[i]) {
      while (r < u.r) {
        r++;
        x += cnt[lo[r]];
        cnt[qs[r]]++;
      }
      while (l > u.l) {
        l--;
        x += cnt[hi[l]];
        cnt[qs[l]]++;
      }
      while (l < u.l) {
        cnt[qs[l]]--;
        x -= cnt[hi[l]];
        l++;
      }
      ans[u.id] = x;
    }
  }
  for (int i = 1; i <= q; i++) {
    printf("%lld\n", ans[i]);
  }
  return 0;
}
