您的位置:首页 > Web前端

codeforces 232D Fence

2015-08-14 10:23 387 查看
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[18][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*80][2],cnt[maxn*80];
void init(){tot=N;}
void insert(int k,int p,int l,int r,int x){
if (l==r){cnt[k]=cnt[p]+1;return;}
int m=(l+r)>>1;
if (x<=m){
cnt[k]=cnt[p]+1,son[k][0]=++tot,son[k][1]=son[p][1];
insert(son[k][0],son[p][0],l,m,x);
}
else{
cnt[k]=cnt[p]+1,son[k][0]=son[p][0],son[k][1]=++tot;
insert(son[k][1],son[p][1],m+1,r,x);
}
}
int query(int k,int l,int r,int x,int y){
if (!k||x>y) return 0;
if (l==x&&r==y) return cnt[k];
int m=(l+r)>>1;
if (y<=m) return query(son[k][0],l,m,x,y);
else if (x<=m) return query(son[k][0],l,m,x,m)+query(son[k][1],m+1,r,m+1,y);
else return query(son[k][1],m+1,r,x,y);
}
int query(int l,int r,int x,int y){return query(r,1,N,x,y)-query(l-1,1,N,x,y);}
}T;
bool ok;
void read(int &x){
for (ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=1;
for (x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
if (ok) x=-x;
}
void get_SA(){
for (int i=1;i<=N;i++) list[i]=(DATA){s[i],i};
sort(list+1,list+N+1,cmp);
for (int i=1;i<=N;i++) SA[i]=list[i].id;
int *x=t1,*y=t2,tot=0;
x[SA[1]]=m=1;
for (int i=2;i<=N;i++){
if (s[SA[i]]!=s[SA[i-1]]) m++;
x[SA[i]]=m;
}
for (int len=1;tot<N;len<<=1,m=tot){
tot=0;
for (int i=N-len+1;i<=N;i++) y[++tot]=i;
for (int i=1;i<=N;i++) if (SA[i]>len) y[++tot]=SA[i]-len;
for (int i=1;i<=m;i++) sum[i]=0;
for (int i=1;i<=N;i++) sum[x[y[i]]]++;
for (int i=1;i<=m;i++) sum[i]+=sum[i-1];
for (int i=N;i>=1;i--) SA[sum[x[y[i]]]--]=y[i];
swap(x,y),x[SA[1]]=tot=1;
for (int i=2;i<=N;i++){
if (y[SA[i]]!=y[SA[i-1]]||y[SA[i]+len]!=y[SA[i-1]+len]) tot++;
x[SA[i]]=tot;
}
}
for (int i=1;i<=N;i++) rank[i]=x[i];
}
void get_height(){
for (int i=1,j=0;i<=N;i++){
if (rank[i]==1) continue;
while (s[i+j]==s[SA[rank[i]-1]+j]) j++;
height[rank[i]]=j;
if (j>0) j--;
}
}
void prepare(){
for (int i=1;i<=N;i++) st[0][i]=height[i];
for (int i=1;i<=17;i++)
for (int j=1;j<=N;j++){
st[i][j]=st[i-1][j];
if (j+(1<<(i-1))<=N) st[i][j]=min(st[i][j],st[i-1][j+(1<<(i-1))]);
}
T.init();
for (int i=1;i<=N;i++) T.insert(i,i-1,1,N,SA[i]);
}
int calc(int l,int r){
if (l>r) swap(l,r);
int t=0; l++;
if (l==r) return height[r];
for (;l+(1<<t)<r;t++);
if (l+(1<<t)>r) t--;
return min(st[t][l],st[t][r-(1<<t)+1]);
}
int find(int s,int x,int op){
int l,r,m;
if (op) l=s,r=N;else l=1,r=s;
while (l!=r){
m=(l+r)>>1;
if (op) m++;
if (calc(m,s)<x){
if (op) r=m-1;
else l=m+1;
}
else{
if (op) l=m;
else r=m;
}
}
return l;
}
void query(int l,int r){
int x=find(rank[l],r-l,0),y=find(rank[l],r-l,1);
printf("%d\n",T.query(x,y,n+1,n+(l-1)-(r-l))+T.query(x,y,n+(r+1),N));
}
int main(){
read(n);
for (int i=1;i<=n;i++) read(h[i]);
for (int i=1;i<n;i++) d[i]=h[i+1]-h[i];
for (int i=1;i<n;i++) s[i]=d[i];
s
=1;
for (int i=1;i<n;i++) s[n+i]=-d[i];
N=(n<<1)-1;
get_SA(),get_height(),prepare();
for (read(q);q;q--){
read(l),read(r);
if (l==r) printf("%d\n",n-1);
else query(l,r);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: