#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
const int B = 333;
struct node {
  int l, r, id;
  bool operator<(const node& p) const {
    if (l / B == p.l / B) return r < p.r;
    return l < p.l;
  }
};
node pt[N];
long long ans[N];
long long now = 0;
long long num[N];
int n, q, k;
set<long long> s;
map<long long, int> mp;
int pos[3][N];
long long sum[N * 3];
void Move(int x, int op) {
  if (op == 1) {
    sum[pos[0][x]]++;
    now += sum[pos[2][x]];
    if (k == 0) now--;
  } else if (op == 2) {
    now -= sum[pos[2][x]];
    sum[pos[0][x]]--;
    if (k == 0) now++;
  } else if (op == 3) {
    sum[pos[0][x - 1]]++;
    now += sum[pos[1][x - 1]];
    if (k == 0) now--;
  } else {
    now -= sum[pos[1][x - 1]];
    sum[pos[0][x - 1]]--;
    if (k == 0) now++;
  }
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%lld", &num[i]);
  for (int i = 1; i <= n; i++) {
    if (num[i] == 2) num[i] = -1;
    int now;
    scanf("%d", &now);
    num[i] *= now;
  }
  for (int i = 0; i <= n; i++) {
    num[i] = num[i - 1] + num[i];
    s.insert(num[i]);
    s.insert(num[i] + k);
    s.insert(num[i] - k);
  }
  int tot = 0;
  for (auto it : s) {
    mp[it] = tot++;
  }
  for (int i = 0; i <= n; i++) {
    pos[0][i] = mp[num[i]];
    pos[1][i] = mp[num[i] + k];
    pos[2][i] = mp[num[i] - k];
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d %d", &pt[i].l, &pt[i].r);
    pt[i].id = i;
  }
  sort(pt + 1, pt + 1 + q);
  int L = 1, R = 1;
  sum[pos[0][0]]++;
  sum[pos[0][1]]++;
  if (num[1] == k) now++;
  for (int i = 1; i <= q; i++) {
    while (R < pt[i].r) Move(++R, 1);
    while (R > pt[i].r) Move(R--, 2);
    while (L > pt[i].l) Move(--L, 3);
    while (L < pt[i].l) Move(L++, 4);
    ans[pt[i].id] = now;
  }
  for (int i = 1; i <= q; i++) {
    printf("%lld\n", ans[i]);
  }
  return 0;
}
