【codeforces 765F】 Souvenirs
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的更多相关文章
- 【codeforces 765F】Souvenirs
Description Artsem is on vacation and wants to buy souvenirs for his two teammates. There are n souv ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
随机推荐
- LeetCode 刷题笔记 155. 最小栈(Min Stack)
tag: 栈(stack) 题目描述 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素 ...
- java IO流 对文件操作的代码集合
Io流 按照分类 有两种分类 流向方向: 有输入流和输出流 按照操作类型有:字节流和字符流 按照流向方向 字节流的一些操作 //读文件 FileInputStream fis = new FileIn ...
- vue 自定义全局按键修饰符
在监听键盘事件时,我们经常需要检查常见的键值.Vue 允许为 v-on 在监听键盘事件时添加按键修饰符: JS部分: Vue.config.keyCodes = { f2:113, } var app ...
- Docker Zero Deployment and Secrets (二)
一. 健康检测: (1)定义检测信息如下(案例,在Dockerfile中定义) FROM alpine:3.6 ... HEALTHCHECK --interval=30s \ --timeo ...
- python基础知识-11-函数装饰器
python其他知识目录 1.装饰器学习前热身准备 1.1装饰器简介 1.2装饰器热身分析 ) def func(): pass v1 = v2 = func #将函数名赋予一个变量,就和变量赋值是同 ...
- nginx正向vs反向代理
1.概述 nginx的正向代理,只能代理http.tcp等,不能代理https请求.有很多人不是很理解具体什么是nginx的正向代理.什么是反向代理.下面结合自己的使用做的一个简介: 1)正向代理: ...
- 互评beta版本 - 探路者【贪吃蛇】
基于NABCD评论,及改进建议 1)N(Need 需求) 开发本软件一方面是为了让80,90后用户重温童年的美好记忆,另一方面也是为了增加对英语学习兴趣较弱.意志力薄弱的中小学生学习英语的兴趣和动力, ...
- Daily Scrum (2015/11/7)
今晚谢金洛同学的UI工作完成,我们进行了UI和后端的拼接,准备开始规范化地进行系统测试. 成员 今日任务及成果 时间 明日任务 符美潇 1.把之前PM分配的编码任务及其说明准备好发给PM 1h 待定 ...
- Daily Scrum (2015/10/21)
今天可以说是项目正式开始的第一天,由于大家缺乏做团队项目的经验,对TFS的使用都还不太熟悉,所以今天大家的主要工作是熟悉TFS的使用和对代码进行初步的理解.我们预计需要2-3天时间来理解透彻源代码.以 ...
- MathExam6378
我的第一个程序 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 15 10 • Estima ...