zoukankan      html  css  js  c++  java
  • 【codeforces 765F】Souvenirs

    Description

    Artsem is on vacation and wants to buy souvenirs for his two teammates. There are n souvenir shops along the street. In i-th shop Artsem can buy one souvenir for ai dollars, and he cannot buy more than one souvenir in one shop. He doesn't want to introduce envy in his team, so he wants to buy two souvenirs with least possible difference in price.

    Artsem has visited the shopping street m times. For some strange reason on the i-th day only shops with numbers from li to ri were operating (weird? yes it is, but have you ever tried to come up with a reasonable legend for a range query problem?). For each visit, Artsem wants to know the minimum possible difference in prices of two different souvenirs he can buy in the opened shops.

    In other words, for each Artsem's visit you should find the minimum possible value of |as - at| where li ≤ s, t ≤ ris ≠ t.

    Input

    The first line contains an integer n (2 ≤ n ≤ 105).

    The second line contains n space-separated integers a1, ..., a(0 ≤ ai ≤ 109).

    The third line contains the number of queries m (1 ≤ m ≤ 3·105).

    Next m lines describe the queries. i-th of these lines contains two space-separated integers li and ri denoting the range of shops working on i-th day (1 ≤ li < ri ≤ n).

    Output

    Print the answer to each query in a separate line.

    题意:给定 $n$ 个数, $m$ 次询问,每次询问区间中 $|a_{i}-a_{j}|$ 的最小值。

    分析:

    对于每一个 $i$ ,考虑所有满足 $j>i$ 且 $a_{j}leq a_{i}$ 的可能可以成为答案的 $j$($a_{j}geq a_{i}$ 的情况可以用同样的方式处理)。假设当前已经找到了一对 $(i,j)$,则下一个合法的位置 $k$ 需要满足 $a_{k}<a_{j}$ 且 $a_{k}-a_{i}<a_{j}-a_{k}$,即每次需要查询区间 [j+1,n] 内的第一个满足 $a_{i}leq a_{k}<frac{a_{i}+a_{j}}{2}$ 的 $k$,可以将数字从大到小加入线段树后直接查询。由于 $a_{j}-a_{i}$ 每次至少减少一半,所以最多有 $O(nloga)$$(i,j)$ 

    得到所有合法的 $(i,j)$ 后,可以按 $j$ 排序后插入树状数组,每次查询左端点。时间复杂度 $O((nloga+m)logn)$ 

     1 #include<cstdio>
     2 #include<algorithm> 
     3 #include<cstring>
     4 #define LL long long
     5 #define lc(x) x<<1
     6 #define rc(x) x<<1|1
     7 using namespace std;
     8 const int N=1e5+5;
     9 const int inf=0x3f3f3f3f;
    10 int n,m,tot,sum,a[N],b[N];
    11 int mn[N*4],ans[N*10];
    12 struct data
    13 {
    14     int l,r,w,id;
    15     bool operator < (const data &t) const{return r<t.r||(r==t.r&&id<t.id);}
    16 }c[N*70];
    17 int read()
    18 {
    19     int x=0,f=1;char c=getchar();
    20     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    21     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    22     return x*f;
    23 }
    24 bool cmp(int x,int y){return a[x]>a[y]||(a[x]==a[y]&&x>y);}
    25 void modify(int x,int l,int r,int p,int w)
    26 {
    27     mn[x]=min(mn[x],w);
    28     if(l==r)return;
    29     int mid=(l+r)>>1;
    30     if(p<=mid)modify(lc(x),l,mid,p,w);
    31     else modify(rc(x),mid+1,r,p,w);
    32 }
    33 int find(int x,int l,int r,int p,int w)
    34 {
    35     if(l==r)return mn[x]<=w?l:0;
    36     int mid=(l+r)>>1;
    37     if(p<=mid&&mn[lc(x)]<=w)
    38     {
    39         int t=find(lc(x),l,mid,p,w);
    40         if(t)return t;
    41     }
    42     return find(rc(x),mid+1,r,p,w);
    43 }
    44 void solve()
    45 {
    46     memset(mn,0x3f,sizeof(mn));
    47     sort(b+1,b+n+1,cmp);
    48     for(int i=1;i<=n;i++)
    49     {
    50         int j=find(1,1,n,b[i],inf-1);
    51         while(j)
    52         {
    53             c[++tot]=(data){b[i],j,a[j]-a[b[i]],0};
    54             j=find(1,1,n,b[i],((a[b[i]]+a[j]-1)/2));
    55         }
    56         modify(1,1,n,b[i],a[b[i]]);
    57     }
    58 }
    59 int lowbit(int x){return x&(-x);}
    60 void change(int x,int w)
    61 {
    62     x=n-x+1;
    63     while(x<=n)mn[x]=min(mn[x],w),x+=lowbit(x);
    64 }
    65 int query(int x)
    66 {
    67     x=n-x+1;sum=inf;
    68     while(x)sum=min(sum,mn[x]),x-=lowbit(x);
    69     return sum;
    70 }
    71 int main()
    72 {
    73     n=read();
    74     for(int i=1;i<=n;i++)a[i]=read()+1,b[i]=i;
    75     solve();
    76     for(int i=1;i<=n;i++)a[i]=1e9+2-a[i];
    77     solve();
    78     m=read();
    79     for(int i=1;i<=m;i++)
    80         c[++tot]=(data){read(),read(),0,i};
    81     sort(c+1,c+tot+1);
    82     memset(mn,0x3f,sizeof(mn));
    83     for(int i=1;i<=tot;i++)
    84         if(c[i].id)ans[c[i].id]=query(c[i].l);
    85         else change(c[i].l,c[i].w);
    86     for(int i=1;i<=m;i++)printf("%d
    ",ans[i]);
    87     return 0;
    88 }
    View Code
  • 相关阅读:
    DataTable常用操作总结[转帖]
    Jquery实现淡入淡出效果
    在自定义Server Control中捆绑JS文件 [转帖]
    ASP.NET利用String.Join以分隔符號來串連集合資料 [转帖]
    fork()的一些测试
    大端小端表示法 && GAS对过程的实现
    GAS中流程控制的实现,for, while, if, switch
    一个看起来奇怪的C++程序 && c++操作符重载
    修改函数的返回地址
    stl algorithm sort ,unique
  • 原文地址:https://www.cnblogs.com/zsnuo/p/8897979.html
Copyright © 2011-2022 走看看