#include <bits/stdc++.h>
using namespace std;
struct aa {
  int l, r;
  int idx;
  int pos;
} q[100008];
long long cnt[3 * 100008], ans[100008], sum[100008], typ[100008], s[100008],
    l_x[100008], x_r[100008], x[100008];
vector<long long> key;
bool cmp(const aa &a, const aa &b) {
  if (a.pos == b.pos) return a.r < b.r;
  return a.pos < b.pos;
}
int main() {
  int n, k;
  while (scanf("%d%d", &n, &k) != EOF) {
    memset(cnt, 0, sizeof(cnt));
    memset(sum, 0, sizeof(sum));
    for (int i = 1; i <= n; i++) cin >> typ[i];
    for (int i = 1; i <= n; i++) {
      cin >> s[i];
      typ[i] == 1 ? sum[i] = sum[i - 1] + s[i] : sum[i] = sum[i - 1] - s[i];
    }
    for (int i = 0; i <= n; i++) {
      key.push_back(sum[i]);
      key.push_back(sum[i] + k);
      key.push_back(sum[i] - k);
    }
    sort(key.begin(), key.end());
    key.erase(unique(key.begin(), key.end()), key.end());
    for (int i = 0; i <= n; i++) {
      x[i] = lower_bound(key.begin(), key.end(), sum[i]) - key.begin();
      l_x[i] = lower_bound(key.begin(), key.end(), sum[i] - k) - key.begin();
      x_r[i] = lower_bound(key.begin(), key.end(), sum[i] + k) - key.begin();
    }
    int qry;
    long long res = 0;
    int len = sqrt(n + 0.5);
    cin >> qry;
    for (int i = 0; i < qry; i++) {
      scanf("%d%d", &q[i].l, &q[i].r);
      q[i].l--;
      q[i].idx = i;
      q[i].pos = q[i].l / len;
    }
    sort(q, q + qry, cmp);
    int L = 0, R = -1;
    for (int i = 0; i < qry; i++) {
      while (q[i].r > R) {
        res += cnt[l_x[++R]];
        cnt[x[R]]++;
      }
      while (q[i].r < R) {
        cnt[x[R]]--;
        res -= cnt[l_x[R--]];
      }
      while (q[i].l > L) {
        cnt[x[L]]--;
        res -= cnt[x_r[L++]];
      }
      while (q[i].l < L) {
        res += cnt[x_r[--L]];
        cnt[x[L]]++;
      }
      ans[q[i].idx] = res;
    }
    for (int i = 0; i < qry; i++) cout << ans[i] << endl;
  }
}
