#include <bits/stdc++.h>
using namespace std;
inline void splay(int &v) {
  v = 0;
  char c = 0;
  int p = 1;
  while (c < '0' || c > '9') {
    if (c == '-') p = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    v = (v << 3) + (v << 1) + c - '0';
    c = getchar();
  }
  v *= p;
}
const int N = 100010, P = 600;
struct Q {
  int l, r, id;
  bool operator<(const Q &x) const {
    if (l / P != x.l / P) return l < x.l;
    return r < x.r;
  }
} q[N];
long long sum[N], Ans[N];
map<long long, int> t;
int n, k, op[N], a[N];
int ton[N << 3], A[N], B[N], C[N];
int main() {
  splay(n), splay(k);
  for (int i = 1; i <= n; i++) {
    splay(op[i]);
  }
  for (int i = 1; i <= n; i++) {
    splay(a[i]);
    if (op[i] == 2) a[i] = -a[i];
    sum[i] = sum[i - 1] + a[i];
  }
  for (int i = 0; i <= n; i++) {
    t[sum[i]] = 1;
    t[sum[i] - k] = 1;
    t[sum[i] + k] = 1;
  }
  int cnt = 0;
  for (map<long long, int>::iterator it = t.begin(); it != t.end(); ++it) {
    it->second = ++cnt;
  }
  for (int i = 0; i <= n; i++) {
    A[i] = t[sum[i]];
    B[i] = t[sum[i] - k];
    C[i] = t[sum[i] + k];
  }
  int Q;
  splay(Q);
  for (int i = 1; i <= Q; i++) {
    splay(q[i].l), splay(q[i].r);
    q[i].id = i;
  }
  sort(q + 1, q + Q + 1);
  int l = 1, r = 0;
  long long ans = 0;
  for (int i = 1; i <= Q; i++) {
    while (r < q[i].r) {
      r++;
      ans += ton[B[r]];
      if (sum[r] - sum[l - 1] == k) ans++;
      ton[A[r]]++;
    }
    while (r > q[i].r) {
      ton[A[r]]--;
      if (sum[r] - sum[l - 1] == k) ans--;
      ans -= ton[B[r]];
      r--;
    }
    while (l > q[i].l) {
      l--;
      ton[A[l]]++;
      ans += ton[C[l - 1]];
    }
    while (l < q[i].l) {
      ans -= ton[C[l - 1]];
      ton[A[l]]--;
      l++;
    }
    Ans[q[i].id] = ans;
  }
  for (int i = 1; i <= Q; i++) {
    printf("%I64d\n", Ans[i]);
  }
}
