#include <bits/stdc++.h>
using namespace std;
int n, k, vc = -1, t[100005], le[100005], ri[100005];
pair<long long, int> p[100005];
long long v[100005], ans[100005], cur, cnt[100005];
pair<pair<int, int>, pair<int, int>> q[100005];
int ta, tb;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", t + i);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &ta);
    if (t[i] == 2) ta = -ta;
    p[i] = {p[i - 1].first + ta, i};
  }
  sort(p, p + n + 1);
  for (int i = 0; i <= n; i++) {
    if (i == 0 || p[i].first != p[i - 1].first) v[++vc] = p[i].first;
    t[p[i].second] = vc;
  }
  v[++vc] = LLONG_MAX;
  ta = tb = -1;
  for (int i = 0; i < vc; i++) {
    while (v[ta + 1] <= v[i] - k) ta++;
    while (v[tb + 1] <= v[i] + k) tb++;
    if (ta != -1 && v[ta] == v[i] - k)
      le[i] = ta;
    else
      le[i] = vc;
    if (tb != -1 && v[tb] == v[i] + k)
      ri[i] = tb;
    else
      ri[i] = vc;
  }
  scanf("%d", &k);
  for (int i = 0; i < k; i++) {
    scanf("%d%d", &q[i].second.first, &q[i].first.second);
    q[i].second.first--;
    q[i].first.first = q[i].second.first / 320;
    q[i].second.second = i;
  }
  sort(q, q + k);
  ta = tb = 0;
  cnt[t[0]]++;
  for (int i = 0; i < k; i++) {
    while (q[i].second.first < ta) {
      ta--;
      cur += cnt[ri[t[ta]]];
      cnt[t[ta]]++;
    }
    while (q[i].first.second > tb) {
      tb++;
      cur += cnt[le[t[tb]]];
      cnt[t[tb]]++;
    }
    while (q[i].second.first > ta) {
      cnt[t[ta]]--;
      cur -= cnt[ri[t[ta]]];
      ta++;
    }
    while (q[i].first.second < tb) {
      cnt[t[tb]]--;
      cur -= cnt[le[t[tb]]];
      tb--;
    }
    ans[q[i].second.second] = cur;
  }
  for (int i = 0; i < k; i++) printf("%lld\n", ans[i]);
  return 0;
}
