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. TableExistsException: hbase:namespace

    解决:zookeeper还保留着上一次的Hbase设置,所以造成了冲突.删除zookeeper信息,重启之后就没问题了 1.切换到zookeeper的bin目录: 2.执行$sh zkCli.sh 输 ...

  2. 重建二叉树[by Python]

    题目: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2 ...

  3. 如何使用 IDEA 创建项目并且上传到 GitHub

    在 GitHub中 注册创建账号 :https://github.com 下载安装 Git : https://git-scm.com 安装成功后打开 Git Bash,输入下列命令,设置 Git 全 ...

  4. python2.7添加注释后,代码无法保存

    最近需要学习一下Python,然后开始学习中,使用的编辑环境是Python自带的IDLE Python的注释使用的是   # 然后我再代码添加注释 #Python的注释是这个字符 发现始终无法保存代码 ...

  5. HRBUST - 2069-萌萌哒十五酱的衣服~-multiset-lower_bound

    众所周知,十五酱有很多的衣服,而且十五酱东西收拾的非常糟糕. 所以十五酱经常找不到合适的衣服穿,于是她觉得收拾一下屋子,把衣服配成一套一套的~(即一件衬衫一件裤子. 十五酱一共有n件衣服,有衬衫有裤子 ...

  6. auth模块用户认证

    一.auth模块什么 auth模块时django自带的用户认证模块 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功 ...

  7. 微信小程序开发教程 #043 - 在小程序开发中使用 npm

    本文介绍了如何在微信小程序开发中使用 npm 中包的功能,大大提高微信小程序的开发效率,同时也是微信小程序系列教程的视频版更新. 微信小程序在发布之初没有对 npm 的支持功能,这也是目前很多前端开发 ...

  8. 动态链接库-Win32 DLL的说明

    在实际编程时,我们可以把完成某种功能的函数放在一个动态链接库中,然后给其他程序调用. WinAPI中所有的函数都包含在3个最重要的DLL中. Kernel32.dll 它包含那些用于管理内存.进程和线 ...

  9. jquery实现点击控制div的显示和隐藏

    我们使用点击显示.点击隐藏的时候,一般有两种可选方案 .示例 html <div class="index"> <h1> 首页 </h1> &l ...

  10. 第三章· Redis消息队列

    一.生产消费模型 二.Redis发布消息的两种模式