#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const bool MULTI_TESTCASES = 0;
struct Query {
  int l, r, id;
};
void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> t(n + 1);
  for (int i = 1; i < n + 1; ++i) {
    cin >> t[i];
    if (t[i] == 2) t[i] = -1;
  }
  vector<ll> a(n + 1);
  for (int i = 1; i < n + 1; ++i) {
    cin >> a[i];
    a[i] *= t[i];
    a[i] += a[i - 1];
  }
  map<ll, int> comp;
  for (int i = 0; i < n + 1; ++i) {
    comp[a[i]] = comp[a[i] - k] = comp[a[i] + k] = 0;
  }
  int z = 0;
  for (auto &kv : comp) kv.second = z++;
  vector<int> a1(n + 1), a2(n + 1), a3(n + 1);
  for (int i = 0; i < n + 1; ++i) {
    a1[i] = comp[a[i]];
    a2[i] = comp[a[i] - k];
    a3[i] = comp[a[i] + k];
  }
  int q;
  cin >> q;
  vector<Query> qs(q);
  for (int i = 0; i < q; ++i) {
    cin >> qs[i].l >> qs[i].r;
    --qs[i].l;
    qs[i].id = i;
  }
  int sqrtn = sqrt(n + 1);
  sort(qs.begin(), qs.end(), [&](const Query &q1, const Query &q2) {
    return make_pair(q1.l / sqrtn, q1.r) < make_pair(q2.l / sqrtn, q2.r);
  });
  int l = 0, r = -1;
  ll cur = 0;
  vector<ll> ans(q), oc(z);
  for (auto &query : qs) {
    while (l > query.l) {
      --l;
      cur += oc[a3[l]];
      oc[a1[l]]++;
    }
    while (l < query.l) {
      oc[a1[l]]--;
      cur -= oc[a3[l]];
      ++l;
    }
    while (r < query.r) {
      ++r;
      cur += oc[a2[r]];
      oc[a1[r]]++;
    }
    while (r > query.r) {
      oc[a1[r]]--;
      cur -= oc[a2[r]];
      --r;
    }
    ans[query.id] = cur;
  }
  for (auto x : ans) {
    cout << x << '\n';
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int testcases = 1;
  if (MULTI_TESTCASES) cin >> testcases;
  while (testcases--) solve();
  return 0;
}
