#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int maxn = 100005;
int n, t[maxn], q, sz, lst[maxn], nxt[maxn];
long long K, a[maxn], pre[maxn], cnt[maxn], res[maxn];
vector<long long> v;
map<long long, int> mp;
struct node {
  int l, r, id;
  bool operator<(const node &rhs) const {
    return l / sz == rhs.l / sz ? r < rhs.r : l / sz < rhs.l / sz;
  }
} buc[maxn];
inline void print() {
  printf("sz=%d\n", sz);
  for (int i = 0; i < (int)v.size(); ++i)
    printf("%I64d%c", v[i], i + 1 == (int)v.size() ? '\n' : ' ');
  for (int i = 1; i <= n; ++i) printf("%I64d%c", a[i], i == n ? '\n' : ' ');
  for (int i = 1; i <= n; ++i) printf("%d%c", lst[a[i]], i == n ? '\n' : ' ');
  for (int i = 1; i <= n; ++i) printf("%d%c", nxt[a[i]], i == n ? '\n' : ' ');
}
int main() {
  scanf("%d%I64d", &n, &K);
  sz = sqrt(n + eps);
  for (int i = 1; i <= n; ++i) scanf("%d", &t[i]);
  for (int i = 1; i <= n; ++i) {
    scanf("%I64d", &a[i]);
    pre[i] = pre[i - 1] + ((t[i] == 1) ? a[i] : -a[i]);
    v.push_back(pre[i]);
  }
  v.push_back(0);
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  for (int i = 0; i < (int)v.size(); ++i) mp[v[i]] = i + 1;
  for (int i = 0; i < (int)v.size(); ++i) {
    if (mp.count(v[i] - K)) lst[mp[v[i]]] = mp[v[i] - K];
    if (mp.count(v[i] + K)) nxt[mp[v[i]]] = mp[v[i] + K];
  }
  for (int i = 0; i <= n; ++i)
    a[i] = lower_bound(v.begin(), v.end(), pre[i]) - v.begin() + 1;
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    scanf("%d%d", &buc[i].l, &buc[i].r);
    buc[i].id = i;
  }
  sort(buc, buc + q);
  int l = buc[0].l, r = buc[0].l - 1;
  long long now = 0;
  for (int i = 0; i < q; ++i) {
    while (r < buc[i].r) {
      ++r;
      if (lst[a[r]]) now += cnt[lst[a[r]]];
      ++cnt[a[r]];
    }
    while (r > buc[i].r) {
      --cnt[a[r]];
      if (lst[a[r]]) now -= cnt[lst[a[r]]];
      --r;
    }
    while (l < buc[i].l) {
      --cnt[a[l]];
      if (nxt[a[l]]) now -= cnt[nxt[a[l]]];
      ++l;
    }
    while (l > buc[i].l) {
      --l;
      if (nxt[a[l]]) now += cnt[nxt[a[l]]];
      ++cnt[a[l]];
    }
    res[buc[i].id] = now + cnt[nxt[a[l - 1]]];
  }
  for (int i = 0; i < q; ++i) printf("%I64d\n", res[i]);
  return 0;
}
