#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18L;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int Mod = 1e9 + 7;
struct Node {
  int l, r, id;
} p[maxn];
int n, k, q;
int tot, t[maxn];
long long sum[maxn], h[maxn * 3], ans[maxn];
int t1[maxn], t2[maxn], t3[maxn], num[maxn * 3], block;
bool cmp(Node A, Node B) {
  if (A.l / block == B.l / block) return A.r < B.r;
  return A.l < B.l;
}
void Mo() {
  long long last = 0;
  int L = 1, R = 0;
  for (int i = 1; i <= q; i++) {
    while (L < p[i].l) {
      last -= num[t2[L - 1]];
      num[t1[L]]--;
      L++;
    }
    while (L > p[i].l) {
      L--;
      num[t1[L]]++;
      last += num[t2[L - 1]];
    }
    while (R < p[i].r) {
      R++;
      last += num[t3[R]];
      if (sum[R] - sum[L - 1] == k) last++;
      num[t1[R]]++;
    }
    while (R > p[i].r) {
      num[t1[R]]--;
      last -= num[t3[R]];
      if (sum[R] - sum[L - 1] == k) last--;
      R--;
    }
    ans[p[i].id] = last;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  block = sqrt(n);
  for (int i = 1; i <= n; i++) cin >> t[i];
  for (int i = 1; i <= n; i++) {
    cin >> sum[i];
    if (t[i] == 2)
      sum[i] = -sum[i] + sum[i - 1];
    else
      sum[i] += sum[i - 1];
    h[++tot] = sum[i];
    h[++tot] = sum[i] + k;
    h[++tot] = sum[i] - k;
  }
  h[++tot] = 0;
  h[++tot] = -k;
  h[++tot] = k;
  sort(h + 1, h + tot + 1);
  tot = unique(h + 1, h + tot + 1) - (h + 1);
  for (int i = 0; i <= n; i++) {
    t1[i] = lower_bound(h + 1, h + tot + 1, sum[i]) - h;
    t2[i] = lower_bound(h + 1, h + tot + 1, sum[i] + k) - h;
    t3[i] = lower_bound(h + 1, h + tot + 1, sum[i] - k) - h;
  }
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> p[i].l >> p[i].r;
    p[i].id = i;
  }
  sort(p + 1, p + q + 1, cmp);
  Mo();
  for (int i = 1; i <= q; i++) cout << ans[i] << endl;
  return 0;
}
