单调栈
RMQ
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<string> #include<queue> #include<map> #include<set> #include<list> #include<ctime> #include<ctype.h> #include<bitset> #include<algorithm> #include<numeric> //accumulate #define endl " " #define fi first #define se second #define FOR(i,s,t) for(int i=(s);i<=(t);++i) #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; const int maxn=300000+5; long long n,a; int d[maxn],c[maxn]; long long pref[maxn]; int stc[maxn],top; int l[maxn],r[maxn]; long long f1[maxn][32]; long long f2[maxn][32]; void rmq_init() { for(int i=1; i<=n; i++) f1[i][0]=f2[i][0]=pref[i]; for(int j=1; (1<<j)<=n; j++) { for(int i=1; i+(1<<j)-1<=n; i++) { f1[i][j]=min(f1[i][j-1],f1[i+(1<<(j-1))][j-1]); f2[i][j]=max(f2[i][j-1],f2[i+(1<<(j-1))][j-1]); } } } long long rmq_query(int l,int r,int ty) { int k=0; while(1<<(k+1)<=r-l+1) k++; if(ty==0) return min(f1[l][k],f1[r-(1<<k)+1][k]); else return max(f2[l][k],f2[r-(1<<k)+1][k]); } int main() { //cin.tie(0); //cout.tie(0); //ios_base::sync_with_stdio(false); //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); cin>>n>>a; for(int i=1; i<=n; i++) { cin>>d[i]>>c[i]; } for(int i=n; i; i--) { d[i]=d[i]-d[i-1]; } top=0; d[n+1]=1e9; for(int i=2; i<=n+1; i++) { while(top&&d[stc[top]]<d[i]) r[stc[top--]]=i-1; stc[++top]=i; } d[1]=1e9; top=0; for(int i=n; i; i--) { while(top&&d[stc[top]]<d[i]) l[stc[top--]]=i+1; stc[++top]=i; } //for(int i=2;i<=n;i++) //cout<<l[i]<<' '<<r[i]<<endl; for(int i=1; i<=n; i++) { pref[i]=pref[i-1]+a-c[i]; } rmq_init(); //cout<<rmq_query(1,n,0)<<endl; long long ans=0; for(int i=2; i<=n; i++) { long long mi; if(i==2) mi=0; else if(l[i]==2) mi=min(0ll,rmq_query(1,i-2,0)); else mi=rmq_query(l[i]-2,i-2,0); long long mx=rmq_query(i,r[i],1); ans=max(ans,mx-mi-1ll*d[i]*d[i]); } for(int i=1; i<=n; i++) { ans=max(ans,1ll*a-c[i]); } cout<<ans; } /* void read() { char c = getchar(); int x = 0; for (; (c < 48 || c>57); c = getchar()); for (; c > 47 && c < 58; c = getchar()) { x = (x << 1) + (x << 3) + c - 48; } return x; } */