#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010, maxb = 320, MOD = 1e7 + 7;
struct Query {
  int l, r, id;
  bool operator<(const Query &b) const {
    if (l / maxb == b.l / maxb) return r < b.r;
    return l / maxb < b.l / maxb;
  }
} Q[maxn];
long long sum[maxn];
long long pre[maxn], nxt[maxn];
int M[maxn];
long long tmp[maxn], cnt;
int a[maxn];
int t[maxn];
long long ans[maxn];
int n, k, q;
int Find(long long x) {
  int res = upper_bound(tmp + 1, tmp + 1 + cnt, x) - (tmp + 1);
  if (tmp[res] == x) return res;
  return 0;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", &t[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    sum[i] = sum[i - 1] + ((t[i] == 1) ? a[i] : -a[i]);
  for (int i = 1; i <= n + 1; i++) tmp[i] = sum[i - 1];
  sort(tmp + 1, tmp + 2 + n);
  cnt = unique(tmp + 1, tmp + 2 + n) - (tmp + 1);
  for (int i = 0; i <= n; i++)
    pre[i] = Find(sum[i] - k), nxt[i] = Find(sum[i] + k), sum[i] = Find(sum[i]);
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) scanf("%d%d", &Q[i].l, &Q[i].r), Q[i].id = i;
  sort(Q + 1, Q + 1 + q);
  long long res = 0;
  M[sum[0]] = 1;
  for (int i = 1, l = 0, r = 0; i <= q; i++) {
    int ql = Q[i].l - 1, qr = Q[i].r;
    while (r < qr) ++r, res += M[pre[r]], M[sum[r]]++;
    while (l > ql) --l, res += M[nxt[l]], M[sum[l]]++;
    while (r > qr) M[sum[r]]--, res -= M[pre[r]], r--;
    while (l < ql) M[sum[l]]--, res -= M[nxt[l]], l++;
    ans[Q[i].id] = res;
  }
  for (int i = 1; i <= q; i++) printf("%I64d\n", ans[i]);
  return 0;
}
