#include <bits/stdc++.h>
using namespace std;
map<long long int, int> mp;
int re[100022], ad[100022], cou[100022];
long long int arr[100022], val[100022], aa[100022], ansa[100022];
struct node {
  int l, r, in;
} q[100222];
int cmp(node a, node b) {
  if (a.l / 333 != b.l / 333) return a.l / 333 < b.l / 333;
  return a.r < b.r;
}
long long int ans = 0;
void add1(int in) {
  if (ad[in] != -1) ans += cou[ad[in]];
  cou[aa[in]]++;
}
void add2(int in) {
  if (re[in] != -1) ans += cou[re[in]];
  cou[aa[in]]++;
}
void rem1(int in) {
  cou[aa[in]]--;
  if (ad[in] != -1) ans -= cou[ad[in]];
}
void rem2(int in) {
  cou[aa[in]]--;
  if (re[in] != -1) ans -= cou[re[in]];
}
int main() {
  int i, n, k;
  scanf("%d%d", &n, &k);
  for (i = 1; i <= n; i++) scanf("%lld", &arr[i]);
  for (i = 1; i <= n; i++) scanf("%lld", &val[i]);
  long long int one = 0, two = 0;
  mp[one - two]++;
  for (i = 1; i <= n; i++) {
    if (arr[i] == 1)
      one += val[i];
    else
      two += val[i];
    mp[one - two]++;
    aa[i] = one - two;
  }
  int aay = 0;
  for (map<long long int, int>::iterator it = mp.begin(); it != mp.end(); it++)
    mp[it->first] = aay++;
  for (i = 0; i <= n; i++) {
    re[i] = ad[i] = -1;
    if (mp.find(aa[i] + k) != mp.end()) re[i] = mp[aa[i] + k];
    if (mp.find(aa[i] - k) != mp.end()) ad[i] = mp[aa[i] - k];
    aa[i] = mp[aa[i]];
  }
  int m;
  cin >> m;
  for (i = 0; i < m; i++) {
    scanf("%d%d", &q[i].l, &q[i].r);
    q[i].in = i;
  }
  sort(q, q + m, cmp);
  int l = 0, r = 0;
  for (i = 0; i < m; i++) {
    int nl = q[i].l - 1, nr = q[i].r;
    while (l > nl) add2(--l);
    while (r <= nr) add1(r++);
    while (l < nl) rem2(l++);
    while (r > nr + 1) rem1(--r);
    ansa[q[i].in] = ans;
  }
  for (i = 0; i < m; i++) printf("%lld\n", ansa[i]);
  return 0;
}
