#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, SQR, q;
int t[N];
long long freq[N], a[N];
long long Ans[N], res, k;
unordered_map<long long, long long> mp;
struct Query {
  long long L, R, Index, Bucket_index;
} Range[N];
inline bool comp(Query x, Query y) {
  if (x.Bucket_index != y.Bucket_index) return x.Bucket_index < y.Bucket_index;
  if (x.Bucket_index & 1) return x.R < y.R;
  return x.R > y.R;
}
inline void AddR(int i) {
  if (mp.find(a[i] - k) != mp.end()) res += mp[a[i] - k];
  mp[a[i]]++;
}
inline void RemoveR(int i) {
  mp[a[i]]--;
  if (mp.find(a[i] - k) != mp.end()) res -= mp[a[i] - k];
}
inline void AddL(int i) {
  if (mp.find(a[i] + k) != mp.end()) res += mp[a[i] + k];
  mp[a[i]]++;
}
inline void RemoveL(int i) {
  mp[a[i]]--;
  if (mp.find(a[i] + k) != mp.end()) res -= mp[a[i] + k];
}
void solve() {
  sort(Range + 1, Range + q + 1, comp);
  int CL = 1, CR = 0;
  for (int i = 1; i <= q; ++i) {
    while (CL < Range[i].L) RemoveL(CL++);
    while (CL > Range[i].L) AddL(--CL);
    while (CR < Range[i].R) AddR(++CR);
    while (CR > Range[i].R) RemoveR(CR--);
    Ans[Range[i].Index] = res;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  scanf("%d %lld", &n, &k);
  SQR = sqrt(n) + 1;
  for (int i = 1; i <= n; ++i) scanf("%d", t + i);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", a + i);
    if (t[i] - 1) a[i] *= -1;
    a[i] += a[i - 1];
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    long long L, R;
    scanf("%lld %lld", &L, &R);
    Range[i].Index = i;
    Range[i].Bucket_index = L / SQR;
    Range[i].L = L - 1;
    Range[i].R = R;
  }
  solve();
  for (int i = 1; i <= q; i++) printf("%lld \n", Ans[i]);
}
