#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename t>
using V = vector<t>;
template <typename t>
void print(ostream& os, const t& a) {
  os << a << '\n';
}
template <typename t, typename... A>
void print(ostream& os, const t& a, A&&... b) {
  os << a << ' ';
  print(os, b...);
}
struct Node {
  int v, w, m;
  Node(int v, int w, int m) : v(v), w(w), m(m) {}
};
struct Ev {
  int lo, hi, id;
  Ev(int lo, int hi, int id) : lo(lo), hi(hi), id(id) {}
};
inline bool operator<(const Ev& mn, const Ev& wc) { return mn.hi < wc.hi; }
int n, k, m;
V<ll> pr;
V<ll> lp;
V<ll> odp;
V<Node> tb;
V<V<Ev> > ilo;
inline int fun(ll v) { return lower_bound(begin(lp), end(lp), v) - begin(lp); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> k;
  pr.resize(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> pr[i];
    pr[i] = pr[i] == 2 ? -1 : 1;
  }
  for (int i = 1; i <= n; ++i) {
    int p;
    cin >> p;
    pr[i] = pr[i - 1] + p * pr[i];
  };
  for (int i = 0; i <= n; ++i) {
    lp.push_back(pr[i]);
    lp.push_back(pr[i] + k);
    lp.push_back(pr[i] - k);
  }
  sort(begin(lp), end(lp));
  lp.resize(unique(begin(lp), end(lp)) - begin(lp));
  for (int i = 0; i <= n; ++i)
    tb.emplace_back(fun(pr[i]), fun(pr[i] + k), fun(pr[i] - k));
  ;
  int sqr = sqrt(n);
  ilo.resize(n / sqr + 1);
  cin >> m;
  odp.resize(m);
  for (int i = 0; i < m; ++i) {
    int lo, hi;
    cin >> lo >> hi;
    --lo;
    ilo[lo / sqr].emplace_back(lo, hi, i);
  }
  for (int i = 0; i < ilo.size(); ++i) {
    int lo = i * sqr;
    int hi = lo - 1;
    ll cnt = 0;
    V<int> arr(lp.size());
    sort(begin(ilo[i]), end(ilo[i]));
    for (auto& ev : ilo[i]) {
      while (hi < ev.hi) {
        ++hi;
        cnt += arr[tb[hi].m];
        ++arr[tb[hi].v];
      }
      while (lo > ev.lo) {
        --lo;
        cnt += arr[tb[lo].w];
        ++arr[tb[lo].v];
      }
      while (lo < ev.lo) {
        --arr[tb[lo].v];
        cnt -= arr[tb[lo].w];
        ++lo;
      };
      odp[ev.id] = cnt;
    }
  }
  for (auto i : odp) print(cout, i);
}
