题解 CF1359D 【Yet Another Yet Another Task】
赛时没看到值域,就秒了个扫描线做法。
幸亏这题和 E 都很水,救了我一命。
题意:选一个区间,使得区间和减去区间最大值最大。
考虑扫描线,从小到大枚举
使用线段树,在
在扫描到
使用单调栈处理,一次可以更新一整个区间的最大值。不会的话大概可以去模板题学一下?
再不懂的话看代码吧。
时间复杂度
#include<algorithm>
#include<cctype>
#include<cstdio>
using namespace std;
inline int readint(){
int x=0;
bool f=0;
char c=getchar();
while(!isdigit(c)&&c!='-') c=getchar();
if(c=='-'){
f=1;
c=getchar();
}
while(isdigit(c)){
x=x*10+c-'0';
c=getchar();
}
return f?-x:x;
}
const int maxn=1e5+5;
int n,a[maxn];
struct node{
int l,r;
node* ch[2];
int mx,add;
void pushup(){
mx=max(ch[0]->mx,ch[1]->mx);
}
node(int l,int r):l(l),r(r),mx(0),add(0){
if(l<r){
int mid=l+(r-l)/2;
ch[0]=new node(l,mid);
ch[1]=new node(mid+1,r);
}
}
void pushtag(int k){
add+=k;
mx+=k;
}
void pushdown(){
ch[0]->pushtag(add);
ch[1]->pushtag(add);
add=0;
}
void modify(int ql,int qr,int k){
if(ql<=l&&qr>=r) pushtag(k);
else{
pushdown();
if(ql<=ch[0]->r) ch[0]->modify(ql,qr,k);
if(qr>=ch[1]->l) ch[1]->modify(ql,qr,k);
pushup();
}
}
}*rt;
int s[maxn],top=0;
int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
n=readint();
for(int i=1;i<=n;i++) a[i]=readint();
rt=new node(1,n);
int ans=0;
for(int i=1;i<=n;i++){
rt->modify(1,i,a[i]);
while(top&&a[s[top]]<a[i]){
rt->modify(s[top-1]+1,s[top],a[s[top]]);
top--;
}
rt->modify(s[top]+1,i,-a[i]);
s[++top]=i;
ans=max(ans,rt->mx);
}
printf("%d\n",ans);
return 0;
}