#include <bits/stdc++.h>
using namespace std;
map<long long, int> mp;
long long n, k, i, v[500005], val[500005], x, sum[500005], fr[500005],
    frec[500005], fr3[500005], nr, sum2[500005], bucket, val1, m, st, dr, s,
    stcur, drcur, sol[500005], fr2[500005], val2, b[500005];
struct wow {
  long long st, dr, poz;
} a[500005];
long long cautbin(long long x) {
  long long st = 0, dr = n, mij, sol = 0;
  while (st <= dr) {
    mij = (st + dr) / 2;
    if (sum2[mij] <= x) {
      if (sum2[mij] == x) {
        sol = mij;
      }
      st = mij + 1;
    } else {
      dr = mij - 1;
    }
  }
  return sol;
}
bool compare(wow a, wow b) {
  if (a.st / bucket != b.st / bucket) {
    return a.st < b.st;
  }
  return a.dr < b.dr;
}
int main() {
  cin >> n >> k;
  for (i = 1; i <= n; i++) {
    cin >> x;
    if (x == 1) {
      val[i] = 1;
    } else {
      val[i] = -1;
    }
  }
  for (i = 1; i <= n; i++) {
    cin >> x;
    v[i] = x * val[i];
  }
  for (i = 1; i <= n; i++) {
    sum[i] = sum[i - 1] + v[i];
  }
  for (i = 0; i <= n; i++) {
    mp[sum[i] - k] = mp[sum[i]] = mp[sum[i] + k] = 0;
  }
  nr = 0;
  for (auto &a : mp) {
    a.second = ++nr;
  }
  for (i = 0; i <= n; i++) {
    fr[i] = mp[sum[i] - k];
    fr2[i] = mp[sum[i]];
    fr3[i] = mp[sum[i] + k];
  }
  cin >> m;
  for (i = 1; i <= m; i++) {
    cin >> a[i].st >> a[i].dr;
    a[i].st--;
    a[i].poz = i;
  }
  bucket = 300;
  sort(a + 1, a + m + 1, compare);
  st = 0;
  dr = -1;
  s = 0;
  for (i = 1; i <= m; i++) {
    stcur = a[i].st;
    drcur = a[i].dr;
    while (st > stcur) {
      st--;
      s = s + frec[fr3[st]];
      frec[fr2[st]]++;
    }
    while (dr < drcur) {
      dr++;
      s = s + frec[fr[dr]];
      frec[fr2[dr]]++;
    }
    while (st < stcur) {
      frec[fr2[st]]--;
      s = s - frec[fr3[st]];
      st++;
    }
    while (dr > drcur) {
      frec[fr2[dr]]--;
      s = s - frec[fr[dr]];
      dr--;
    }
    sol[a[i].poz] = s;
  }
  for (i = 1; i <= m; i++) {
    cout << sol[i] << '\n';
  }
  return 0;
}
