codeforces765F Souvenirs
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
题目链接:http://codeforces.com/contest/765/problem/F
正解:线段树
解题报告:
我看完这道题,只想到了莫队套平衡树套堆的带loglog根号算法(和暴力有啥区别)…
考虑把询问按右端点排序,那么我可以从左往右把端点一个一个往里面加,并一边查询。
我维护一个f[i]f[i]表示以i为左端点的答案,那么我每次加入一个新的点时,相当于是需要更新左边的所有的ff。
我们用线段树支持查询操作,线段树上每个节点维护所控制区间的有序序列,以及这个区间内部的最优ansans。
我们考虑如何优化加入点时,更新操作的复杂度。
因为ff一定是从左往右递增的,而查询操作是按右端点升序的。
我每次更新时先更新右边再更新左边,顺便维护一个当前最优值。
我每次进入一个线段树上的节点更新时,我先在其有序序列查询一下,比新加入的点大的第一个值和小的第一个值,如果新加入点val+val+当前最优值<=<=大的,并且,新加入点val−val−最优值>=>=小的,说明这个区间不可能比当前最优值更优。
那我就没有必要更新下去了,因为我的最优值是在右边取到的,而不难想到,我先做右边再坐左边,那么以后每次查询一定都会覆盖右边取到最优值的那个区间,所以这个节点无论如何不可能是最优值,所以没必要往下更新下去了。
可以想一想这样做的复杂度稳定在loglog级别,总复杂度O(nlog2n)O(nlog2n)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | //It is made by ljh2000 #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> #include <ctime> #include <vector> #include <queue> #include <map> #include <set> #include <string> #include <complex> using namespace std; #define lc root<<1 #define rc root<<1|1 typedef long long LL; typedef complex< double > C; const double pi = acos (-1); const int MAXN = 100011; const int inf = (1<<30)-1; //不要炸int了... int n,m,val[MAXN]; int A[MAXN*3]; struct node{ //线段树每个节点保存的这个区间的有序序列 vector< int >w; int ans; //ans保存的是以区间内每个数为左端点,右端点不断后移的最小值 }a[MAXN*3]; struct ask{ int l,r,id; }q[MAXN*3]; inline int getint(){ int w=0,q=0; char c= getchar (); while ((c< '0' ||c> '9' ) && c!= '-' ) c= getchar (); if (c== '-' ) q=1,c= getchar (); while (c>= '0' &&c<= '9' ) w=w*10+c- '0' ,c= getchar (); return q?-w:w; } inline void upd( int &x, int y){ if (y<x) x=y; } inline bool cmp(ask q,ask qq){ return q.r<qq.r; } inline void build( int root, int l, int r){ if (l==r) { a[root].w.push_back(val[l]); a[root].ans=inf; /*需要设为inf!!!*/ return ; } int mid=(l+r)>>1; build(lc,l,mid); build(rc,mid+1,r); int head=0,s1=a[lc].w.size(),s2=a[rc].w.size(); for ( int i=0;i<s1;i++) { //归并 while (head<s2 && a[rc].w[head]<=a[lc].w[i]) a[root].w.push_back(a[rc].w[head]),head++; a[root].w.push_back(a[lc].w[i]); } while (head<s2) a[root].w.push_back(a[rc].w[head]),head++; a[root].ans=min(a[lc].ans,a[rc].ans); for ( int i=1;i<=r-l;i++) upd(a[root].ans,a[root].w[i]-a[root].w[i-1]); } inline int query( int root, int l, int r, int ql, int qr){ if (ql<=l && r<=qr) return a[root].ans; int mid=(l+r)>>1; if (ql>mid) return query(rc,mid+1,r,ql,qr); else if (qr<=mid) return query(lc,l,mid,ql,qr); else return min(query(lc,l,mid,ql,qr),query(rc,mid+1,r,ql,qr)); } //由于右端点往右移动过程中,以之前的点为左端点的ans不升,而原先保存的仅是控制区间内的最小ans,需要用新加入的节点去更新 inline void modify( int root, int l, int r, int pos, int vv, int &nowans){ if (l==r) { upd(nowans, abs (a[root].w[0]-vv) /*!!!*/ ); upd(a[root].ans,nowans); return ; } vector < int > :: iterator it = lower_bound(a[root].w.begin(),a[root].w.end(),vv); if ((it==a[root].w.end() || *it>=vv+nowans) && (it==a[root].w.begin() || *(it-1)<=vv-nowans)) { upd(nowans,query(root,l,r,l,pos)); return ; } int mid=(l+r)>>1; if (pos>mid) modify(rc,mid+1,r,pos,vv,nowans),modify(lc,l,mid,pos,vv,nowans); //左边也需要更新,不过先更新右边!因为ans单增! else modify(lc,l,mid,pos,vv,nowans); a[root].ans=min(a[root].ans,min(a[lc].ans,a[rc].ans)); //记得update,不要丢失了本来的最优值!有可能未下传或者不更优! } inline void work(){ n=getint(); for ( int i=1;i<=n;i++) val[i]=getint(); build(1,1,n); m=getint(); for ( int i=1;i<=m;i++) q[i].l=getint(),q[i].r=getint(),q[i].id=i; sort(q+1,q+m+1,cmp); int now=1,minl; for ( int o=1;o<=m;o++) { while (now<q[o].r) minl=inf,modify(1,1,n,now,val[now+1] /*!!!*/ ,minl),now++; //注意不要用自己更新了自己! A[q[o].id]=query(1,1,n,q[o].l,q[o].r); } for ( int i=1;i<=m;i++) printf ( "%d\n" ,A[i]); } int main() { work(); return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· Apifox不支持离线,Apipost可以!
· 历时 8 年,我冲上开源榜前 8 了!
· Trae 开发工具与使用技巧
· 通过 API 将Deepseek响应流式内容输出到前端
· 分享一个我遇到过的“量子力学”级别的BUG。