#include <bits/stdc++.h>
using namespace std;
const int inf = 1e18;
const int N = 3e3 + 222;
int c;
bool comp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  if (a.first.first / c != b.first.first / c) {
    return a.first.first / c < b.first.first / c;
  } else
    return a.first.second < b.first.second;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  c = pow(n, 0.66);
  int t[n];
  for (int i = 0; i < n; ++i) cin >> t[i];
  int a[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if (t[i] == 2) a[i] = -a[i];
  }
  long long pref[n];
  pref[0] = a[0];
  for (int i = 1; i < n; ++i) pref[i] = pref[i - 1] + a[i];
  long long p[n], l[n], r[n];
  set<long long> st;
  st.insert(0);
  st.insert(k);
  map<long long, int> mp;
  for (int i = 0; i < n; ++i) {
    l[i] = pref[i] - k;
    r[i] = pref[i] + k;
    p[i] = pref[i];
    st.insert(l[i]);
    st.insert(r[i]);
    st.insert(p[i]);
  }
  int z = 1;
  for (auto to : st) {
    mp[to] = z;
    z++;
  }
  int q;
  cin >> q;
  pair<pair<int, int>, int> x[q];
  for (int i = 0; i < q; ++i) {
    cin >> x[i].first.first >> x[i].first.second;
    x[i].first.first--;
    x[i].first.second--;
    x[i].second = i;
  }
  for (int i = 0; i < n; ++i) {
    l[i] = mp[l[i]];
    r[i] = mp[r[i]];
    p[i] = mp[p[i]];
  }
  sort(x, x + q, comp);
  vector<int> cnt(z, 0);
  vector<long long> ot(q);
  int cl = -1, cr = -1;
  long long ans = 0;
  cnt[mp[0]] = 1;
  for (int i = 0; i < q; ++i) {
    while (cr < x[i].first.second) {
      cr++;
      ans += cnt[l[cr]];
      cnt[p[cr]]++;
    }
    while (cr > x[i].first.second) {
      cnt[p[cr]]--;
      ans -= cnt[l[cr]];
      cr--;
    }
    while (cl > x[i].first.first - 1) {
      cl--;
      if (cl > -1) {
        ans += cnt[r[cl]];
        cnt[p[cl]]++;
      } else {
        ans += cnt[mp[k]];
        cnt[mp[0]]++;
      }
    }
    while (cl < x[i].first.first - 1) {
      if (cl > -1) {
        cnt[p[cl]]--;
        ans -= cnt[r[cl]];
      } else {
        cnt[mp[0]]--;
        ans -= cnt[mp[k]];
      }
      cl++;
    }
    ot[x[i].second] = ans;
  }
  for (auto to : ot) cout << to << endl;
}
