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. VC++ 动态检测串口的热插拔(一)通过注册表实现

    在上一篇文章中讲述了如何通过循环遍历的方法获取可用串口,可是这样的方法过于暴力,难免会想有没有其他的办法那,嘿嘿,那是肯定会有的,不管什么问题,解决问题的方法永远都不止一种.下面讲述如何通过注册表来获 ...

  2. 谈JSON在Ajax中的使用

    JSON是一种轻量级的数据交换格式.易于人阅读和编写,同时也易于机器解析和生成.AJAX是一种用于创建更好更快以及交互性更强的 Web 应用程序的技术.之前也曾介绍过在PHP语言中使用JSON的文章, ...

  3. DCL双检查锁机制实现的线程安全的单例模式

    public class MyObject { private volatile static MyObject myObject; private MyObject(){} public stati ...

  4. PHP学习之[第10讲]PHP 的 Mysql 数据库函数 (微型博客系统)II

    mysql结构如下: -- phpMyAdmin SQL Dump -- version 4.4.1.1 -- http://www.phpmyadmin.net -- -- Host: localh ...

  5. Appium官网Introduction

    英文官网:http://appium.io/introduction.html?lang=zh Appium 简介 Appium是一个开源的自动化测试工具,其支持iOS和安卓平台上的原生的,基于移动浏 ...

  6. PHP安全编程:HTTP请求欺骗(转)

    一个比欺骗表单更高级和复杂的攻击方式是HTTP请求欺骗.这给了攻击者完全的控制权与灵活性,它进一步证明了不能盲目信任用户提交的任何数据. 为了演示这是如何进行的,请看下面位于http://exampl ...

  7. 《Android开发艺术探索》读书笔记 (1) 第1章 Activity的生命周期和启动模式

    第1章 Activity的生命周期和启动模式 1.1 Activity生命周期全面分析 1.1.1 典型情况下生命周期分析(1)一般情况下,当当前Activity从不可见重新变为可见状态时,onRes ...

  8. Java基础知识强化之IO流笔记10:File类输出指定目录下指定后缀名的文件名称案例(File类的文件过滤器方法改进list( FilenameFilter ff))

    1. 案例: 判断F盘下是否有后缀名为.jpg的文件,如果有的话,就输出这个文件名. 2. 案例代码如下: (1)思路是:先获取所有的文件和文件夹封装的对象,然后遍历的时候,依次判断,如果满足条件就输 ...

  9. Codeforces Round #291 (Div. 2) C - Watto and Mechanism 字符串

    [题意]给n个字符串组成的集合,然后有m个询问(0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) ,每个询问都给出一个字符串s,问集合中是否存在一个字符串t,使得s和t长度相同,并且仅有一个 ...

  10. centos7 部署ssserver

    centos7 部署shadowsocks服务端 为什么要选centos7? 以后centos7 肯定是主流,在不重要的环境还是尽量使用新系统吧 centos7 的坑 默认可能会有firewall 或 ...