#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int BLOCK = 316;
struct node {
  long long down, val, up;
} v[N];
long long temp[N];
vector<long long> sorted;
int ap[3 * N];
struct query {
  int l, r, i;
} Q[N];
bool comp(query a, query b) {
  if (a.l / BLOCK == b.l / BLOCK) {
    return a.r < b.r;
  }
  return a.l < b.l;
}
long long ans;
long long res[N];
int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%I64d", &temp[i]);
    if (temp[i] == 2) {
      temp[i] = -1;
    }
  }
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    temp[i] *= x;
    temp[i] += temp[i - 1];
  }
  for (int i = 0; i <= n; i++) {
    v[i] = {temp[i] - k, temp[i], temp[i] + k};
    sorted.push_back(v[i].down);
    sorted.push_back(v[i].val);
    sorted.push_back(v[i].up);
  }
  sort(sorted.begin(), sorted.end());
  sorted.resize(unique(sorted.begin(), sorted.end()) - sorted.begin());
  for (int i = 0; i <= n; i++) {
    v[i].down =
        lower_bound(sorted.begin(), sorted.end(), v[i].down) - sorted.begin();
    v[i].val =
        lower_bound(sorted.begin(), sorted.end(), v[i].val) - sorted.begin();
    v[i].up =
        lower_bound(sorted.begin(), sorted.end(), v[i].up) - sorted.begin();
  }
  int q;
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d %d", &Q[i].l, &Q[i].r);
    Q[i].i = i;
  }
  sort(Q + 1, Q + q + 1, comp);
  int cL, cR;
  cL = 0;
  cR = n;
  for (int i = 0; i <= n; i++) {
    ans += ap[v[i].down];
    ap[v[i].val]++;
  }
  for (int id = 1; id <= q; id++) {
    int L, R;
    L = Q[id].l - 1;
    R = Q[id].r;
    for (int i = cL; i < L; i++) {
      ap[v[i].val]--;
      ans -= ap[v[i].up];
    }
    for (int i = cL - 1; i >= L; i--) {
      ans += ap[v[i].up];
      ap[v[i].val]++;
    }
    cL = L;
    for (int i = cR; i > R; i--) {
      ap[v[i].val]--;
      ans -= ap[v[i].down];
    }
    for (int i = cR + 1; i <= R; i++) {
      ans += ap[v[i].down];
      ap[v[i].val]++;
    }
    cR = R;
    res[Q[id].i] = ans;
  }
  for (int i = 1; i <= q; i++) {
    printf("%I64d\n", res[i]);
  }
  return 0;
}
