#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
void err(istream_iterator<string> it) { cout << '\n'; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  string s = typeid(a).name();
  cout << "[" << *it << " = ";
  cout << a;
  cout << "] ";
  err(++it, args...);
}
pair<int, int> dx[] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
const int inf = 2e9 + 7;
const long long INF = 9e18 + 7;
const double eps = 1e-6;
int mod = 998244353;
const long long MOD = 1e15 + 7;
const bool multiple_tests = 0;
const int N = 1e5 + 7;
struct Q {
  int i, l, r;
};
int block = 300;
bool cmp(Q a, Q b) {
  if (a.l / block == b.l / block) return a.r < b.r;
  return a.l < b.l;
}
int n, k, a[N], t[N], qq, l, r = -1, prevv[N], cur[N], nextt[N], cnt[N * 3];
long long pref[N], ans[N], curans;
Q q[N];
void solve() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> t[i];
    if (t[i] == 2) t[i] = -1;
  }
  vector<long long> v;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    pref[i] = pref[i - 1] + a[i] * t[i];
    v.push_back(pref[i] - k);
    v.push_back(pref[i]);
    v.push_back(pref[i] + k);
  }
  v.push_back(0);
  v.push_back(-k);
  v.push_back(k);
  sort((v).begin(), (v).end());
  v.erase(unique((v).begin(), (v).end()), v.end());
  for (int i = 0; i <= n; i++) {
    prevv[i] = lower_bound((v).begin(), (v).end(), (pref[i] - k)) - v.begin();
    cur[i] = lower_bound((v).begin(), (v).end(), (pref[i])) - v.begin();
    nextt[i] = lower_bound((v).begin(), (v).end(), (pref[i] + k)) - v.begin();
  }
  cin >> qq;
  for (int i = 1; i <= qq; i++) {
    cin >> q[i].l >> q[i].r;
    q[i].l--;
    q[i].i = i;
  }
  sort(q + 1, q + qq + 1, cmp);
  for (int i = 1; i <= qq; i++) {
    int L = q[i].l, R = q[i].r, ID = q[i].i;
    while (r < R) {
      r++;
      curans += cnt[prevv[r]];
      cnt[cur[r]]++;
    }
    while (l > L) {
      l--;
      curans += cnt[nextt[l]];
      cnt[cur[l]]++;
    }
    while (r > R) {
      cnt[cur[r]]--;
      curans -= cnt[prevv[r]];
      r--;
    }
    while (l < L) {
      cnt[cur[l]]--;
      curans -= cnt[nextt[l]];
      l++;
    }
    ans[ID] = curans;
  }
  for (int i = 1; i <= qq; i++) {
    cout << ans[i] << '\n';
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int TT = 1;
  if (multiple_tests) cin >> TT;
  for (int testcase = 1; testcase <= TT; testcase++) {
    solve();
    cout << '\n';
  }
  return 0;
}
