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 ...
随机推荐
- UVALive - 4094 WonderTeam (贪心)
题目大意: 有n支队伍,每两支队伍打两场比赛(主客场各一次),胜得3分,平得1分,输不得分,比赛结束之后会评选出一个梦之队,梦之队满足以下条件:进球总数最多,胜利场数最多,丢求总数最少,三个都不能并列 ...
- bzoj千题计划289:bzoj 2707: [SDOI2012]走迷宫
http://www.lydsy.com/JudgeOnline/problem.php?id=2707 dp[i] 表示从点i到终点的期望步数 dp[i]= Σ (dp[j]+1)/out[i] j ...
- 51Nod 1684 子集价值 (平方和去括号技巧)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1684 题意: 新建一个位运算,求所有子集通过这个位运算后的答案的平方和是 ...
- HDU 1730 类NIM模型
两者间的间距就是可取石子数,因为对于行内黑白相连的局面该子游戏已经结束了因为此时不管先手再怎么移都是必败,SG=0的终止态 /** @Date : 2017-10-14 21:46:21 * @Fil ...
- JAVA多线程之线程的挂起与恢复(suspend方法与resume方法)
一,介绍 本文讨论JAVA多线程中,使用 thread.suspend()方法暂停线程,使用 thread.resume()恢复暂停的线程 的特点. 先介绍二个关于线程的基本知识: ①线程的执行体是r ...
- artTemplate
1.http://www.cnblogs.com/jiqiyoudu/p/4588042.html
- HDU 4502 吉哥系列故事——临时工计划(一维动态规划)
题意:吉哥的假期是1到n天,然后有m个工作可以让吉哥选择做,每个工作都有一个开始 t_s 和结束的时间 t_e ,都用天来表示,然后每个工作必须从第一天做到最后一天, 从头到尾做完之后就可以得到 ...
- HDU 1308 What Day Is It?(模拟题)
解题报告:输入一个年月日,让你求出那一天是星期几,但是做这题之前必须先了解一点历史.首先在1582年之前,判断是否是闰年的标准是只要能被四整除就是闰年, 然后在1752年9月2号的后的11天被抹去了, ...
- C++--------------------------------指针和数组替换使用原因
马上要考试了,复习数据结构中,对C的指针不太了解,在严蔚敏<数据结构(C语言版)>中,发现p22定义顺序存储结构: typedef srtuct{ ElemType *elem; //存储 ...
- linux笔记_day11_shell编程
1.条件测试类型: 整数测试 字符测试 文件测试 条件测试的表达式: [ expression ] 必须有空格 [[ expression ]] test expression 整数比较 : -eq ...