Codeforces 765F Souvenirs
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.
The first line contains an integer n (2 ≤ n ≤ 105).
The second line contains n space-separated integers a1, ..., an (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).
Print the answer to each query in a separate line.
8
3 1 4 1 5 9 2 6
4
1 8
1 3
4 8
5 7
0
1
1
3
树套树 线段树+set 脑洞题
将询问按右端点从小到大排序。从左向右扫描,每次用当前点x更新从1到x位置的答案,然后回答所有右端点在x的询问。
我们可以用线段树更新每个位置答案的时候,这样复杂度显然比暴力更……劣了(可能需要遍历每个位置)。
不过我们可以给线段树加一些剪枝。当用a[x]更新区间[L,R]的时候,设当前最优答案是nowans 如果[L,R]区间内没有在a[x]-nowans ~ a[x]+nowans范围内的数,就不往深层更新了。
如何查找线段树区间内有哪些数?结点上挂个平(s)衡(e)树(t)即可。
↑为了让这个剪枝正确,更新的时候必须先更新线段树右区间再更新左区间。
/*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<set>
using namespace std;
const int INF=1e9;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct node{
int f;
set<int>st;
}t[mxn<<];
int a[mxn];
set<int>::iterator it,it2;
struct query{
int L,R;
int id;
bool operator < (const query &b)const{
return (R==b.R && L<b.L) || R<b.R;
}
}q[mxn];
int ans[mxn];
int n,Q;
#define ls rt<<1
#define rs rt<<1|1
void Build(int l,int r,int rt){
t[rt].f=INF;
for(int i=l;i<=r;i++)t[rt].st.insert(a[i]);
if(l==r)return;
int mid=(l+r)>>;
Build(l,mid,ls);Build(mid+,r,rs);
it=t[rt].st.begin();
it2=it;it2++;
for(;it2!=t[rt].st.end();it++,it2++){
t[rt].f=min(t[rt].f,abs(*it2-*(it)));
}
return;
}
int query(int L,int R,int l,int r,int rt){
// printf("L:%d R:%d l:%d r:%d rt:%d f:%d\n",L,R,l,r,rt,t[rt].f);
if(L<=l && r<=R)return t[rt].f;
int mid=(l+r)>>;int res=INF;
if(L<=mid)res=min(res,query(L,R,l,mid,ls));
if(R>mid)res=min(res,query(L,R,mid+,r,rs));
return res;
}
int nowans;
void update(int pos,int v,int l,int r,int rt){
if(l==r){
nowans=min(nowans,abs(a[l]-v));
t[rt].f=min(t[rt].f,nowans);
}
it=t[rt].st.lower_bound(v);
if(it!=t[rt].st.begin()){it2=it;it2--;}
if((it==t[rt].st.begin() || *(it2)<=v-nowans)&&(it==t[rt].st.end() || *(it)>=v+nowans)){
nowans=min(nowans,query(l,r,,n,));
return;
}
int mid=(l+r)>>;
if(pos>mid)
update(pos,v,mid+,r,rs);
update(pos,v,l,mid,ls);
t[rt].f=min(t[ls].f,t[rs].f);
return;
}
void solve(){
int i,j;
int hd=;
for(i=;i<=Q;i++){
while(hd<q[i].R){
nowans=INF; update(hd,a[hd+],,n,); hd++;
}
ans[q[i].id]=query(q[i].L,q[i].R,,n,);
}
for(i=;i<=Q;i++)printf("%d\n",ans[i]);
return;
}
int main(){
int i,j;
n=read();
for(i=;i<=n;i++)a[i]=read();
Q=read();
Build(,n,);
for(i=;i<=Q;i++){
q[i].L=read();q[i].R=read();q[i].id=i;
}
sort(q+,q+Q+);
solve();
return ;
}
Codeforces 765F Souvenirs的更多相关文章
- Codeforces 765F Souvenirs - 莫队算法 - 链表 - 线段树
题目传送门 神速的列车 光速的列车 声速的列车 题目大意 给定一个长度为$n$的序列,$m$次询问区间$[l, r]$内相差最小的两个数的差的绝对值. Solution 1 Mo's Algorith ...
- Codeforces 765F Souvenirs 线段树 + 主席树 (看题解)
Souvenirs 我们将询问离线, 我们从左往右加元素, 如果当前的位置为 i ,用一棵线段树保存区间[x, i]的答案, 每次更新完, 遍历R位于 i 的询问更新答案. 我们先考虑最暴力的做法, ...
- Codeforces.765F.Souvenirs(主席树)
题目链接 看题解觉得非常眼熟,总感觉做过非常非常类似的题啊,就是想不起来=v=. 似乎是这道...也好像不是. \(Description\) 给定长为\(n\)的序列\(A_i\).\(m\)次询问 ...
- 【codeforces 765F】 Souvenirs
http://codeforces.com/problemset/problem/765/F (题目链接) 题意 给出$n$个数的序列,$m$次询问,每次查询区间$[l,r]$之间相差最小的两个数的差 ...
- 【codeforces 765F】Souvenirs
Description Artsem is on vacation and wants to buy souvenirs for his two teammates. There are n souv ...
- Souvenirs CodeForces - 765F (好题)
大意: 给定序列$a$, $m$个询问$[l,r]$, 回答$[l,r]$内最接近的两个数的差. 考虑离线, 枚举右端点, 每个点维护左端点的贡献, 对于新添一个点$a_r$, 只考虑左侧点比$a_r ...
- codeforces选做
收录了最近本人完成的一部分codeforces习题,不定期更新 codeforces 1132E Knapsack 注意到如果只使用某一种物品,那么这八种物品可以达到的最小相同重量为\(840\) 故 ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) F. Souvenirs 线段树套set
F. Souvenirs 题目连接: http://codeforces.com/contest/765/problem/F Description Artsem is on vacation and ...
- CodeForces 765 F Souvenirs 线段树
Souvenirs 题意:给你n个数, m次询问, 对于每次一次询问, 求出询问区间内绝对值差值的最小值. 题解:先按查询的右端点从小到大sort一下,然后对于塞入一个数的时候, 就处理出所有左端点到 ...
随机推荐
- 基于 IBM WAS ND v6.1 搭建稳定高效的集群环境
如今的电子商务及电子政务应用系统的发展已经到了一个新的阶段,应用系统的成熟度和可用性都达到了更高的水准.因此庞大的部署规模和海量的用户访问成为目前大型电子商务及电子政务应用系统的显著特征.在这样的情况 ...
- 全栈https
前言:在httpd中使用已经被私有CA签署的证书,对外提供一个https服务 配置httpd服务支持使用https 1:配置httpd支持使用ssl,及使用的证书 yum -y install mod ...
- TFTP服务 简单文件传输协议)是TCP/IP协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,默认端口号为69
(1)yum安装:tftp.tftp-server (2)启动tftp CentOS 6 service xinetd restart chkconfig tftp on CentOS 7 sys ...
- 1029 C语言文法定义
program à external_declaration | program external_declaration <源程序> -> <外部声明> | < ...
- ctf实验平台-成绩单
题目链接:http://120.24.86.145:8002/chengjidan/ 平台地址:http://123.206.31.85/ 第一步:暴库 id=-1' union select 1,2 ...
- C#开发移动应用 - 环境搭建
前言 其实从2013开始就想用Xamarin,奈何 当初收费一座大山压在身上 完全无法见得庐山真面目 后面2015,微软收购Xamarin,没过多久就宣布对个人用户免费..那个兴奋劲就别提了.. 兴奋 ...
- 第138天:Web前端面试题总结(编程)
1.如何让一个盒子水平垂直居中 //已知宽高 <div class="div1"></div> <style> .div1{ width:400 ...
- MySQL复制 -- Binlog (1)
复制之所以工作得益于MySQL把对数据库的变更都记录在 binlog中,然后主库把它读出来,放到从库上去应用.当然binlog 的用途不仅限于此,比如 PITR等 在5.1.4版本以前,binlog格 ...
- P1407 [国家集训队]稳定婚姻
题目描述 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有关. 25岁的姗姗和男友谈恋爱半年就结婚,结婚不到 ...
- 前端跨域问题相关知识详解(原生js和jquery两种方法实现jsonp跨域)
1.同源策略 同源策略(Same origin policy),它是由Netscape提出的一个著名的安全策略.同源策略是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正 ...