#include <bits/stdc++.h>
using namespace std;
int n, k, qu, t[100005];
long long a[100005], ret[100005], psum[100005], sufsum[100005];
struct info {
  int l, r, idx;
  bool operator<(const info &i) const { return r < i.r; }
};
vector<info> query[100005];
vector<long long> val;
int LQ[100005], RQ[100005], me[100005], me2[100005], lcnt[200005], rcnt[200005];
int main() {
  scanf("%d %d", &n, &k);
  int sz = sqrt(n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &t[i]);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (t[i] == 2) a[i] = -a[i];
    psum[i] = psum[i - 1] + a[i];
    val.push_back(psum[i]);
  }
  val.push_back(0);
  for (int i = n; i >= 0; i--) {
    sufsum[i] = sufsum[i + 1] + a[i];
    val.push_back(sufsum[i]);
  }
  sort(val.begin(), val.end());
  val.erase(unique(val.begin(), val.end()), val.end());
  for (int i = 1; i <= n; i++) {
    auto it = lower_bound(val.begin(), val.end(), psum[i]);
    me[i] = it - val.begin();
    it = lower_bound(val.begin(), val.end(), sufsum[i]);
    me2[i] = it - val.begin();
    it = lower_bound(val.begin(), val.end(), sufsum[i + 1] + k);
    if (it != val.end() && *it == sufsum[i + 1] + k)
      RQ[i] = it - val.begin();
    else
      RQ[i] = -1;
    it = lower_bound(val.begin(), val.end(), psum[i - 1] + k);
    if (it != val.end() && *it == psum[i - 1] + k)
      LQ[i] = it - val.begin();
    else
      LQ[i] = -1;
  }
  scanf("%d", &qu);
  for (int l, r, i = 1; i <= qu; i++) {
    scanf("%d %d", &l, &r);
    query[l / sz].push_back({l, r, i});
  }
  for (int i = 0; i <= 100000; i++) {
    if (query[i].empty()) continue;
    sort(query[i].begin(), query[i].end());
    int lpos = query[i][0].l, rpos = lpos - 1;
    long long ans = 0;
    for (int j = 0; j < query[i].size(); j++) {
      while (rpos < query[i][j].r) {
        ++rpos;
        rcnt[me2[rpos]]++;
        lcnt[me[rpos]]++;
        if (RQ[rpos] != -1) ans += rcnt[RQ[rpos]];
      }
      while (lpos > query[i][j].l) {
        --lpos;
        rcnt[me2[lpos]]++;
        lcnt[me[lpos]]++;
        if (LQ[lpos] != -1) ans += lcnt[LQ[lpos]];
      }
      while (lpos < query[i][j].l) {
        if (LQ[lpos] != -1) ans -= lcnt[LQ[lpos]];
        rcnt[me2[lpos]]--;
        lcnt[me[lpos]]--;
        ++lpos;
      }
      ret[query[i][j].idx] = ans;
    }
    while (lpos <= rpos) {
      rcnt[me2[lpos]]--;
      lcnt[me[lpos]]--;
      ++lpos;
    }
  }
  for (int i = 1; i <= qu; i++) {
    cout << ret[i] << endl;
  }
}
