#include <bits/stdc++.h>
using namespace std;
const long long INF = 2147483647;
const long long LNF = INF * INF;
const long long MOD = 1000000007;
const long long mod = 998244353;
const long double eps = 1e-12;
const long long MAX = 100010;
long long n, k, q;
long long sign[MAX];
long long arr[MAX], arrL[MAX], arrR[MAX];
long long cnt[3 * MAX];
long long block;
vector<vector<long long> > queries;
long long res[MAX];
long long tmp_res;
long long lptr, rptr;
bool cmp(vector<long long> a, vector<long long> b) {
  if (a[1] / block == b[1] / block) return (a[2] < b[2]);
  return (a[1] / block < b[1] / block);
}
long long dptr = 0;
void move_ptr(long long L, long long R) {
  while (lptr > L) {
    lptr--;
    tmp_res += cnt[arrR[lptr]];
    cnt[arr[lptr]]++;
    dptr++;
  }
  while (rptr < R) {
    rptr++;
    tmp_res += cnt[arrL[rptr]];
    cnt[arr[rptr]]++;
    dptr++;
  }
  while (lptr < L) {
    cnt[arr[lptr]]--;
    tmp_res -= cnt[arrR[lptr]];
    lptr++;
    dptr++;
  }
  while (rptr > R) {
    cnt[arr[rptr]]--;
    tmp_res -= cnt[arrL[rptr]];
    rptr--;
    dptr++;
  }
  assert(dptr < 3e8);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  block = floor(sqrt(n + eps));
  for (long long i = (1); i <= (n); i += (1)) {
    cin >> sign[i];
  }
  for (long long i = (1); i <= (n); i += (1)) {
    cin >> arr[i];
    if (sign[i] == 2) arr[i] *= -1;
  }
  for (long long i = (1); i <= (n); i += (1)) {
    arr[i] = arr[i - 1] + arr[i];
  }
  for (long long i = (0); i <= (n); i += (1)) {
    arrL[i] = arr[i] - k;
    arrR[i] = arr[i] + k;
  }
  vector<long long> tmp;
  for (long long i = (0); i <= (n); i += (1)) {
    tmp.push_back(arr[i]);
    tmp.push_back(arrL[i]);
    tmp.push_back(arrR[i]);
  }
  sort(tmp.begin(), tmp.end());
  auto it = unique(tmp.begin(), tmp.end());
  tmp.resize(distance(tmp.begin(), it));
  map<long long, long long> dis;
  for (long long i : tmp) {
    dis[i] = ((long long)(dis).size());
  }
  for (long long i = (0); i <= (n); i += (1)) {
    arr[i] = dis[arr[i]];
    arrL[i] = dis[arrL[i]];
    arrR[i] = dis[arrR[i]];
  }
  cin >> q;
  for (long long i = (1); i <= (q); i += (1)) {
    long long L, R;
    cin >> L >> R;
    queries.push_back({i, L - 1, R});
  }
  sort(queries.begin(), queries.end(), cmp);
  lptr = 0;
  rptr = -1;
  for (vector<long long> qq : queries) {
    long long qid = qq[0];
    long long L = qq[1];
    long long R = qq[2];
    move_ptr(L, R);
    res[qid] = tmp_res;
  }
  for (long long i = (1); i <= (q); i += (1)) {
    cout << res[i] << '\n';
  }
  return 0;
}
