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. web框架开发-Django组件cookie与session

    http协议的每一次都是无保存状态的请求,这会带来很多的不方便,比如,一刷新网页,或者进入该网页的其他页面,无法保存之前的登录状态.为了解决类似这样的问题,引入了会话跟踪 会话跟踪技术 1 什么是会话 ...

  2. Windows服务器环境下jenkins下载和安装

    1.下载jenkins 在windows系统上搭建jenkins持续集成有两种方法:1.war文件,可以用tomcat或者java命令直接运行2.msi安装,作为系统服务后台运行 下载地址 https ...

  3. 实验吧 简单的SQL注入1

    解题链接:http://ctf5.shiyanbar.com/423/web/ 解题思路:一,   输入1,不报错:输入1',报错:输入1'',不报错. 二 ,   输入1 and 1=1,返回1 1 ...

  4. Kafka 详解(一)------简介

    在前面几篇博客我们介绍过一种消息中间件——RabbitMQ,本篇博客我们介绍另外一个消息中间件——Kafka,Kafka是由LinkedIn开发的,使用Scala编写,是一种分布式,基于发布/订阅的消 ...

  5. HDU-problem-1002-人类史上最大最好的希望事件-矩阵快速幂

    Problem Description 作为CNCS的半壁江山,狗哥常常在宇宙中心邵阳眺望黄浦江,夜晚的星空总是迷人,有时候还能见到彗星滑落. 狗哥是幸运的,他在两秒钟内看到了十七颗彗星划过天际,作为 ...

  6. 即将发布的 ASP.NET Core 2.2 会有哪些新玩意儿?

    今年 6 月份的时候时候 .NET 团队就在 GitHub 公布了 ASP.NET Core 2.2 版本的 Roadmap(文末有链接),而前两天 ASP.NET Core 2.2 预览版 2 已经 ...

  7. Git初始化-添加提交以及查看状态

    本地库初始化 使用 git bash 打开 git 命令行窗口.这个命令行窗口兼容 Linux 命令. 命令:mkdir 当前文件下新建文件夹. 命令:git init 初始化空的本地库,.git文件 ...

  8. python 支付宝SDK

    python 支付宝SDK代码如下 from datetime import datetime from Crypto.PublicKey import RSA from Crypto.Signatu ...

  9. [转帖]SAP BASIS日常需要做的工作

    SAP BASIS日常需要做的工作 https://www.cnblogs.com/swordxia/p/4790684.html SAP Basis的一些日常工作包括用户权限管理.集团管理.数据库管 ...

  10. Shell命令-系统信息及显示之uname、hostname

    文件及内容处理 - uname.hostname 1. uname:显示系统信息 uname命令的功能说明 uname 命令用于显示系统信息.uname 可显示电脑以及操作系统的相关信息 uname命 ...