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 ;
}

codeforces 232D Fence的更多相关文章

  1. codeforces 448CPainting Fence

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/y990041769/article/details/37935237 题目:codeforces 4 ...

  2. Fence(codeforces 232D)

    题意: 对于给定的a[1..n],定义区间[s,t]和[x,y]"匹配"当且仅当下列条件同时满足:1. t-s=y-x,即长度相同.3. t<x或s>y,即两区间没有交 ...

  3. CodeForces 363B Fence

    Fence Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ...

  4. codeforces B.Fence 解题报告

    题目链接:http://codeforces.com/problemset/problem/363/B 题目意思:给定整数n和k,需要从n个数中找出连续的k个数之和最小,输出这连续的k个数中的第一个数 ...

  5. Codeforces 659G Fence Divercity dp

    Fence Divercity 我们设a[ i ] 为第 i 个围栏被切的最靠下的位置, 我们发现a[ i ] 的最大取值有一下信息: 如果从i - 1过来并在 i  结束a[ i ] = min(h ...

  6. codeforces 349B Color the Fence 贪心,思维

    1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...

  7. Codeforces 484E Sign on Fence(是持久的段树+二分法)

    题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于 ...

  8. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) E. Sign on Fence

    http://codeforces.com/contest/484/problem/E 题意: 给出n个数,查询最大的在区间[l,r]内,长为w的子区间的最小值 第i棵线段树表示>=i的数 维护 ...

  9. Codeforces Round #355 (Div. 2) A. Vanya and Fence 水题

    A. Vanya and Fence 题目连接: http://www.codeforces.com/contest/677/problem/A Description Vanya and his f ...

随机推荐

  1. Apache multiple domains setup

    Running several name-based web sites on a single IP address. Your server has a single IP address, an ...

  2. [Java] Map / HashMap - 源代码学习笔记

    Map 1. 用于关联 key 和 value 的对象,其中 key 与 key 之间不能重复. 2. 是一个接口,用来代替 Java 早期版本中的 Dictionary 抽象类. 3. 提供三种不同 ...

  3. Timeout for tests

    如果想把timeout太久的测试自动标记为失败,有两种方法: 1.在 @Test里加上 Timeout 参数 定义"timeout=1000“的话,如果超过1000 毫秒,failure会被 ...

  4. java中的代理

    package cn.itcast.day3; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationHan ...

  5. linux —— 学习笔记(汇总)

    笔记目录:一.系统知识 和 基本概念                    二.常用操作                   三.系统管理(内存.设备.服务等管理)                   ...

  6. 如何定制Windows系统右键菜单

    今天心血来潮把几个自己常用的工具定制到了系统的右键菜单.包括notepad++,7zip,还有复制文件全路径和文件夹路径.下面简单介绍一下步骤. 1. Windows系统右键菜单对应的注册表位置 Wi ...

  7. Ken Norton和软件工程师打交道的10个秘诀

    How to work with software engineers - by Ken Norton Deflect praise Absorb blame Sweat the details In ...

  8. C# 对JS编码/解码进行转换

    public static class Extension { #region [编码/解码统一转换] /// <summary> /// /// </summary> /// ...

  9. [Angular 2] ng-model and ng-for with Select and Option elements

    You can use Select and Option elements in combination with ng-for and ng-model to create mini-forms ...

  10. 用 Java 技术创建 RESTful Web 服务--转载

    简介 JAX-RS (JSR-311) 是为 Java EE 环境下的 RESTful 服务能力提供的一种规范.它能提供对传统的基于 SOAP 的 Web 服务的一种可行替代. 在本文中,了解 JAX ...