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 ≤ ri, s ≠ 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)$ 。

 #include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
#define lc(x) x<<1
#define rc(x) x<<1|1
using namespace std;
const int N=1e5+;
const int inf=0x3f3f3f3f;
int n,m,tot,sum,a[N],b[N];
int mn[N*],ans[N*];
struct data
{
int l,r,w,id;
bool operator < (const data &t) const{return r<t.r||(r==t.r&&id<t.id);}
}c[N*];
int read()
{
int x=,f=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
bool cmp(int x,int y){return a[x]>a[y]||(a[x]==a[y]&&x>y);}
void modify(int x,int l,int r,int p,int w)
{
mn[x]=min(mn[x],w);
if(l==r)return;
int mid=(l+r)>>;
if(p<=mid)modify(lc(x),l,mid,p,w);
else modify(rc(x),mid+,r,p,w);
}
int find(int x,int l,int r,int p,int w)
{
if(l==r)return mn[x]<=w?l:;
int mid=(l+r)>>;
if(p<=mid&&mn[lc(x)]<=w)
{
int t=find(lc(x),l,mid,p,w);
if(t)return t;
}
return find(rc(x),mid+,r,p,w);
}
void solve()
{
memset(mn,0x3f,sizeof(mn));
sort(b+,b+n+,cmp);
for(int i=;i<=n;i++)
{
int j=find(,,n,b[i],inf-);
while(j)
{
c[++tot]=(data){b[i],j,a[j]-a[b[i]],};
j=find(,,n,b[i],((a[b[i]]+a[j]-)/));
}
modify(,,n,b[i],a[b[i]]);
}
}
int lowbit(int x){return x&(-x);}
void change(int x,int w)
{
x=n-x+;
while(x<=n)mn[x]=min(mn[x],w),x+=lowbit(x);
}
int query(int x)
{
x=n-x+;sum=inf;
while(x)sum=min(sum,mn[x]),x-=lowbit(x);
return sum;
}
int main()
{
n=read();
for(int i=;i<=n;i++)a[i]=read()+,b[i]=i;
solve();
for(int i=;i<=n;i++)a[i]=1e9+-a[i];
solve();
m=read();
for(int i=;i<=m;i++)
c[++tot]=(data){read(),read(),,i};
sort(c+,c+tot+);
memset(mn,0x3f,sizeof(mn));
for(int i=;i<=tot;i++)
if(c[i].id)ans[c[i].id]=query(c[i].l);
else change(c[i].l,c[i].w);
for(int i=;i<=m;i++)printf("%d\n",ans[i]);
return ;
}

【codeforces 765F】Souvenirs的更多相关文章

  1. 【codeforces 765F】 Souvenirs

    http://codeforces.com/problemset/problem/765/F (题目链接) 题意 给出$n$个数的序列,$m$次询问,每次查询区间$[l,r]$之间相差最小的两个数的差 ...

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  3. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  4. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  5. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  6. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  7. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  8. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  9. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

随机推荐

  1. 超哥笔记 --nginx入门(6)

    一 NGINX 1 nignx是什么 nginx是一个开源的支持高性能,高并发的web服务和代理服务软件. nginx比他大哥apache性能改进许多,nginx占用的系统资源更少,支持高并发连接,有 ...

  2. SDOI 2019 R1游记

    $SDOI$ $2019$ $R1$游记 昨天才刚回来,今天就来写游记啦! Day -5: 做了一下去年省选的Day1,感觉很神仙. Day -4: 做了一下去年省选的Day2,感觉还是很神仙. Da ...

  3. Analyzing 'enq: HW - contention' Wait Event (Doc ID 740075.1)

    Analyzing 'enq: HW - contention' Wait Event (Doc ID 740075.1) In this Document   Symptoms   Cause   ...

  4. shell 脚本中执行sql

    #!/bin/bashsql='show databases' result=`mysql -uroot -pengine2user -h127.0.0.1 -e "${sql}" ...

  5. UIImagePickerDelegate - 官方文档说明

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDic ...

  6. 【转】Spark实现行列转换pivot和unpivot

    背景 做过数据清洗ETL工作的都知道,行列转换是一个常见的数据整理需求.在不同的编程语言中有不同的实现方法,比如SQL中使用case+group,或者Power BI的M语言中用拖放组件实现.今天正好 ...

  7. 基于stm32智能车的设计(ucosiii)---北京之行

    实物演示视频:https://v.youku.com/v_show/id_XMzc3MDE3NjMyNA==.html?x&sharefrom=android&sharekey=172 ...

  8. git 原理

    1.git基本原理 2.git提交代码到远程仓库 3.远程仓库同步到本地 git pull #等同于下面命令 git fetch git merge 3.提交代码是冲突解决 一般提交前先get pul ...

  9. Manacher算法详解

    问题 什么是回文串,如果一个字符串正着度读和反着读是一样的,这个字符串就被称为回文串. such as noon level aaa bbb 既然有了回文,那就要有关于回文的问题,于是就有了-- 最长 ...

  10. Flask 构建微电影视频网站(四)

    后台管理 实现后台管理系统使用flask sqlalchemy结合mysql数据库进行增删改查操作.分页的使用.路由装饰器定义.模板中变量调用.登录会话机制.上传文件.flask wtforms表单使 ...