题解 P1315 【观光公交】O(nlog(n))
主要是写心得,而且可能是我在luogu的第一篇&最后一篇题解了
加强版可以在LOJ上找到
对于
开题
拿到题后有一个“显而易见”的贪心:一条道路不能加速次数过多而导致车等人,所以加速有一个范围,同时每次加速选择一条人数最多的路(我知道这个东西错的很凶)
所以线段树区间修改,区间查询最大值,同时把过短的边赋为
复杂度美滋滋
然后我就挂了
错因一:
不考虑一条边会对后面连续的一些边产生影响
也就是说区间查询最大值不是最优的决策了
也就是说要开始推公式了
分析后可得
这个式子很重要也很有用!
其中
显然的,如果我们设
所以分析一波
首先我们设
发现这是一个递推式,尝试把它拆开
像这样一直拆下去,就得到了这样一个奇妙的式子
把
合并后完成变形:
这时再回过头来看答案,可以推一波
现在考虑修改答案(我要吐了md)
如果我们修改美滋滋
然后我又挂了
错因二:
没有考虑
此时
艹
完成以上工作LOJ 60分(黑线)
错因三:
没有考虑
可是怎么还是60分?
错因四:
处理
不知道读者发现没有
因为我们默认区在间内左端点最大,而上面的过程显然没有保证。
不慌,分析一下
还是60分,心态有点小崩
错因五
wtcl
不能直接存旧左端点值,因为可能这个值会被修改。所以我们存旧左端点的位置,取
至此收获
附上最终代码
#include<bits/stdc++.h>
using namespace std;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
//#define nc getchar
inline int read(){
int x=0,f=1;
char ch=nc();
for(;!isdigit(ch);ch=nc())if(ch=='-')f=-1;
while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=nc();}
return x*f;
}
typedef long long ll;
const int N=1e5+3;
struct tree{
int l,r;
long long maxn,maxpos,lazy;
inline tree friend operator +(tree l,tree r){
tree ret={0,0,0,0,0};ret.l=l.l;ret.r=r.r;
if(l.maxn>=r.maxn)ret.maxpos=l.maxpos;
else ret.maxpos=r.maxpos;
ret.maxn=max(l.maxn,r.maxn);return ret;
}
}t[N<<2];
int cnt[N],maxn[N],sum[N],pre[N],d[N];
//#define lim(i) (maxn[i+1]-maxn[i])
struct tour{
int tim,l,r;
}p[N*10];
inline bool cmp(tour a,tour b){
if(a.l==b.l)return a.tim<b.tim;
return a.l<b.l;
}
int n,m,k;ll T;
#define lc (p<<1)
#define rc ((p<<1)|1)
inline void pushup(int p){
if(t[lc].maxn>=t[rc].maxn)t[p].maxpos=t[lc].maxpos;
else t[p].maxpos=t[rc].maxpos;
t[p].maxn=max(t[lc].maxn,t[rc].maxn);
}
inline void pushnow(int p,int w){
t[p].maxn+=w;t[p].lazy+=w;
}
inline void pushdown(int p){
if(t[p].lazy!=0){
pushnow(lc,t[p].lazy);pushnow(rc,t[p].lazy);t[p].lazy=0;
}
}
void build(int p,int l,int r){
t[p].l=l;t[p].r=r;t[p].lazy=0;
if(l==r){t[p].maxn=maxn[l]-pre[l],t[p].maxpos=l;return;}
int mid=(l+r)>>1;
build(lc,l,mid);build(rc,mid+1,r);
pushup(p);
}
void lmy(int p,int pl,int pr,int w){
int l=t[p].l,r=t[p].r;
if(pl<=l&&r<=pr){pushnow(p,w);return;}
int mid=(l+r)>>1;pushdown(p);
if(pl<=mid)lmy(lc,pl,pr,w);
if(pr>mid)lmy(rc,pl,pr,w);
pushup(p);
}
tree glf(int p,int pl,int pr){
int l=t[p].l,r=t[p].r;
if(pl<=l&&r<=pr){return t[p];}
pushdown(p);int mid=(l+r)>>1;
if(pl<=mid&&pr>mid)return glf(lc,pl,pr)+glf(rc,pl,pr);
else if(pl<=mid)return glf(lc,pl,pr);
else return glf(rc,pl,pr);
}
struct segment{
int l,r;ll sum, vis;
segment(int _l=0,int _r=0,ll _sum=0,ll _vis=0):l(_l),r(_r),sum(_sum),vis(_vis){}
const bool operator <(const segment &b)const{
return sum<b.sum;
}
};
priority_queue<segment>q;
int main(){
n=read();m=read();k=read();
for(int i=1;i<n;i++)d[i]=read(),pre[i+1]=pre[i]+d[i];
for(int i=1;i<=m;i++){
p[i].tim=read();p[i].l=read();p[i].r=read();T+=p[i].tim;
maxn[p[i].l]=max(maxn[p[i].l],p[i].tim);cnt[p[i].r]++;
}
build(1,1,n);
for(int i=1;i<=n+1;i++)sum[i]=sum[i-1]+cnt[i];
for(int i=2,last=1,lastval=maxn[1]-pre[1];i<=n;i++){
if(maxn[i]-pre[i]>=lastval)q.push(segment(last,i-1,sum[i]-sum[last],last)),last=i,lastval=maxn[i]-pre[i];
if(i==n&&last!=n)q.push(segment(last,i,sum[i]-sum[last],last));
}
while(k&&!q.empty()){
segment now=q.top();q.pop();
if(now.l==now.r){
if(k<=d[now.l])lmy(1,now.l+1,n,k),d[now.l]-=k,k=0;
else k-=d[now.l],lmy(1,now.l+1,n,d[now.l]),d[now.l]=0;
continue;
}
int x=glf(1,now.l+1,now.r).maxn,lmaxn=max(glf(1,now.l,now.l).maxn,glf(1,now.vis,now.vis).maxn);
if(k<=min(d[now.l],lmaxn-x)){lmy(1,now.l+1,n,k);d[now.l]-=k;k=0;}
else{
if(lmaxn-x>=d[now.l]){
lmy(1,now.l+1,n,d[now.l]);k-=d[now.l];d[now.l]=0;
if(now.l+1>now.r)continue;
if(now.l+1<=now.r)q.push(segment(now.l+1,now.r,sum[now.r+1]-sum[now.l+1],now.vis));
}else{
lmy(1,now.l+1,n,lmaxn-x);k-=lmaxn-x;d[now.l]-=lmaxn-x;
if(now.l+1>now.r)continue;
int pos=0;
pos=glf(1,now.l+1,now.r).maxpos;
if(now.l<=pos-1)q.push(segment(now.l,pos-1,sum[pos]-sum[now.l],now.vis));
if(pos<=now.r)q.push(segment(pos,now.r,sum[now.r+1]-sum[pos],pos));
}
}
}
for(int i=1;i<n;i++)pre[i+1]=pre[i]+d[i];
ll ans=-T;
for(int i=2;i<=n;i++)ans+=cnt[i]*(glf(1,1,i-1).maxn+pre[i]);
printf("%lld",ans);
return 0;
}