232_D. Fence

John Doe has a crooked fence, consisting of n rectangular planks, lined up from the left to the right: the plank that goes i-th (1 ≤ i ≤ n) (from left to right) has width 1 and height hi. We will assume that the plank that goes i-th (1 ≤ i ≤ n) (from left to right) has index i.

A piece of the fence from l to r (1 ≤ l ≤ r ≤ n) is a sequence of planks of wood with indices from l to r inclusive, that is, planks with indices l, l + 1, ..., r. The width of the piece of the fence from l to r is value r - l + 1.

Two pieces of the fence from l1 to r1 and from l2 to r2 are called matching, if the following conditions hold:

  * the pieces do not intersect, that is, there isn't a single plank, such that it occurs in both pieces of the fence; 
  * the pieces are of the same width; 
  * for all i (0 ≤ i ≤ r1 - l1) the following condition holds: hl1 + i + hl2 + i = hl1 + hl2. 



John chose a few pieces of the fence and now wants to know how many distinct matching pieces are for each of them. Two pieces of the fence are distinct if there is a plank, which belongs to one of them and does not belong to the other one.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of wood planks in the fence. The second line contains n space-separated integers h1, h2, ..., hn (1 ≤ hi ≤ 109) — the heights of fence planks.

The third line contains integer q (1 ≤ q ≤ 105) — the number of queries. Next q lines contain two space-separated integers li and ri (1 ≤ li ≤ ri ≤ n) — the boundaries of the i-th piece of the fence.

Output

For each query on a single line print a single integer — the number of pieces of the fence that match the given one. Print the answers to the queries in the order, in which the queries are given in the input.

Examples

Input

10
1 2 2 1 100 99 99 100 100 100
6
1 4
1 2
3 4
1 5
9 10
10 10


Output

1
2
2
0
2
9

Example input:
10
1 2 2 1 100 99 99 100 100 100
6
1 4
1 2
3 4
1 5
9 10
10 10


Example output:
1
2
2
0
2
9


Tags: binary search
data structures
string suffix structures
Difficulty rating: 2900

Solution:
#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000002, N = 200100;
int a[N], r[N], sa[N], h[N], ans[N], L[N], R[N], _l[N], _r[N], C[N], n, t, cnt,
    Q;
pair<pair<int, int>, int> c[N];
vector<pair<int, int> > q[N];
vector<int> q1[N], q2[N];
set<pair<int, int> > p;
void deal(int i, int w, int *a) {
  for (typeof(p.end()) it = p.upper_bound(make_pair(h[i] + 1, -1));
       it != p.end(); p.erase(it++))
    a[it->second] = i + w;
}
int A(int x) {
  int s = 0;
  for (; x; x -= x & -x) s += C[x];
  return s;
}
void M(int x) {
  for (; x <= t; x += x & -x) C[x]++;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (int)n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < (int)n - 1; i++) a[i] = a[i + 1] - a[i], a[i + n] = -a[i];
  a[n - 1] = -inf + 1;
  a[(t = n + n) - 1] = -inf + 2;
  for (int i = 0; i < (int)t; i++) r[i] = a[i] += inf;
  for (int d = 1; d < t; d += d) {
    for (int i = 0; i < (int)t; i++)
      c[i] = make_pair(make_pair(r[i], r[i + d]), i);
    sort(c, c + t);
    cnt = r[c[0].second] = 0;
    for (int i = 0; i < (int)t - 1; i++)
      r[c[i + 1].second] = (cnt += (c[i].first != c[i + 1].first));
    if (cnt == t) break;
  }
  for (int i = 0; i < (int)t; i++) sa[r[i]] = i;
  for (int k = 0, i = 0, j; i < t; h[r[i++]] = k, k = max(k - 1, 0))
    if (r[i] != 0)
      for (j = sa[r[i] - 1]; a[j + k] == a[i + k]; k++)
        ;
  h[t] = -1;
  scanf("%d", &Q);
  for (int i = 0; i < (int)Q; i++)
    scanf("%d%d", L + i, R + i), --L[i], --R[i],
        q[r[L[i] + n]].push_back(make_pair(R[i] - L[i], i));
  for (int i = 0; i < (int)t + 1; i++) {
    deal(i, -1, _r);
    for (int j = 0; j < (int)q[i].size(); j++) p.insert(q[i][j]);
  }
  for (int i = t - 1; i >= 0; i--) {
    for (int j = 0; j < (int)q[i].size(); j++) p.insert(q[i][j]);
    deal(i, 0, _l);
  }
  for (int i = 0; i < (int)Q; i++)
    q1[_l[i]].push_back(i), q2[_r[i]].push_back(i),
        L[i] = max(2 * L[i] - R[i], 0);
  for (int i = 0; i < (int)t; i++) {
    for (int j = 0; j < (int)q1[i].size(); j++) {
      int d = q1[i][j];
      ans[d] -= A(n) - A(R[d] + 1) + A(L[d]);
    }
    M(sa[i] + 1);
    for (int j = 0; j < (int)q2[i].size(); j++) {
      int d = q2[i][j];
      ans[d] += A(n) - A(R[d] + 1) + A(L[d]);
    }
  }
  for (int i = 0; i < (int)Q; i++) printf("%d\n", ans[i]);
}