题意:

对于给定的a[1..n],定义区间[s,t]和[x,y]"匹配"当且仅当下列条件同时满足:
1. t-s=y-x,即长度相同。
3. t<x或s>y,即两区间没有交。
2. 对任0<=i<=t-s,有a[s]+a[x]=a[s+i]+a[x+i]。
现给出a[1..n]和Q个询问(x,y),求与[x,y]匹配的区间的个数。

/*
写了n个小时,还没有调出来,Orz...
代码量倒是不大,但是细节巨多,已经弃疗了。。。 先差分一下, 然后题目就变成了,也就是这段区间取相反数之后可以与原区间匹配。
设当前询问为(x,y),把整个串取相反数,再复制到后面,用后缀数组向上向下二分出可行区间。
然后要求不可重叠,就是求rank在(l,r)中有多少串的位置在一个区间内,用主席树搞一搞。
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#define N 200010
using namespace std;
int id[N],a[N],b[N],s[N],t1[N],t2[N],c[N],sa[N],rank[N],height[N],n;
int lg2[N],st[N][],sum[N*],son[N*][],root[N],cnt;
struct node{
int x,num;
bool operator<(const node&s1)const{
if(x==s1.x) return num<s1.num;
return x<s1.x;
}
}r[N];
bool cmp(int *y,int a,int b,int k){
int a1=y[a],b1=y[b];
int a2=a+k<=n?y[a+k]:-;
int b2=b+k<=n?y[b+k]:-;
return a1==b1&&a2==b2;
}
void DA(){
int *x=t1,*y=t2,m=;
for(int i=;i<=n;i++) r[i]=(node){s[i],i};
sort(r+,r+n+);
for(int i=;i<=n;i++) sa[i]=r[i].num;
x[sa[]]=;
for(int i=;i<=n;i++) x[sa[i]]=r[i].x==r[i-].x?m:++m;
for(int k=,p=;k<=n;k*=,m=p,p=){
for(int i=n-k+;i<=n;i++) y[++p]=i;
for(int i=;i<=n;i++) if(sa[i]>k) y[++p]=sa[i]-k;
for(int i=;i<=m;i++) c[i]=;
for(int i=;i<=n;i++) c[x[y[i]]]++;
for(int i=;i<=m;i++) c[i]+=c[i-];
for(int i=n;i;i--) sa[c[x[y[i]]]--]=y[i];
swap(x,y);p=;x[sa[]]=;
for(int i=;i<=n;i++)
if(cmp(y,sa[i-],sa[i],k)) x[sa[i]]=p;
else x[sa[i]]=++p;
if(p>=n) break;
}
}
void geth(){
for(int i=;i<=n;i++) rank[sa[i]]=i;
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--;
}
for(int i=;i<=n;i++) lg2[i]=lg2[i>>]+;
for(int i=;i<=n;i++) st[i][]=height[i];
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<=n;i++)
st[i][j]=min(st[i][j-],st[i+(<<j-)][j-]);
}
void insert(int &k,int last,int l,int r,int pos){
k=++cnt;
son[k][]=son[last][];
son[k][]=son[last][];
sum[k]=sum[last]+;
if(l==r) return;
int mid=l+r>>;
if(pos<=mid) insert(son[k][],son[last][],l,mid,pos);
else insert(son[k][],son[last][],mid+,r,pos);
}
int query(int k,int l,int r,int x,int y){
if(l>=x&&r<=y) return sum[k];
int mid=l+r>>,tot=;
if(x<=mid) tot+=query(son[k][],l,mid,x,y);
if(y>mid) tot+=query(son[k][],mid+,r,x,y);
return tot;
}
int querymn(int x,int y){
int k=lg2[y-x+];
return min(st[x][k],st[y-(<<k)+][k]);
}
int work(int x,int y){
int pos=rank[x],l=,r=pos,tmp1=pos,tmp2=pos;
while(l<r-){
int mid=l+r>>;
if(querymn(mid+,pos)>=y-x+) r=mid,tmp1=mid;
else l=mid;
}
l=pos,r=n+;
while(l<r-){
int mid=l+r>>;
if(querymn(pos+,mid)>=y-x+) l=mid,tmp2=mid;
else r=mid;
}
int ans1=query(root[tmp2],,n+,x+n/+,y+n/+)-query(root[tmp1-],,n+,x+n/+,y+n/+);
int ans2=query(root[tmp2],,n,,n/)-query(root[tmp1-],,n,,n/);
return tmp2-tmp1+-ans1-ans2;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<n;i++) s[i]=a[i+]-a[i],s[i+n]=-s[i];s[n]=-1e9;
n=n*-;DA();geth();
for(int i=;i<=n;i++)
insert(root[i],root[i-],,n,sa[i]);
int Q;scanf("%d",&Q);
while(Q--){
int x,y;scanf("%d%d",&x,&y);
if(x==y){
printf("%d\n",n/);
continue;
}
printf("%d\n",work(x,y-));
}
return ;
}

Fence(codeforces 232D)的更多相关文章

  1. 【CF484E】Sign on Fence(主席树)

    [CF484E]Sign on Fence(主席树) 题面 懒得贴CF了,你们自己都找得到 洛谷 题解 这不就是[TJOI&HEOI 排序]那题的套路吗... 二分一个答案,把大于答案的都变成 ...

  2. (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)

    (CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...

  3. Painting The Fence(贪心+优先队列)

    Painting The Fence(贪心+优先队列) 题目大意:给 m 种数字,一共 n 个,从前往后填,相同的数字最多 k 个在一起,输出构造方案,没有则输出"-1". 解题思 ...

  4. Sorted Adjacent Differences(CodeForces - 1339B)【思维+贪心】

    B - Sorted Adjacent Differences(CodeForces - 1339B) 题目链接 算法 思维+贪心 时间复杂度O(nlogn) 1.这道题的题意主要就是让你对一个数组进 ...

  5. CF448C Painting Fence (分治递归)

    Codeforces Round #256 (Div. 2) C C. Painting Fence time limit per test 1 second memory limit per tes ...

  6. (CodeForces 558C) CodeForces 558C

    题目链接:http://codeforces.com/problemset/problem/558/C 题意:给出n个数,让你通过下面两种操作,把它们转换为同一个数.求最少的操作数. 1.ai = a ...

  7. SPOJ:House Fence(分治&DP)

    "Holiday is coming, holiday is coming, hurray hurray!" shouts Joke in the last day of his ...

  8. [题解]Yet Another Subarray Problem-DP 、思维(codeforces 1197D)

    题目链接:https://codeforces.com/problemset/problem/1197/D 题意: 给你一个序列,求一个子序列 a[l]~a[r] 使得该子序列的 sum(l,r)-k ...

  9. 【Codeforces】【图论】【数量】【哈密顿路径】Fake bullions (CodeForces - 804F)

    题意 有n个黑帮(gang),每个黑帮有siz[i]个人,黑帮与黑帮之间有有向边,并形成了一个竞赛完全图(即去除方向后正好为一个无向完全图).在很多年前,有一些人参与了一次大型抢劫,参与抢劫的人都获得 ...

随机推荐

  1. SpringBoot学习4:springboot整合listener

    整合方式一:通过注解扫描完成 Listener 组件的注册 1.编写listener package com.bjsxt.listener; import javax.servlet.ServletC ...

  2. MySQL - UNION 和 UNION ALL 操作符

    UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 SE ...

  3. 多页应用 Webpack4 配置优化与踩坑记录

    前言 最近新起了一个多页项目,之前都未使用 webpack4 ,于是准备上手实践一下.这篇文章主要就是一些配置介绍,对于正准备使用 webpack4 的同学,可以做一些参考. webpack4 相比之 ...

  4. TP5数据库操作方法总结

    一.TP5数据库操作方法 1.name()方法        作用 : 指定默认的数据表名(不含前缀)        示例 : Db::name('weiba_post');        返回 : ...

  5. mysql 5.7初始化默认密码错误

    下载了一个mysql 5.7.17的安装包后,安装后怎么都启动不了,好在mysql安装是成功了,没办法只有使用命令行重新初始化设置了 我的mysql安装根目录为:C:\Program Files\My ...

  6. javaWeb开发中常见的问题

    1.修改表单提交的时候不好使可能是因为没写对应隐藏域的ID 2.el表达式在js代码中要加“”,例如 "${}" 3.JavaScript中的函数也有重载的特性.如果两个input ...

  7. LightOJ - 1341 Aladdin and the Flying Carpet(数论)

    题意 有一块矩形(也可能是正方形)的飞毯. 给定飞毯的面积\(n\)和最小可能的边长\(a\),求可能有多少种不同边长的飞毯.(\(1<=a<=n<=1e12\)) 如面积\(n=6 ...

  8. Kubespray部署Kubernetes 1.13.0(使用本地镜像仓库)

    1. 下载kubespray # git clone https://github.com/kubernetes-sigs/kubespray.git # cd kubespray # pip ins ...

  9. Python 有序字典简介

    Table of Contents 1. 有序字典-OrderedDict简介 1.1. 示例 1.2. 相等性 1.3. 注意 2. 参考资料 有序字典-OrderedDict简介 示例 有序字典和 ...

  10. appcompat_v7\res\values-v21\themes_base.xml:158: error: Error: No resource

    C:\DevelopSoft\workspace\appcompat_v7\res\values-v21\themes_base.xml:158: error: Error: No resource ...