#include <bits/stdc++.h>
using namespace std;
struct query {
  int l, r, id;
};
map<long long, int> mp;
int n, q, t[100005], b[100005], g[100005], nxt[100005], pre[100005];
long long k, ss[100005], d[100005], f[100005], ans[100005], s = 0;
query a[100005];
int cmp(query x, query y) {
  return b[x.l] != b[y.l] ? b[x.l] < b[y.l] : x.r < y.r;
}
void delfst(int x) {
  int p = ss[x], tmp;
  --g[p];
  tmp = nxt[p];
  if (tmp != -1) s = s - g[tmp];
}
void dellst(int x) {
  int p = ss[x], tmp;
  --g[p];
  tmp = pre[p];
  if (tmp != -1) s = s - g[tmp];
}
void addfst(int x) {
  int p = ss[x], tmp;
  tmp = nxt[p];
  if (tmp != -1) s = s + g[tmp];
  ++g[p];
}
void addlst(int x) {
  int p = ss[x], tmp;
  tmp = pre[p];
  if (tmp != -1) s = s + g[tmp];
  ++g[p];
}
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", &f[i]);
  for (int i = 1; i <= n; i++)
    if (t[i] == 1)
      ss[i] = ss[i - 1] + f[i];
    else
      ss[i] = ss[i - 1] - f[i];
  for (int i = 1; i <= n; i++) d[i] = ss[i];
  stable_sort(d + 1, d + n + 1);
  long long lst = -1;
  int cnt = 0;
  for (int i = 0; i <= n; i++)
    if (d[i] != lst) mp[d[i]] = ++cnt, lst = d[i];
  for (int i = 0; i <= n; i++) {
    long long x = ss[i];
    int tmp = mp[x];
    pre[tmp] = (mp[x - k] ? mp[x - k] : -1);
    nxt[tmp] = (mp[x + k] ? mp[x + k] : -1);
    ss[i] = tmp;
  }
  for (int i = 0; i <= n; i++) b[i] = i / 100;
  scanf("%d", &q);
  for (int i = 1; i <= q; i++)
    scanf("%d%d", &a[i].l, &a[i].r), a[i].id = i, --a[i].l;
  stable_sort(a + 1, a + q + 1, cmp);
  int l = 0, r = -1;
  for (int i = 1; i <= q; i++) {
    int ll = a[i].l, rr = a[i].r, id = a[i].id;
    while (r < rr) addlst(++r);
    while (ll < l) addfst(--l);
    while (l < ll) delfst(l++);
    while (rr < r) dellst(r--);
    ans[id] = s;
  }
  for (int i = 1; i <= q; i++) printf("%lld\n", ans[i]);
  return 0;
}
