http://codeforces.com/problemset/problem/765/F (题目链接)

题意

  给出$n$个数的序列,$m$次询问,每次查询区间$[l,r]$之间相差最小的两个数的差。

Solution

  迷,右转题解→_→:jump

  奇怪的线段树

细节

  不造

代码

// codeforces 765F
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<ctime>
#define LL long long
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout)
using namespace std; const int maxn=300010;
int n,Q,ans,a[maxn],res[maxn];
struct data {int l,r,id;}q[maxn];
struct node {int l,r,s;vector<int> v;}tr[maxn<<2]; bool cmp(data a,data b) {return a.r<b.r;}
void build(int k,int s,int t) {
tr[k].l=s;tr[k].r=t;tr[k].s=inf;
if (s==t) {tr[k].v.push_back(a[s]);return;}
int mid=(s+t)>>1;
build(k<<1,s,mid);build(k<<1|1,mid+1,t);
int i=0,j=0,ll=tr[k<<1].v.size(),rr=tr[k<<1|1].v.size();
while (i<ll || j<rr) {
if ((i<ll && tr[k<<1].v[i]<tr[k<<1|1].v[j]) || j==rr)
tr[k].v.push_back(tr[k<<1].v[i++]);
else tr[k].v.push_back(tr[k<<1|1].v[j++]);
}
for (int i=1;i<=t-s;i++) tr[k].s=min(tr[k].s,tr[k].v[i]-tr[k].v[i-1]);
}
int query(int k,int s,int t) {
int l=tr[k].l,r=tr[k].r,mid=(l+r)>>1;
if (l==s && r==t) return tr[k].s;
if (t<=mid) return query(k<<1,s,t);
else if (s>mid) return query(k<<1|1,s,t);
else return min(query(k<<1,s,mid),query(k<<1|1,mid+1,t));
}
void modify(int k,int p,int val) {
int l=tr[k].l,r=tr[k].r,mid=(l+r)>>1;
if (l==r) {tr[k].s=min(tr[k].s,abs(val-tr[k].v[0]));ans=min(ans,tr[k].s);return;}
vector<int>::iterator t=lower_bound(tr[k].v.begin(),tr[k].v.end(),val);
if (t==tr[k].v.end() || *t-val>=ans)
if (t==tr[k].v.begin() || val-*--t>=ans) {ans=min(ans,tr[k].s);return;}
if (p<=mid) modify(k<<1,p,val);
else modify(k<<1|1,p,val),modify(k<<1,p,val);
tr[k].s=min(tr[k].s,min(tr[k<<1].s,tr[k<<1|1].s));
}
int main() {
scanf("%d",&n);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
scanf("%d",&Q);
for (int i=1;i<=Q;i++) scanf("%d%d",&q[i].l,&q[i].r),q[i].id=i;
sort(q+1,q+1+Q,cmp);
build(1,1,n);
int p=1;
for (int i=1;i<=Q;i++) {
while (p<q[i].r) ans=inf,modify(1,p,a[p+1]),p++;
res[q[i].id]=query(1,q[i].l,q[i].r);
}
for (int i=1;i<=Q;i++) printf("%d\n",res[i]);
return 0;
}

【codeforces 765F】 Souvenirs的更多相关文章

  1. 【codeforces 765F】Souvenirs

    Description Artsem is on vacation and wants to buy souvenirs for his two teammates. There are n souv ...

  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. Python学习之路目录(收藏整理)

    目录 Python之路[第一篇]:Python简介和入门 Python之路[第二篇]:Python基础(一) Python之路[第三篇]:Python基础(二) Python之路[第四篇]:模块    ...

  2. time命令详情

    基础命令学习目录首页 原文链接:https://blog.csdn.net/adaptiver/article/details/6596143?utm_source=blogxgwz3 linux下t ...

  3. C++ 类 构造函数 constructor

    构造函数 当定义了一个整型变量: int a; 这会申请了一块内存空间来存储a,但是这块内存中原本有数据的,可能是任何值,这不是你所希望的,若你就希望a表示1,所以要把a的值赋值为1. ; 例: #i ...

  4. bootstrap中的data-toggle模态框相关

    一,点击即打开1,点击按钮 <a href="javascript:void(0)" class="btn btn-primary" data-toggl ...

  5. 2-Fifth Scrum Meeting20151205

    任务安排 闫昊: 今日完成:设计本地数据库. 明日任务:请假.(最近代码写得多……很累……) 唐彬: 今日完成:ios客户端代码的了解. 明日任务:ios客户端代码的深度学习. 史烨轩: 今日完成: ...

  6. so easy, too happy

    一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 • Estimate • 估计这个任务需要多 ...

  7. 20135234mqy 实验三:敏捷开发与XP实践

    实     验    报     告 课程:Java 班级: 1352    姓名:mqy    学号:20135234 成绩:              指导教师:娄嘉鹏    实验日期:2015. ...

  8. 20135234mqy

    北京电子科技学院(BESTI) 实     验    报     告 课程:Java实验    班级:1352    姓名: mqy  学号:20135234 成绩:             指导教师 ...

  9. 软工实践-Beta 冲刺 (6/7)

    队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 1.界面的修改与完善 展示GitHub当日代码/文档签入记 ...

  10. POJ 2096 Collecting Bugs 期望dp

    题目链接: http://poj.org/problem?id=2096 Collecting Bugs Time Limit: 10000MSMemory Limit: 64000K 问题描述 Iv ...