#include <bits/stdc++.h>
using namespace std;
template <typename T>
void printvec(vector<T>& vec) {
  for (int i = 0; i < vec.size(); i++) {
    cout << vec[i] << " ";
  }
  cout << "\n";
}
struct Query {
  int left;
  int right;
  int idx;
};
int x;
bool compare(struct Query& a, struct Query& b) {
  if (a.left / 300 == b.left / 300) {
    return a.right < b.right;
  }
  return a.left < b.left;
}
int main() {
  std::ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<int> arr1(n), arr2(n);
  int i;
  for (i = 0; i < n; i++) {
    cin >> arr1[i];
  }
  for (i = 0; i < n; i++) {
    cin >> arr2[i];
  }
  int q;
  cin >> q;
  int x = sqrt(q);
  vector<struct Query> vec(q);
  for (i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    vec[i] = {--l, r, i};
  }
  sort(vec.begin(), vec.end(), compare);
  vector<long long> prefix(n + 1, 0);
  for (i = 1; i <= n; i++) {
    if (arr1[i - 1] == 1) {
      prefix[i] = prefix[i - 1] + (long long)arr2[i - 1];
    } else {
      prefix[i] = prefix[i - 1] - (long long)arr2[i - 1];
    }
  }
  map<long long, int> mapp;
  for (i = 0; i <= n; i++) {
    mapp[prefix[i]] = 0;
  }
  map<long long, int>::iterator it;
  int pre = 0;
  for (it = mapp.begin(); it != mapp.end(); it++) {
    it->second = (pre++);
  }
  vector<int> frnd(pre, -1), revfrnd(pre, -1);
  for (i = 0; i <= n; i++) {
    if (mapp.find(prefix[i] - k) != mapp.end()) {
      frnd[mapp[prefix[i]]] = mapp[prefix[i] - k];
    }
    if (mapp.find(prefix[i] + k) != mapp.end()) {
      revfrnd[mapp[prefix[i]]] = mapp[prefix[i] + k];
    }
  }
  for (i = 0; i <= n; i++) {
    prefix[i] = mapp[prefix[i]];
  }
  vector<long long> ans(q);
  int prel = 0, prer = -1;
  vector<long long> cnt(pre, 0);
  long long preans = 0;
  for (i = 0; i < q; i++) {
    int idx = vec[i].idx;
    int l = vec[i].left;
    int r = vec[i].right;
    while (prer < r) {
      prer++;
      if (frnd[prefix[prer]] != -1) preans += cnt[frnd[prefix[prer]]];
      cnt[prefix[prer]]++;
    }
    while (prel > l) {
      prel--;
      if (revfrnd[prefix[prel]] != -1) preans += cnt[revfrnd[prefix[prel]]];
      cnt[prefix[prel]]++;
    }
    while (prer > r) {
      cnt[prefix[prer]]--;
      if (frnd[prefix[prer]] != -1) preans -= cnt[frnd[prefix[prer]]];
      prer--;
    }
    while (prel < l) {
      cnt[prefix[prel]]--;
      if (revfrnd[prefix[prel]] != -1) preans -= cnt[revfrnd[prefix[prel]]];
      prel++;
    }
    ans[idx] = preans;
  }
  for (i = 0; i < q; i++) {
    printf("%lld\n", ans[i]);
  }
  return 0;
}
