#include <bits/stdc++.h>
using namespace std;
const int seed = 131;
const int maxn = 5e5 + 5;
const int mod = 1e9 + 7;
long long n, m, k, block;
long long pre[maxn];
struct node {
  long long l, r, id;
  bool operator<(const node &a) const {
    if (l / block != a.l / block) return l / block < a.l / block;
    return r < a.r;
  }
} e[maxn];
long long ans[maxn];
long long num, prek[maxn], nxtk[maxn], sum[maxn];
long long cnt[maxn * 5];
long long l, r;
long long t[maxn * 5];
void addr(long long r) {
  long long pos = prek[r];
  num += cnt[pos];
  cnt[pre[r]]++;
}
void delr(long long r) {
  long long pos = prek[r];
  cnt[pre[r]]--;
  num -= cnt[pos];
}
void addl(long long l) {
  long long pos = nxtk[l];
  num += cnt[pos];
  cnt[pre[l]]++;
}
void dell(long long l) {
  long long pos = nxtk[l];
  cnt[pre[l]]--;
  num -= cnt[pos];
}
long long a[maxn];
long long tot;
int main() {
  scanf("%I64d%I64d", &n, &k);
  block = sqrt(n);
  for (long long i = 1; i <= n; i++) {
    scanf("%I64d", &a[i]);
    if (a[i] == 2) a[i] = -1;
  }
  for (long long i = 1; i <= n; i++) {
    scanf("%I64d", &sum[i]);
    sum[i] = sum[i - 1] + a[i] * sum[i];
    t[++tot] = sum[i];
    t[++tot] = sum[i] - k;
    t[++tot] = sum[i] + k;
  }
  t[++tot] = -k;
  t[++tot] = k;
  t[++tot] = 0;
  sort(t + 1, t + 1 + tot);
  long long len = unique(t + 1, t + 1 + tot) - t - 1;
  for (long long i = 0; i <= n; i++) {
    prek[i] = lower_bound(t + 1, t + 1 + len, sum[i] - k) - t;
    nxtk[i] = lower_bound(t + 1, t + 1 + len, sum[i] + k) - t;
    pre[i] = lower_bound(t + 1, t + 1 + len, sum[i]) - t;
  }
  scanf("%I64d", &m);
  for (long long i = 1; i <= m; i++) {
    scanf("%I64d%I64d", &e[i].l, &e[i].r);
    e[i].id = i;
    e[i].l--;
  }
  sort(e + 1, e + 1 + m);
  l = 0;
  r = 0;
  cnt[lower_bound(t + 1, t + 1 + len, 0) - t] = 1;
  for (long long i = 1; i <= m; i++) {
    while (r < e[i].r) addr(++r);
    while (r > e[i].r) delr(r--);
    while (l < e[i].l) dell(l++);
    while (l > e[i].l) addl(--l);
    ans[e[i].id] = num;
  }
  for (long long i = 1; i <= m; i++) {
    printf("%I64d\n", ans[i]);
  }
  return 0;
}
