zoukankan      html  css  js  c++  java
  • codeforces 232D Fence

    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.

    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:
      1 #include<cstdio>
      2 #include<iostream>
      3 #include<cmath>
      4 #include<cstring>
      5 #include<algorithm>
      6 #define maxn 200005
      7 using namespace std;
      8 char ch;
      9 int n,q,N,m,l,r,h[maxn],d[maxn],s[maxn],st[18][maxn];
     10 int SA[maxn],rank[maxn],t1[maxn],t2[maxn],height[maxn],sum[maxn];
     11 struct DATA{
     12     int v,id;
     13 }list[maxn];
     14 bool cmp(DATA a,DATA b){
     15     if (a.v!=b.v) return a.v<b.v;
     16     return a.id<b.id;
     17 }
     18 struct seg{
     19     int tot,son[maxn*80][2],cnt[maxn*80];
     20     void init(){tot=N;}
     21     void insert(int k,int p,int l,int r,int x){
     22         if (l==r){cnt[k]=cnt[p]+1;return;}
     23         int m=(l+r)>>1;
     24         if (x<=m){
     25             cnt[k]=cnt[p]+1,son[k][0]=++tot,son[k][1]=son[p][1];
     26             insert(son[k][0],son[p][0],l,m,x);
     27         }
     28         else{
     29             cnt[k]=cnt[p]+1,son[k][0]=son[p][0],son[k][1]=++tot;
     30             insert(son[k][1],son[p][1],m+1,r,x);
     31         }
     32     }
     33     int query(int k,int l,int r,int x,int y){
     34         if (!k||x>y) return 0;
     35         if (l==x&&r==y) return cnt[k];
     36         int m=(l+r)>>1;
     37         if (y<=m) return query(son[k][0],l,m,x,y);
     38         else if (x<=m) return query(son[k][0],l,m,x,m)+query(son[k][1],m+1,r,m+1,y);
     39         else return query(son[k][1],m+1,r,x,y);
     40     }
     41     int query(int l,int r,int x,int y){return query(r,1,N,x,y)-query(l-1,1,N,x,y);}
     42 }T;
     43 bool ok;
     44 void read(int &x){
     45     for (ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=1;
     46     for (x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
     47     if (ok) x=-x;
     48 }
     49 void get_SA(){
     50     for (int i=1;i<=N;i++) list[i]=(DATA){s[i],i};
     51     sort(list+1,list+N+1,cmp);
     52     for (int i=1;i<=N;i++) SA[i]=list[i].id;
     53     int *x=t1,*y=t2,tot=0;
     54     x[SA[1]]=m=1;
     55     for (int i=2;i<=N;i++){
     56         if (s[SA[i]]!=s[SA[i-1]]) m++;
     57         x[SA[i]]=m;
     58     }
     59     for (int len=1;tot<N;len<<=1,m=tot){
     60         tot=0;
     61         for (int i=N-len+1;i<=N;i++) y[++tot]=i;
     62         for (int i=1;i<=N;i++) if (SA[i]>len) y[++tot]=SA[i]-len;
     63         for (int i=1;i<=m;i++) sum[i]=0;
     64         for (int i=1;i<=N;i++) sum[x[y[i]]]++;
     65         for (int i=1;i<=m;i++) sum[i]+=sum[i-1];
     66         for (int i=N;i>=1;i--) SA[sum[x[y[i]]]--]=y[i];
     67         swap(x,y),x[SA[1]]=tot=1;
     68         for (int i=2;i<=N;i++){
     69             if (y[SA[i]]!=y[SA[i-1]]||y[SA[i]+len]!=y[SA[i-1]+len]) tot++;
     70             x[SA[i]]=tot;    
     71         }
     72     }
     73     for (int i=1;i<=N;i++) rank[i]=x[i];
     74 }
     75 void get_height(){
     76     for (int i=1,j=0;i<=N;i++){
     77         if (rank[i]==1) continue;
     78         while (s[i+j]==s[SA[rank[i]-1]+j]) j++;
     79         height[rank[i]]=j;
     80         if (j>0) j--;
     81     }
     82 }
     83 void prepare(){
     84     for (int i=1;i<=N;i++) st[0][i]=height[i];
     85     for (int i=1;i<=17;i++)
     86         for (int j=1;j<=N;j++){
     87             st[i][j]=st[i-1][j];
     88             if (j+(1<<(i-1))<=N) st[i][j]=min(st[i][j],st[i-1][j+(1<<(i-1))]);
     89         }
     90     T.init();
     91     for (int i=1;i<=N;i++) T.insert(i,i-1,1,N,SA[i]);
     92 }
     93 int calc(int l,int r){
     94     if (l>r) swap(l,r);
     95     int t=0; l++;
     96     if (l==r) return height[r];
     97     for (;l+(1<<t)<r;t++); 
     98     if (l+(1<<t)>r) t--;
     99     return min(st[t][l],st[t][r-(1<<t)+1]);
    100 }
    101 int find(int s,int x,int op){
    102     int l,r,m;
    103     if (op) l=s,r=N;else l=1,r=s;
    104     while (l!=r){
    105         m=(l+r)>>1;
    106         if (op) m++;
    107         if (calc(m,s)<x){
    108             if (op) r=m-1;
    109             else l=m+1;    
    110         }
    111         else{
    112             if (op) l=m;
    113             else r=m;
    114         }
    115     }
    116     return l;
    117 }
    118 void query(int l,int r){
    119     int x=find(rank[l],r-l,0),y=find(rank[l],r-l,1);
    120     printf("%d
    ",T.query(x,y,n+1,n+(l-1)-(r-l))+T.query(x,y,n+(r+1),N));
    121 }
    122 int main(){
    123     read(n);
    124     for (int i=1;i<=n;i++) read(h[i]);
    125     for (int i=1;i<n;i++) d[i]=h[i+1]-h[i];
    126     for (int i=1;i<n;i++) s[i]=d[i];
    127     s[n]=1;
    128     for (int i=1;i<n;i++) s[n+i]=-d[i];
    129     N=(n<<1)-1;
    130     get_SA(),get_height(),prepare();
    131     for (read(q);q;q--){
    132         read(l),read(r);
    133         if (l==r) printf("%d
    ",n-1);
    134         else query(l,r);
    135     }
    136     return 0;
    137 }
  • 相关阅读:
    PostgreSQL与MySQL常用命令比较
    honghe
    Linux中find命令path prune用法详解
    推荐几种在网页中播放FLV文件的代码
    Html文件转换为Aspx文件后发现样式丢失或失效
    友情链接对PR值的贡献表
    decimal(numeric )、float 和 real 数据类型的区别[转]
    HttpHandle
    一个Ext2+SWFUpload做的图片上传对话框收藏
    IE浏览器无法查看源文件原因及应用技巧
  • 原文地址:https://www.cnblogs.com/chenyushuo/p/4729221.html
Copyright © 2011-2022 走看看