#include <bits/stdc++.h>
using namespace std;
long long K = 320;
struct Q {
  long long l;
  long long r;
  long long ind;
  bool operator<(const Q& other) const {
    if (l / K != other.l / K) return l < other.l;
    return r < other.r;
  }
};
bool cmp(Q a, Q b) { return a.r < b.r; }
const long long maxn = 3e5 + 10;
vector<long long> cnt(maxn), fl(maxn), fr(maxn), f(maxn);
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cerr.tie(nullptr);
  long long n, k;
  cin >> n >> k;
  vector<long long> tmp(n);
  for (auto& i : tmp) cin >> i;
  vector<long long> a = {0};
  set<long long> s;
  s.insert(0);
  s.insert(k);
  s.insert(-1 * k);
  for (long long i = 0; i < n; ++i) {
    long long x;
    cin >> x;
    if (tmp[i] == 2) x *= -1;
    a.push_back(x + a.back());
    s.insert(a.back());
    s.insert(a.back() - k);
    s.insert(a.back() + k);
  }
  map<long long, long long> mapa;
  long long cur = 1;
  for (long long x : s) {
    mapa[x] = cur++;
  }
  for (long long i = 0; i <= n; ++i) {
    f[i] = mapa[a[i]];
    fl[i] = mapa[a[i] + k];
    fr[i] = mapa[a[i] - k];
  }
  long long m;
  cin >> m;
  vector<Q> t(m);
  for (long long i = 0; i < m; ++i) {
    long long l, r;
    cin >> l >> r;
    t[i] = {l, r, i};
  }
  sort(t.begin(), t.end());
  vector<long long> sol(m);
  long long l = 1, r = 1;
  cnt[f[0]] = 1;
  ++cnt[f[1]];
  long long res = 0;
  if (a[1] == k) ++res;
  for (long long e = 0; e < m; ++e) {
    auto p = t[e];
    while (r < p.r) {
      ++r;
      long long x = fr[r];
      res += cnt[x];
      ++cnt[f[r]];
    }
    while (r > p.r) {
      long long x = fr[r];
      --cnt[f[r]];
      res -= cnt[x];
      --r;
    }
    while (l > p.l) {
      --l;
      long long x = fl[l - 1];
      res += cnt[x];
      ++cnt[f[l - 1]];
    }
    while (l < p.l) {
      long long x = fl[l - 1];
      --cnt[f[l - 1]];
      res -= cnt[x];
      ++l;
    }
    sol[p.ind] = res;
  }
  for (auto& i : sol) cout << i << '\n';
}
