#include <bits/stdc++.h>
using namespace std;
const long long sz = 320, C = 320;
inline bool read(long long &a) {
  char c;
  bool neg = 0;
  if (!(c = getchar())) {
    return 0;
  }
  a = 0;
  if (c == '-') {
    neg = 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    a = (a << 3) + (a << 1) + (c - '0');
    c = getchar();
  }
  if (neg) a *= -1;
  return 1;
}
struct q {
  long long l, r, uk;
};
vector<q> blocks[sz];
inline bool comp(const q &a, const q &b) { return a.r < b.r; }
long long t[1000000];
inline void solve() {
  long long n, k;
  read(n);
  read(k);
  long long a[n];
  for (long long i = 0; i < n; i++) {
    read(a[i]);
    if (a[i] == 2) {
      a[i] = -1;
    } else {
      a[i] = 1;
    }
  }
  long long pi[n + 1];
  pi[0] = 0;
  vector<pair<long long, long long> > all;
  all.push_back({0, 0});
  all.push_back({-k, n * 2});
  all.push_back({k, n * 4});
  for (long long i = 0; i < n; i++) {
    long long x;
    read(x);
    a[i] *= x;
    pi[i + 1] = pi[i] + a[i];
    all.push_back({pi[i + 1], i + 1});
    all.push_back({pi[i + 1] - k, i + 1 + n * 2});
    all.push_back({pi[i + 1] + k, i + 1 + n * 4});
  }
  sort(all.begin(), all.end());
  long long ar[n + 2][3];
  long long now = 0;
  for (long long i = 0; i < all.size(); i++) {
    if (i != 0 && all[i].first != all[i - 1].first) now = i;
    ar[all[i].second % (n * 2)][all[i].second / (n * 2)] = now;
  }
  long long q;
  read(q);
  for (long long i = 0; i < q; i++) {
    long long l, r;
    read(l);
    read(r);
    l--;
    blocks[l / C].push_back({l, r, i});
  }
  for (long long i = 0; i < sz; i++) {
    sort(blocks[i].begin(), blocks[i].end(), comp);
  }
  long long ans[n];
  t[ar[0][0]]++;
  long long tl = 0, tr = 0, cnt = 0;
  for (long long i = 0; i < sz; i++) {
    for (long long j = 0; j < blocks[i].size(); j++) {
      long long l = blocks[i][j].l, r = blocks[i][j].r;
      while (tr < r) {
        tr++;
        cnt += t[ar[tr][1]];
        t[ar[tr][0]]++;
      }
      while (tl < l) {
        t[ar[tl][0]]--;
        cnt -= t[ar[tl][2]];
        tl++;
      }
      while (tl > l) {
        tl--;
        cnt += t[ar[tl][2]];
        t[ar[tl][0]]++;
      }
      while (tr > r) {
        t[ar[tr][0]]--;
        cnt -= t[ar[tr][1]];
        tr--;
      }
      ans[blocks[i][j].uk] = cnt;
    }
  }
  for (long long i = 0; i < q; i++) {
    cout << ans[i] << "\n";
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  solve();
  return 0;
}
