Codeforces Round 144 (Div. 1)
D
Fence
Codeforces
2900
原题
← CE →
翻译中…

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 $*h*_{*i*}$. 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 $*l*_{1}$ to $*r*_{1}$ and from $*l*_{2}$ to $*r*_{2}$ 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* ≤ *r*_{1} - *l*_{1})$ the following condition holds: $*h*_{*l*_{1} + *i*} + *h*_{*l*_{2} + *i*} = *h*_{*l*_{1}} + *h*_{*l*_{2}}$.
翻译中…

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* ≤ 10^{5}$) — the number of wood planks in the fence. The second line contains $*n*$ space-separated integers $*h*_{1}, *h*_{2}, ..., *h*_{*n*}$ ($1 ≤ *h*_{*i*} ≤ 10^{9}$) — the heights of fence planks.

翻译中…

The third line contains integer $*q*$ ($1 ≤ *q* ≤ 10^{5}$) — the number of queries. Next $*q*$ lines contain two space-separated integers $*l*_{*i*}$ and $*r*_{*i*}$ ($1 ≤ *l*_{*i*} ≤ *r*_{*i*} ≤ *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.

翻译中…

样例

Input #1
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
1
2
2
0
2
9
InputCopy OutputCopy
题目信息
题号
232D
时间限制
3 s
内存限制
256 MB
标签
二分
数据结构
后缀结构

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。