#include <bits/stdc++.h>
using namespace std;
const int MN = 1e5 + 20, SQ = 333;
int n, q, k, L, R;
short tmp[MN];
long long Ans = 0;
long long arr[MN], ans[MN];
int Nxt[MN], Prv[MN], cnt[MN];
long long uni[MN];
int un;
struct query {
  int l, r, id;
} qr[MN];
bool cmp(const query &a, const query &b) {
  if (a.l / SQ != b.l / SQ) return a.l < b.l;
  return a.r < b.r;
}
void prep() {
  for (int i = 0; i < n; ++i) uni[un++] = arr[i];
  sort(uni, uni + un);
  un = unique(uni, uni + un) - uni;
  for (int i = 0; i < un; ++i) {
    int ind = lower_bound(uni, uni + un, uni[i] + k) - uni;
    if (ind != un && uni[ind] == uni[i] + k)
      Nxt[i] = ind;
    else
      Nxt[i] = -1;
    ind = lower_bound(uni, uni + un, uni[i] - k) - uni;
    if (ind != un && uni[ind] == uni[i] - k)
      Prv[i] = ind;
    else
      Prv[i] = -1;
  }
  for (int i = 0; i < n; ++i) arr[i] = lower_bound(uni, uni + un, arr[i]) - uni;
}
int main() {
  ios_base ::sync_with_stdio(false), cout.tie(0), cin.tie(0);
  cin >> n >> k;
  long long cur = 0;
  for (int i = 0; i < n; ++i) {
    cin >> tmp[i];
  }
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    if (tmp[i] == 1)
      cur += x;
    else
      cur -= x;
    arr[i] = cur;
  }
  prep();
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> qr[i].l >> qr[i].r;
    qr[i].id = i;
    qr[i].l--;
  }
  sort(qr, qr + q, cmp);
  for (int i = 0; i < q; ++i) {
    while (R < qr[i].r) {
      if (~Prv[arr[R]]) Ans += cnt[Prv[arr[R]]];
      ++cnt[arr[R]];
      ++R;
    }
    while (L > qr[i].l) {
      --L;
      if (~Nxt[arr[L]]) Ans += cnt[Nxt[arr[L]]];
      ++cnt[arr[L]];
    }
    while (R > qr[i].r) {
      --R;
      --cnt[arr[R]];
      if (~Prv[arr[R]]) Ans -= cnt[Prv[arr[R]]];
    }
    while (L < qr[i].l) {
      --cnt[arr[L]];
      if (~Nxt[arr[L]]) Ans -= cnt[Nxt[arr[L]]];
      ++L;
    }
    if (L) {
      int X = arr[L - 1];
      if (~Nxt[X])
        ans[qr[i].id] = Ans + cnt[Nxt[X]];
      else
        ans[qr[i].id] = Ans;
    } else {
      int ind = lower_bound(uni, uni + un, k) - uni;
      if (ind != un && uni[ind] == k)
        ans[qr[i].id] = Ans + cnt[ind];
      else
        ans[qr[i].id] = Ans;
    }
  }
  for (int i = 0; i < q; ++i) cout << ans[i] << '\n';
  return 0;
}
