#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000007;
const long long linf = 1ll * inf * inf;
const int N = 100000 + 7;
const int M = 30;
const int multipleTest = 0;
int n;
int t[N];
long long a[N];
int cnt[3 * N];
int cntPrv[3 * N];
long long ans[N];
long long res;
int k;
map<long long, int> id;
int getId(long long u) {
  if (id.count(u))
    return id[u];
  else {
    int x = (int)(id).size();
    return id[u] = x;
  }
}
int q;
pair<pair<int, int>, int> query[N];
const int MAGIC = 350;
bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  int f = a.first.first / MAGIC, s = b.first.first / MAGIC;
  if (f != s)
    return f < s;
  else {
    return a.first.second < b.first.second;
  }
}
int nxt[N], prv[N];
void solve() {
  cin >> n >> k;
  for (int i = (1), _b = (n); i <= _b; ++i) scanf("%d", t + i);
  for (int i = (1), _b = (n); i <= _b; ++i) {
    scanf("%lld", a + i);
    if (t[i] == 2) a[i] = -a[i];
    a[i] += a[i - 1];
  }
  cin >> q;
  for (int j = (0), _b = (q); j < _b; ++j) {
    scanf("%d%d", &query[j].first.first, &query[j].first.second);
    query[j].second = j;
  }
  sort(query, query + q, cmp);
  for (int i = n; i >= 0; --i) {
    if (i > 0) nxt[i] = getId(a[i - 1] + k);
    prv[i] = getId(a[i] - k);
    a[i] = getId(a[i]);
  }
  int curU = 1, curV = 1;
  cnt[a[1]] = 1;
  cntPrv[a[0]] = 1;
  res = cntPrv[prv[1]];
  for (int j = 0; j < q; ++j) {
    pair<pair<int, int>, int> qr = query[j];
    int u = qr.first.first;
    int v = qr.first.second;
    int id = qr.second;
    while (curV < v) {
      ++curV;
      cnt[a[curV]]++;
      cntPrv[a[curV - 1]]++;
      res += cntPrv[prv[curV]];
    }
    while (curU > u) {
      --curU;
      cnt[a[curU]]++;
      cntPrv[a[curU - 1]]++;
      res += cnt[nxt[curU]];
    }
    while (curU < u) {
      res -= cnt[nxt[curU]];
      cnt[a[curU]]--;
      cntPrv[a[curU - 1]]--;
      ++curU;
    }
    while (curV > v) {
      res -= cntPrv[prv[curV]];
      cnt[a[curV]]--;
      cntPrv[a[curV - 1]]--;
      --curV;
    }
    ans[id] = res;
  }
  for (int j = (0), _b = (q); j < _b; ++j) printf("%lld\n", ans[j]);
}
int main() {
  int Test = 1;
  if (multipleTest) {
    cin >> Test;
  }
  for (int i = 0; i < Test; ++i) {
    solve();
  }
}
