#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
long long n, t[N], q, S;
long long k, a[N], b[N], l1[N], r1[N], f[N], l, r, used[N], ans[N], used1[N],
    cnt;
map<long long, long long> mymap, mymap1;
vector<pair<pair<long long, long long>, long long> > query;
set<long long> st;
set<long long>::iterator it;
bool cmp(const pair<pair<long long, long long>, long long> &a,
         const pair<pair<long long, long long>, long long> &b) {
  if (a.first.first / S != b.first.first / S)
    return a.first.first < b.first.first;
  else
    return a.first.second > b.first.second;
}
signed main() {
  cin.tie(0), ios::sync_with_stdio(0);
  cin >> n >> k;
  S = sqrt(n);
  for (long long i = 1; i <= n; i++) cin >> t[i];
  for (long long i = 1; i <= n; i++) cin >> a[i];
  for (long long i = 1; i <= n; i++) {
    if (t[i] == 1)
      b[i] += a[i];
    else
      b[i] -= a[i];
    b[i] += b[i - 1];
  }
  cin >> q;
  for (long long i = 1; i <= q; i++) {
    long long l1, r1;
    cin >> l1 >> r1;
    l1--;
    query.push_back(make_pair(make_pair(l1, r1), i));
  }
  sort(query.begin(), query.end(), cmp);
  st.insert(0);
  mymap1[0]++;
  for (long long i = 1; i <= n; i++) {
    st.insert(b[i]);
    mymap1[b[i]]++;
  }
  long long num = -LLONG_MAX;
  for (it = st.begin(); it != st.end(); it++) {
    if ((*it) != num) {
      num = (*it);
      cnt++;
      mymap[(*it)] = cnt;
    }
  }
  for (long long i = 0; i <= n; i++) {
    l1[i] = LLONG_MAX;
    r1[i] = LLONG_MAX;
    if (mymap1.count(b[i] - k)) l1[i] = mymap[b[i] - k];
    if (mymap1.count(b[i] + k)) r1[i] = mymap[b[i] + k];
    f[i] = mymap[b[i]];
  }
  memset(used, 0, sizeof(used));
  cnt = 0;
  for (long long i = query[0].first.first; i <= query[0].first.second; i++) {
    cnt += used[l1[i]];
    used[f[i]]++;
  }
  ans[query[0].second] = cnt;
  l = query[0].first.first;
  r = query[0].first.second;
  if (k != 0) {
    for (long long i = 1; i < q; i++) {
      while (l < query[i].first.first) {
        if (r1[l] != LLONG_MAX) cnt -= used[r1[l]];
        used[f[l]]--;
        l++;
      }
      while (l > query[i].first.first) {
        l--;
        if (r1[l] != LLONG_MAX) cnt += used[r1[l]];
        used[f[l]]++;
      }
      while (r < query[i].first.second) {
        r++;
        if (l1[r] != LLONG_MAX) cnt += used[l1[r]];
        used[f[r]]++;
      }
      while (r > query[i].first.second) {
        if (l1[r] != LLONG_MAX) cnt -= used[l1[r]];
        used[f[r]]--;
        r--;
      }
      ans[query[i].second] = cnt;
    }
  } else {
    for (long long i = 1; i < q; i++) {
      while (l < query[i].first.first) {
        used[f[l]]--;
        cnt -= used[r1[l]];
        l++;
      }
      while (l > query[i].first.first) {
        l--;
        cnt += used[r1[l]];
        used[f[l]]++;
      }
      while (r < query[i].first.second) {
        r++;
        cnt += used[l1[r]];
        used[f[r]]++;
      }
      while (r > query[i].first.second) {
        used[f[r]]--;
        cnt -= used[l1[r]];
        r--;
      }
      ans[query[i].second] = cnt;
    }
  }
  for (long long i = 1; i <= q; i++) cout << ans[i] << "\n";
}
