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.

Sample test(s)
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 题解:http://tieba.baidu.com/p/2114943791?pid=28521015525&see_lz=1
code:
 #include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#define maxn 200005
using namespace std;
char ch;
int n,q,N,m,l,r,h[maxn],d[maxn],s[maxn],st[][maxn];
int SA[maxn],rank[maxn],t1[maxn],t2[maxn],height[maxn],sum[maxn];
struct DATA{
int v,id;
}list[maxn];
bool cmp(DATA a,DATA b){
if (a.v!=b.v) return a.v<b.v;
return a.id<b.id;
}
struct seg{
int tot,son[maxn*][],cnt[maxn*];
void init(){tot=N;}
void insert(int k,int p,int l,int r,int x){
if (l==r){cnt[k]=cnt[p]+;return;}
int m=(l+r)>>;
if (x<=m){
cnt[k]=cnt[p]+,son[k][]=++tot,son[k][]=son[p][];
insert(son[k][],son[p][],l,m,x);
}
else{
cnt[k]=cnt[p]+,son[k][]=son[p][],son[k][]=++tot;
insert(son[k][],son[p][],m+,r,x);
}
}
int query(int k,int l,int r,int x,int y){
if (!k||x>y) return ;
if (l==x&&r==y) return cnt[k];
int m=(l+r)>>;
if (y<=m) return query(son[k][],l,m,x,y);
else if (x<=m) return query(son[k][],l,m,x,m)+query(son[k][],m+,r,m+,y);
else return query(son[k][],m+,r,x,y);
}
int query(int l,int r,int x,int y){return query(r,,N,x,y)-query(l-,,N,x,y);}
}T;
bool ok;
void read(int &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
void get_SA(){
for (int i=;i<=N;i++) list[i]=(DATA){s[i],i};
sort(list+,list+N+,cmp);
for (int i=;i<=N;i++) SA[i]=list[i].id;
int *x=t1,*y=t2,tot=;
x[SA[]]=m=;
for (int i=;i<=N;i++){
if (s[SA[i]]!=s[SA[i-]]) m++;
x[SA[i]]=m;
}
for (int len=;tot<N;len<<=,m=tot){
tot=;
for (int i=N-len+;i<=N;i++) y[++tot]=i;
for (int i=;i<=N;i++) if (SA[i]>len) y[++tot]=SA[i]-len;
for (int i=;i<=m;i++) sum[i]=;
for (int i=;i<=N;i++) sum[x[y[i]]]++;
for (int i=;i<=m;i++) sum[i]+=sum[i-];
for (int i=N;i>=;i--) SA[sum[x[y[i]]]--]=y[i];
swap(x,y),x[SA[]]=tot=;
for (int i=;i<=N;i++){
if (y[SA[i]]!=y[SA[i-]]||y[SA[i]+len]!=y[SA[i-]+len]) tot++;
x[SA[i]]=tot;
}
}
for (int i=;i<=N;i++) rank[i]=x[i];
}
void get_height(){
for (int i=,j=;i<=N;i++){
if (rank[i]==) continue;
while (s[i+j]==s[SA[rank[i]-]+j]) j++;
height[rank[i]]=j;
if (j>) j--;
}
}
void prepare(){
for (int i=;i<=N;i++) st[][i]=height[i];
for (int i=;i<=;i++)
for (int j=;j<=N;j++){
st[i][j]=st[i-][j];
if (j+(<<(i-))<=N) st[i][j]=min(st[i][j],st[i-][j+(<<(i-))]);
}
T.init();
for (int i=;i<=N;i++) T.insert(i,i-,,N,SA[i]);
}
int calc(int l,int r){
if (l>r) swap(l,r);
int t=; l++;
if (l==r) return height[r];
for (;l+(<<t)<r;t++);
if (l+(<<t)>r) t--;
return min(st[t][l],st[t][r-(<<t)+]);
}
int find(int s,int x,int op){
int l,r,m;
if (op) l=s,r=N;else l=,r=s;
while (l!=r){
m=(l+r)>>;
if (op) m++;
if (calc(m,s)<x){
if (op) r=m-;
else l=m+;
}
else{
if (op) l=m;
else r=m;
}
}
return l;
}
void query(int l,int r){
int x=find(rank[l],r-l,),y=find(rank[l],r-l,);
printf("%d\n",T.query(x,y,n+,n+(l-)-(r-l))+T.query(x,y,n+(r+),N));
}
int main(){
read(n);
for (int i=;i<=n;i++) read(h[i]);
for (int i=;i<n;i++) d[i]=h[i+]-h[i];
for (int i=;i<n;i++) s[i]=d[i];
s[n]=;
for (int i=;i<n;i++) s[n+i]=-d[i];
N=(n<<)-;
get_SA(),get_height(),prepare();
for (read(q);q;q--){
read(l),read(r);
if (l==r) printf("%d\n",n-);
else query(l,r);
}
return ;
}

最新文章

  1. iOS 获取当前点击的坐标
  2. socket编程知识
  3. An exception occurred while acquiring a poolable resource. Will retry.
  4. (七)中介者模式-C++实现
  5. Kernel Methods - An conclusion
  6. Springmvc + mybatis + spring 配置,spring事物
  7. (九)errno和perror、标准IO
  8. 前端CSS参考阅读
  9. android 自定义组合控件 顶部导航栏
  10. Java中Integer的源码学习
  11. tar常用解包
  12. C#操作数据库,将其查查出来的记录条数显示在winform窗体中的方法之一
  13. cocos2d-x环境搭建(win7+cocos2d-x-3.0)
  14. 使用Oracle 9i工具管理数据库 - 初学者系列 - 学习者系列文章
  15. 对方网络ping不通
  16. .NET Core项目从xproj+project.json向csproj迁移简介
  17. javascript事件委托机制详解
  18. .NET Core 2.0和ASP.NET Core 2.0正式版抢先体验
  19. Java8内存模型—永久代(PermGen)和元空间(Metaspace)
  20. 【.net】未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序解决办法

热门文章

  1. SRM 507(2-1000pt)
  2. J - Invitation Cards
  3. windows server作为文件服务器如何精细控制权限
  4. java+ mysql 给所有的表添加假数据
  5. 关于URL重写
  6. ubuntu sublime安装及配置
  7. Toast的使用具体解释
  8. 通过GitHub Pages建立个人站点(详细步骤)
  9. HDU 3037(Lucas定理)
  10. Python开发【第十篇】:CSS --无内容点击-不进去(一)