#include <bits/stdc++.h>
using namespace std;
constexpr int sq = 396;
struct query {
  int l, r, id;
};
inline bool operator<(const query &a, const query &b) {
  if (a.l / sq ^ b.l / sq) return (a.l / sq < b.l / sq);
  return ((a.l / sq) & 1 ? (a.r > b.r) : (a.r < b.r));
}
const int N = 1e5 + 5;
const int N1 = 4e5 + 100;
long long pf[N];
int cnt[N1], need[N1], need1[N1];
long long ans = 0;
inline void addR(int x) {
  ans += cnt[need[pf[x]]];
  ++cnt[pf[x]];
}
inline void addL(int x) {
  ans += cnt[need1[pf[x]]];
  ++cnt[pf[x]];
}
inline void delR(int x) {
  --cnt[pf[x]];
  ans -= cnt[need[pf[x]]];
}
inline void delL(int x) {
  --cnt[pf[x]];
  ans -= cnt[need1[pf[x]]];
}
void solve() {
  int n, k;
  cin >> n >> k;
  vector<long long> type(n + 1), ar(n + 1);
  for (int i = 1; i <= n; ++i) cin >> type[i];
  for (int i = 1; i <= n; ++i) {
    cin >> ar[i];
    if (type[i] == 2) ar[i] *= -1;
    pf[i] = pf[i - 1] + ar[i];
  }
  map<long long, int> cp;
  for (int i = 0; i <= n; ++i) {
    ++cp[pf[i]];
    ++cp[pf[i] - k];
    ++cp[pf[i] + k];
  }
  int C = 0;
  for (auto &i : cp) i.second = C++;
  for (int i = 0; i <= n; ++i) {
    int val = cp[pf[i]];
    need[val] = cp[pf[i] - k];
    need1[val] = cp[pf[i] + k];
    pf[i] = val;
  }
  int q;
  cin >> q;
  vector<query> a(q);
  for (int i = 0; i < q; ++i) {
    cin >> a[i].l >> a[i].r;
    a[i].id = i;
    --a[i].l;
  }
  sort((a).begin(), (a).end());
  vector<long long> res(q);
  int l = a[0].l, r = a[0].l - 1;
  for (int i = 0; i < q; ++i) {
    while (r < a[i].r) addR(++r);
    while (l > a[i].l) addL(--l);
    while (r > a[i].r) delR(r--);
    while (l < a[i].l) delL(l++);
    res[a[i].id] = ans;
  }
  for (auto i : res) cout << i << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q = 1;
  while (q--) solve();
}
