#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 1e6 + 6;
int S;
ll br(int l, int r, vector<ll> &s) {
  ll res = 0;
  for (int i = l; i <= r; ++i)
    for (int j = i; j <= r; ++j)
      if (s[i] - s[j + 1] == S) ++res;
  return res;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n >> S;
  vector<int> t(n);
  for (int &i : t) cin >> i;
  vector<int> a(n);
  for (int &x : a) cin >> x;
  for (int i = 0; i < n; ++i)
    if (t[i] == 2) a[i] *= -1;
  vector<ll> s(n + 1);
  for (int i = n - 1; i >= 0; --i) s[i] = s[i + 1] + a[i];
  map<ll, int> mid;
  int cid = 0;
  for (int i = 0; i <= n; ++i)
    if (mid.count(s[i]) == 0) mid[s[i]] = ++cid;
  vector<int> id(n + 1), id2(n + 1), id3(n + 1);
  for (int i = 0; i <= n; ++i) {
    id[i] = mid[s[i]];
    id2[i] = mid[s[i] + S];
    id3[i] = mid[s[i] - S];
  }
  int m;
  cin >> m;
  vector<ll> ans(m);
  vector<pair<int, int>> q(m);
  int K = sqrt(n);
  cerr << "K = " << K << endl;
  vector<vector<int>> g(n);
  for (int i = 0; i < m; ++i) {
    int l, r;
    cin >> l >> r;
    --l;
    --r;
    q[i] = {l, r};
    g[l / K].push_back(i);
  }
  for (int k = 0; k * K < n; ++k) {
    sort((g[k]).begin(), (g[k]).end(),
         [&](int i, int j) { return q[i].second < q[j].second; });
    vector<int> cnt(cid + 1);
    ll cur = 0;
    int L = k * K;
    int R = L - 1;
    cnt[id[R + 1]] = 1;
    for (int i : g[k]) {
      int l = q[i].first, r = q[i].second;
      while (R < r) {
        ++R;
        cur += cnt[id2[R + 1]];
        ++cnt[id[R + 1]];
      }
      while (l < L) {
        --L;
        cur += cnt[id3[L]];
        ++cnt[id[L]];
      }
      while (L < l) {
        --cnt[id[L]];
        cur -= cnt[id3[L]];
        ++L;
      }
      ans[i] = cur;
    }
  }
  for (ll res : ans) cout << res << '\n';
  return 0;
}
