CF232D.Fence

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

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

Two pieces of the fence from l1l_{1} to r1r_{1} and from l2l_{2} to r2r_{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 ii (0<=i<=r1l1)(0<=i<=r_{1}-l_{1}) the following condition holds: hl1+i+hl2+i=hl1+hl2h_{l1+i}+h_{l2+i}=h_{l1}+h_{l2} .

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.

输入格式

The first line contains integer nn ( 1<=n<=1051<=n<=10^{5} ) — the number of wood planks in the fence. The second line contains nn space-separated integers h1,h2,...,hnh_{1},h_{2},...,h_{n} ( 1<=hi<=1091<=h_{i}<=10^{9} ) — the heights of fence planks.

The third line contains integer qq ( 1<=q<=1051<=q<=10^{5} ) — the number of queries. Next qq lines contain two space-separated integers lil_{i} and rir_{i} ( 1<=li<=ri<=n1<=l_{i}<=r_{i}<=n ) — the boundaries of the ii -th piece of the fence.

输出格式

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.

输入输出样例

  • 输入#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
    

    输出#1

    1
    2
    2
    0
    2
    9
    
首页