#include <bits/stdc++.h>
using namespace std;
int n, k;
int t[100005], a[100005];
int q;
long long int sum[100005], add[100005], sub[100005];
int m[3 * 100005];
long long int res[100005];
vector<long long int> comp;
struct data {
  int L, R, idx;
} query[100005];
bool cmp(data &x, data &y) {
  int bn1 = (x.L + 320 - 1) / 320;
  int bn2 = (y.L + 320 - 1) / 320;
  if (bn1 != bn2) return bn1 < bn2;
  return x.R < y.R;
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", &t[i]);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (t[i] == 2) a[i] *= -1;
  }
  for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i];
  for (int i = 0; i <= n; i++) {
    add[i] = sum[i] + k;
    sub[i] = sum[i] - k;
  }
  for (int i = 0; i <= n; i++) {
    comp.push_back(sum[i]);
    comp.push_back(add[i]);
    comp.push_back(sub[i]);
  }
  sort(comp.begin(), comp.end());
  comp.resize(unique(comp.begin(), comp.end()) - comp.begin());
  for (int i = 0; i <= n; i++) {
    sum[i] = lower_bound(comp.begin(), comp.end(), sum[i]) - comp.begin();
    add[i] = lower_bound(comp.begin(), comp.end(), add[i]) - comp.begin();
    sub[i] = lower_bound(comp.begin(), comp.end(), sub[i]) - comp.begin();
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d %d", &query[i].L, &query[i].R);
    query[i].idx = i;
  }
  sort(query + 1, query + q + 1, cmp);
  long long int ans = 0;
  int l = 1, r = 0;
  m[sum[0]]++;
  for (int i = 1; i <= q; i++) {
    while (r < query[i].R) {
      r++;
      ans += m[sub[r]];
      m[sum[r]]++;
    }
    while (l > query[i].L) {
      l--;
      ans += m[add[l - 1]];
      m[sum[l - 1]]++;
    }
    while (r > query[i].R) {
      m[sum[r]]--;
      ans -= m[sub[r]];
      r--;
    }
    while (l < query[i].L) {
      m[sum[l - 1]]--;
      ans -= m[add[l - 1]];
      l++;
    }
    res[query[i].idx] = ans;
  }
  for (int i = 1; i <= q; i++) printf("%lld\n", res[i]);
  return 0;
}
