#include <bits/stdc++.h>
using namespace std;
const int N = 1234567;
int t[N], a[N];
long long qq[N];
map<long long, int> ma;
int have[N];
long long pre[N], preu[N], pred[N];
int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", t + i);
    if (t[i] == 2) t[i] = -1;
  }
  preu[0] = k;
  pred[0] = -k;
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
    pre[i + 1] = t[i] * a[i];
    pre[i + 1] += pre[i];
    preu[i + 1] = pre[i + 1] + k;
    pred[i + 1] = pre[i + 1] - k;
    ma[pre[i + 1]];
    ma[preu[i + 1]];
    ma[pred[i + 1]];
  }
  ma[pre[0]];
  ma[preu[0]];
  ma[pred[0]];
  int sz = 0;
  for (auto &it : ma) {
    it.second = sz++;
  }
  for (int i = 0; i <= n; i++) {
    pre[i] = ma[pre[i]];
    preu[i] = ma[preu[i]];
    pred[i] = ma[pred[i]];
  }
  int q;
  scanf("%d", &q);
  int block = (int)sqrt(n + .0) + 1;
  vector<pair<pair<int, int>, int> > qs;
  for (int i = 0; i < q; i++) {
    int l, r;
    scanf("%d %d", &l, &r);
    qs.emplace_back(make_pair(l, r), i);
  }
  sort(qs.begin(), qs.end(),
       [&](const pair<pair<int, int>, int> &a,
           const pair<pair<int, int>, int> &b) {
         if (a.first.first / block == b.first.first / block)
           return a.first.second < b.first.second;
         return a.first.first / block < b.first.first / block;
       });
  int ml = 1, mr = 0;
  long long ans = 0;
  have[pre[0]] = 1;
  for (int i = 0; i < (int)qs.size(); i++) {
    int l = qs[i].first.first, r = qs[i].first.second, id = qs[i].second;
    while (mr < r) {
      mr++;
      ans += have[pred[mr]];
      have[pre[mr]]++;
    }
    while (ml < l) {
      have[pre[ml - 1]]--;
      ans -= have[preu[ml - 1]];
      ml++;
    }
    while (r < mr) {
      have[pre[mr]]--;
      ans -= have[pred[mr]];
      mr--;
    }
    while (l < ml) {
      ml--;
      ans += have[preu[ml - 1]];
      have[pre[ml - 1]]++;
    }
    qq[id] = ans;
  }
  for (int i = 0; i < q; i++) {
    printf("%lld\n", qq[i]);
  }
  return 0;
}
