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 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, ..., 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).
Output
Print the answer to each query in a separate line.
Sample Input
8
3 1 4 1 5 9 2 6
4
1 8
1 3
4 8
5 7
Sample Output
0
1
1
3
Hint
题意
给你n个数,查询区间(l,r)中,某两个l<=i,j<=r,且i!=r的,min(abs(a[i]-a[j]))这个值。
题解:
只有询问,显然就离线。
最简单的做法是莫队+xxx,很容易做到nsqrt(n)logn的复杂度,当然这会TLE的。
于是我们离线去,用线段树套set去维护这个最小值。
每个线段树的节点,都维护一个set,存放的是这个区间的数。
然后每次离线更新的时候,更新这个节点的最小值就好了。因为插入一个数,只会考虑这个数左边和右边的数是什么就好了。
然后不断更新就行。
这样复杂度是nlognlogn的。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
struct node{
int l,r,id;
bool operator<(const node& a)const{
if(a.l==l&&a.r==r){
return id<a.id;
}
if(a.l==l)
return r<a.r;
return l<a.l;
}
}query[maxn];
struct Node{
set<int> T;
int mi;
}T[maxn];
int n,m,b[maxn],ans[maxn];
void build(int x,int l,int r){
T[x].mi=(1<<30);
for(int i=l;i<=r;i++)
T[x].T.insert(b[i]);
if(l==r)return;
int mid=(l+r)/2;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
}
int Query(int x,int l,int r,int L,int R){
if(l>R||L>r)return (1<<30);
if(L<=l&&r<=R)return T[x].mi;
int mid=(l+r)/2;
return min(Query(x<<1,l,mid,L,R),Query(x<<1|1,mid+1,r,L,R));
}
void upd(int x,int l,int r,int L,int R,int val,int &mi){
if(l>R||L>r)return;
if(l==r){
T[x].mi=min(T[x].mi,abs(val-b[l]));
mi=min(mi,T[x].mi);
return;
}
set<int> &t = T[x].T;
auto p = t.lower_bound(val);
if((p==t.end()||(*p-val)>=mi)&&(p==t.begin()||(val-*(--p))>=mi)){
mi=min(mi,Query(x,l,r,L,R));
return;
}
int mid=(l+r)/2;
upd(x<<1,l,mid,L,R,val,mi);
upd(x<<1|1,mid+1,r,L,R,val,mi);
T[x].mi=min(T[x<<1].mi,T[x<<1|1].mi);
}
void init(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&b[i]);
}
scanf("%d",&m);
for(int i=1;i<=m;i++)
scanf("%d%d",&query[i].l,&query[i].r),query[i].id=i;
}
int main(){
init();
sort(query+1,query+1+m);
build(1,1,n);
for(int i=m,l=n;i;i--){
for(;l>=query[i].l;l--){
int inf=(1<<30);
upd(1,1,n,l+1,n,b[l],inf);
}
ans[query[i].id]=Query(1,1,n,query[i].l,query[i].r);
}
for(int i=1;i<=m;i++)
cout<<ans[i]<<endl;
return 0;
}
Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) F. Souvenirs 线段树套set的更多相关文章
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding 拓扑排序
E. Tree Folding 题目连接: http://codeforces.com/contest/765/problem/E Description Vanya wants to minimiz ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders 数学 构造
D. Artsem and Saunders 题目连接: http://codeforces.com/contest/765/problem/D Description Artsem has a fr ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) C. Table Tennis Game 2 水题
C. Table Tennis Game 2 题目连接: http://codeforces.com/contest/765/problem/C Description Misha and Vanya ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) B. Code obfuscation 水题
B. Code obfuscation 题目连接: http://codeforces.com/contest/765/problem/B Description Kostya likes Codef ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A. Neverending competitions 水题
A. Neverending competitions 题目连接: http://codeforces.com/contest/765/problem/A Description There are ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A B C D 水 模拟 构造
A. Neverending competitions time limit per test 2 seconds memory limit per test 512 megabytes input ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding
地址:http://codeforces.com/contest/765/problem/E 题目: E. Tree Folding time limit per test 2 seconds mem ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders
地址:http://codeforces.com/contest/765/problem/D 题目: D. Artsem and Saunders time limit per test 2 seco ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) C - Table Tennis Game 2
地址:http://codeforces.com/contest/765/problem/C 题目: C. Table Tennis Game 2 time limit per test 2 seco ...
随机推荐
- CF876 D 树状数组
大意就是放n个硬币,每次放一个计算下这种情况下的操作次数,一个操作为从左到右扫描,如果一个硬币右边是空的,就将硬币后移,否则该次操作停止. 显然发现对于一个情况,我们只要考虑最右边的空位的左侧有几个硬 ...
- 读书笔记:《思考的乐趣:Matrix67数学笔记》第4章 统计数据的陷阱
<思考的乐趣:Matrix67数学笔记>第4章讲了几个统计学上的陷阱,由于现在流行的大数据与统计学很有渊源,所以认真读了这一章,在<大数据时代>中指出只考虑相关性就够了,而不考 ...
- [ASP.NET]初试Web API
Web API 1: 需要环境(VS2010/.Net4.0/MVC4 ) http://www.asp.net/web-api/overview/creating-web-apis/creating ...
- Ajax和jsonp区别
大多数情况下,无论是框架还是自己实现都是通过Ajax的方式来向后端请求数据的,而Ajax之间是通过传输json格式的文件来进行数据的传输的,大家对Ajax也很熟悉了,那么为什么我又要使用jsonp呢? ...
- [BZOJ 4350]括号序列再战猪猪侠 题解(区间DP)
[BZOJ 4350]括号序列再战猪猪侠 Description 括号序列与猪猪侠又大战了起来. 众所周知,括号序列是一个只有(和)组成的序列,我们称一个括号 序列S合法,当且仅当: 1.( )是一个 ...
- python的__get__、__set__、__delete__(1)
内容: 描述符引导 摘要 定义和介绍 描述符协议 调用描述符 样例 Properties 函数和 ...
- vue实战之狗血事件:页面loading效果诡异之事
接上回 想加一个切换路由时,跳出一个loading动画 ,路由加载后就消失 先做了一个loading提示的浮动层的组件,全局注册,在几个路由页面都引入 在vuex里面维护一个变量比如isLoading ...
- G. (Zero XOR Subset)-less(线性基)
题目链接:http://codeforces.com/contest/1101/problem/G 题目大意:给你n个数,然后让你把这n个数分成尽可能多的集合,要求,每个集合的值看做这个集合所有元素的 ...
- 八、mini2440裸机程序之UART(1)简单介绍【转】
转自:http://blog.csdn.net/shengnan_wu/article/details/8298869 一.概述 S3C2440通用异步接收和发送(UART)提供了三 ...
- elasticsearch安装ik分词器(极速版)
简介:下面讲有我已经打包并且编辑过的zip包,你可以在下面下载即可. 1.下载zip包.elasticsearch-analysis-ik-1.8.0.jar下面有附件链接[ik-安装包.zip],下 ...