#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
struct node {
  int l, r, blk, id;
  friend bool operator<(node a, node b) {
    return a.blk == b.blk ? a.r < b.r : a.l < b.l;
  }
} p[maxn];
int cnt[maxn], n, m, l = 1, r, up[maxn], dn[maxn], bc, t[maxn], sn = 256;
long long a[maxn], ans, sum[maxn], k;
map<long long, int> maps;
void revise(int x, bool p, bool v) {
  if (v)
    ans += p ? cnt[dn[x]] : cnt[up[x]], cnt[int(a[x])]++;
  else
    cnt[int(a[x])]--, ans -= p ? cnt[dn[x]] : cnt[up[x]];
}
void md(int i) {
  while (r < p[i].r) revise(++r, 1, 1);
  while (l > p[i].l) revise(--l, 0, 1);
  while (r > p[i].r) revise(r--, 1, 0);
  while (l < p[i].l) revise(l++, 0, 0);
  sum[p[i].id] = ans;
}
int main() {
  scanf("%d%lld", &n, &k);
  for (register int i = 1; i <= n; i++) scanf("%d", &t[i]);
  maps[0] = bc = 1;
  for (register int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (t[i] == 2) a[i] = -a[i];
    a[i] += a[i - 1];
    if (!maps[a[i]]) maps[a[i]] = ++bc;
  }
  for (register int i = 0; i <= n; i++) {
    up[i] = maps[a[i] + k];
    dn[i] = maps[a[i] - k];
    a[i] = maps[a[i]];
  }
  scanf("%d", &m);
  for (register int i = 1; i <= m; i++)
    scanf("%d%d", &p[i].l, &p[i].r), p[i].l--, p[i].blk = p[i].l / sn,
                                               p[i].id = i;
  sort(p + 1, p + m + 1);
  for (register int i = 1; i <= m; i++) md(i);
  for (register int i = 1; i <= m; i++) printf("%lld\n", sum[i]);
  return 0;
}
