#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10, sq = 320, maxt = 3e5 + 30;
int n, k, q, A[maxn], cnt[maxt], ci[maxn], cp[maxn], cm[maxn];
long long ans[maxn], s, sum[maxn];
struct query {
  int l, r, index;
};
query p[maxn];
bool cmp(query a, query b) {
  if (a.l / sq != b.l / sq) {
    return a.l / sq < b.l / sq;
  }
  return a.r > b.r;
}
int cl, cr;
void add_left();
void add_right();
void add_left() {
  cl--;
  s += cnt[cp[cl]];
  cnt[ci[cl]]++;
}
void del_left() {
  cnt[ci[cl]]--;
  s -= cnt[cp[cl]];
  cl++;
  if (cl > cr) {
    add_right();
  }
}
void add_right() {
  cr++;
  s += cnt[cm[cr]];
  cnt[ci[cr]]++;
}
void del_right() {
  cnt[ci[cr]]--;
  s -= cnt[cm[cr]];
  cr--;
  if (cl > cr) {
    add_left();
  }
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", A + i);
    if (A[i] == 2) A[i] = -1;
  }
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    A[i] *= a;
  }
  for (int i = 0; i < n; i++) {
    sum[i + 1] = sum[i] + A[i];
  }
  map<long long, int> mp;
  int t = 1;
  for (int i = 0; i < n + 1; i++) {
    mp[sum[i]] = t++;
    mp[sum[i] + k] = t++;
    mp[sum[i] - k] = t++;
  }
  for (int i = 0; i < n + 1; i++) {
    ci[i] = mp[sum[i]];
    cp[i] = mp[sum[i] + k];
    cm[i] = mp[sum[i] - k];
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &p[i].l, &p[i].r);
    p[i].l--;
    p[i].index = i;
  }
  sort(p, p + q, cmp);
  cnt[ci[0]] = 1;
  add_right();
  for (int i = 0; i < q; i++) {
    int l = p[i].l, r = p[i].r;
    while (cl > l) {
      add_left();
    }
    while (cl < l) {
      del_left();
    }
    while (cr < r) {
      add_right();
    }
    while (cr > r) {
      del_right();
    }
    ans[p[i].index] = s;
  }
  for (int i = 0; i < q; i++) {
    printf("%I64d\n", ans[i]);
  }
}
