#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize( \
    "O3,Ofast,no-stack-protector,unroll-loops,fast-math,-ffp-contract=off")
const int32_t maxn = 1e5, SQ = sqrt(maxn);
long long a[maxn], b[maxn + 1], c[maxn];
short t[maxn];
int32_t L[maxn], R[maxn], vec[maxn];
unordered_map<long long, long long> cnt;
inline bool cmp(int32_t &A, int32_t &B) {
  int32_t Al = L[A], Bl = L[B], Ar = R[A], Br = R[B];
  if (Al / SQ != Bl / SQ) return Al < Bl;
  if ((Al / SQ) % 2) return Ar < Br;
  return Ar > Br;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  long long k;
  cin >> n >> k;
  for (long long i = 0; i < n; i++) cin >> t[i];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    if (t[i] == 2) a[i] *= -1;
  }
  for (long long i = 1; i <= n; i++) b[i] = b[i - 1] + a[i - 1];
  long long q;
  cin >> q;
  for (long long i = 0; i < q; i++) {
    cin >> L[i] >> R[i];
    L[i]--;
    R[i]++;
    vec[i] = i;
  }
  long long MO_right = 1, MO_left = 0;
  long long ans = 0;
  cnt[0]++;
  sort(vec, vec + n, cmp);
  for (long long i = 0; i < n; i++) {
    long long Q = vec[i];
    long long Ql = L[Q], Qr = R[Q];
    while (MO_right < Qr) {
      ans += cnt[b[MO_right] - k];
      cnt[b[MO_right]]++;
      MO_right++;
    }
    while (MO_left > Ql) {
      MO_left--;
      ans += cnt[b[MO_left] + k];
      cnt[b[MO_left]]++;
    }
    while (MO_right > Qr) {
      MO_right--;
      cnt[b[MO_right]]--;
      ans -= cnt[b[MO_right] - k];
    }
    while (MO_left < Ql) {
      cnt[b[MO_left]]--;
      ans -= cnt[b[MO_left] + k];
      MO_left++;
    }
    c[Q] = ans;
  }
  for (long long i = 0; i < q; i++) cout << c[i] << '\n';
}
