你说得对,但是由乃救爷爷。
联考考到了这个题,要求线性,数据随机,不用离散化。没时间写由乃救爷爷了,于是耻辱下播。
P8592 『JROI-8』颅脑损伤 2.0(加强版)
思路
朴素 DP 是比较简单的。
设 \(f_i\) 表示钦定必须选一个右端点为 \(i\) 的最小代价。
我们将区间挂在右端点上,然后扫。我们设当前右端点为 \(r\),扫到的区间左端点为 \(l\),区间权值为 \(w\).
那么能从一个位置 \(j\) 转移而来的充要条件就是 \((j,l)\) 之间没有任何一个完整的区间。
我们设对于一个位置 \(i\),所有右端点不大于 \(i\) 的区间的左端点的最大值是 \(L_i\)。
那么上面的意思就是 \([L_{l-1},l-1]\) 中必须有一个位置被选,于是我们的转移就是
然后要求 \(r=i\)。
于是就是一个朴素的 RMQ。但是非常遗憾的是写 ST 表会在联考中被空间卡飞,写线段树之类的会被时间卡飞。
我又比较迟钝,单调性什么的显然看不出来,于是只能写线性时空的 ST 表了,也就是最开始说的由乃救爷爷。其大体思路就是将序列按 \(\log n\) 分块,然后暴力预处理块间的 ST 表,块内的前后缀最小值。如果询问在块内,我们就暴力。显然最坏的时间复杂度是单次 \(O(\log n)\) 的,也就是每次都暴力。但是由于联考的时候是随机数据,于是大胜利。
code
但是问题是瓶颈是离散化,就很坏。如果要这么做扫描线可以说是必须的,于是这道题又只能做到 \(O(n\log n)\)。
点击查看代码
#include<bits/stdc++.h>
bool Mbe;
using namespace std;
#define ll unsigned long long
#define ui unsigned int
//namespace FIO{
// template<typename P>
// inline void read(P &x){P res=0,f=1;char ch=getchar();while(ch<'0' || ch>'9'){if(ch=='-') f=-1;ch=getchar();}while(ch>='0' && ch<='9'){res=(res<<3)+(res<<1)+(ch^48);ch=getchar();}x=res*f;}
// template<typename Ty,typename ...Args>
// inline void read(Ty &x,Args &...args) {read(x);read(args...);}
// inline void write(ll x) {if(x<0ll)putchar('-'),x=-x;static int sta[35];int top = 0;do {sta[top++] = x % 10ll, x /= 10ll;} while (x);while (top) putchar(sta[--top] + 48);}
//}
//using FIO::read;using FIO::write;
const int N=1e6+7;
const ll inf=2e18+7;
int n,m,ql[N],qr[N],Lg[N],L[N],stk[N],top;
ll ans,qw[N],f[N],mi[N];
struct node{int l;ll w;};
vector<node>que[N];
void input() {for(int i=1;i<=n;i++){cin>>ql[i]>>qr[i];qw[i]=qr[i]-ql[i];stk[++top]=ql[i],stk[++top]=qr[i];}sort(stk+1,stk+top+1);top=unique(stk+1,stk+top+1)-(stk+1);for(int i=1;i<=n;i++)ql[i]=lower_bound(stk+1,stk+top+1,ql[i])-stk,qr[i]=lower_bound(stk+1,stk+top+1,qr[i])-stk,m=max(m,qr[i]);
}
namespace st{const int B=16;#define bel(x) (((x-1)>>4)+1)#define bl(x) (((x-1)<<4)+1)#define br(x) min(m,(x<<4))ll pre[N],suf[N],g[N/B+5][16];ll get_reg(const int &l,const int &r){const int L=bel(l)+1,R=bel(r)-1;if(L<=R){const int &k=Lg[R-L+1];return min(g[R][k],g[L+(1<<k)-1][k]);}else return inf;}ll get(const int &l,const int &r){if(bel(l)==bel(r)){ll res=inf;for(int i=l;i<=r;i++)res=min(res,f[i]);return res;}return min(suf[l],min(pre[r],get_reg(l,r)));} void insert(const int &i){if(i==bl(bel(i))){pre[i]=f[i];return;}pre[i]=min(pre[i-1],f[i]);if(i==br(bel(i))){suf[i]=f[i];for(int j=i-1;j>=bl(bel(i));j--)suf[j]=min(suf[j+1],f[j]);g[bel(i)][0]=pre[i];for(int k=1;k<=15;k++){if(bel(i)-(1<<k)+1<1)break;g[bel(i)][k]=min(g[bel(i)][k-1],g[bel(i)-(1<<(k-1))][k-1]);}}}
}
using st::get;using st::insert;
bool Med;
signed main(){ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);// freopen("rules.in","r",stdin);// freopen("rules.out","w",stdout);cin>>n;input();for(int i=1;i<=n;i++)que[qr[i]].push_back({ql[i],qw[i]});Lg[0]=-1;for(int r=1;r<=m;r++){Lg[r]=Lg[r/2]+1;ll res=inf;L[r]=L[r-1];for(int j=0;j<(int)que[r].size();j++){int l=que[r][j].l;ll w=que[r][j].w;if(L[l-1]==0)res=min(res,w);else res=min(res,get(L[l-1],l-1)+w);L[r]=max(L[r],l);}f[r]=res;insert(r);}ll ans=get(L[m],m);cout<<ans<<'\n';cerr<<'\n'<<1e3*clock()/CLOCKS_PER_SEC<<"ms\n";cerr<<'\n'<<fabs(&Med-&Mbe)/1048576.0<<"MB\n";return 0;
}