#include <bits/stdc++.h>
using namespace std;
int lg(long long n) {
  assert(n > 0);
  int ans = -1;
  while (n) {
    ans++;
    n >>= 1;
  }
  return ans;
}
const int B = 400;
struct Query {
  int l, r, idx;
  Query() {}
  Query(int l_, int r_, int idx_) : l(l_), r(r_), idx(idx_) {}
  bool operator<(const Query &oth) const {
    return make_pair(l / B, r) < make_pair(oth.l / B, oth.r);
  }
};
void solve() {
  int N, K;
  cin >> N >> K;
  vector<int> T(N), A(N);
  for (auto &t : T) cin >> t;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (T[i] == 2) {
      A[i] = -A[i];
    }
  }
  vector<long long> vals{0};
  vector<long long> presum(N + 1);
  for (int i = 0; i < N; i++) {
    presum[i + 1] = presum[i] + A[i];
    vals.push_back(presum[i + 1]);
  }
  sort(vals.begin(), vals.end());
  vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
  auto get = [&](long long val) {
    auto it = lower_bound(vals.begin(), vals.end(), val);
    return (it == vals.end() || *it != val) ? -1 : it - vals.begin();
  };
  vector<int> v(N + 1), l(N + 1), r(N + 1);
  for (int i = 0; i <= N; i++) {
    v[i] = get(presum[i]);
    l[i] = get(presum[i] - K);
    r[i] = get(presum[i] + K);
  }
  int Q;
  cin >> Q;
  vector<Query> queries(Q);
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    queries[i] = Query(l, r, i);
  }
  sort(queries.begin(), queries.end());
  int pl = 1, pr = 0;
  vector<long long> cnt(vals.size());
  cnt[v[0]] = 1;
  long long res = 0;
  vector<long long> ans(Q);
  auto addL = [&](int idx) {
    if (r[idx - 1] != -1) {
      res += cnt[r[idx - 1]];
    }
    cnt[v[idx - 1]]++;
  };
  auto addR = [&](int idx) {
    if (l[idx] != -1) {
      res += cnt[l[idx]];
    }
    cnt[v[idx]]++;
  };
  auto delL = [&](int idx) {
    cnt[v[idx - 1]]--;
    if (r[idx - 1] != -1) {
      res -= cnt[r[idx - 1]];
    }
  };
  auto delR = [&](int idx) {
    cnt[v[idx]]--;
    if (l[idx] != -1) {
      res -= cnt[l[idx]];
    }
  };
  for (auto [ql, qr, idx] : queries) {
    while (pl > ql) {
      addL(--pl);
    }
    while (pr < qr) {
      addR(++pr);
    }
    while (pl < ql) {
      delL(pl++);
    }
    while (pr > qr) {
      delR(pr--);
    }
    ans[idx] = res;
  }
  for (int i = 0; i < Q; i++) {
    cout << ans[i] << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) solve();
  return 0;
}
