1 条题解
-
0
#include <bits/stdc++.h> using namespace std; #define ll long long const int N=100005,S=80; ll a[N],b[N],tag[N]; int pos[N],L[N],R[N],bel[N]; vector <int> s[N/S+5]; struct Node{ ll val,k; int id; }; bool cmp(const Node&x,const Node&y){ return x.val>y.val; } long double slope(Node a,Node b){ if (a.k==b.k) return a.val<b.val?-1e18:1e18; return (long double)(a.val-b.val)/(b.k-a.k); } void build(int x){ static Node t[N];int sz=R[x]-L[x]+1; for (int i=L[x];i<=R[x];i++) a[i]+=tag[x];tag[x]=0; for (int i=L[x];i<=R[x];i++) t[i-L[x]+1]=(Node){a[i]*b[i],b[i],i}; sort(t+1,t+sz+1,cmp);s[x].clear();int top=-1; for (int i=1;i<=sz;i++){ while (s[x].size()>=2&&slope(t[s[x][top-1]],t[s[x][top]])<=slope(t[s[x][top-1]],t[i])) --top,s[x].pop_back(); s[x].push_back(t[i].id),++top; }pos[x]=0; } inline ll calc(int x){ return (a[x]+tag[bel[x]])*b[x]; } ll querymax(int x){ while (pos[x]<s[x].size()-1){ if (calc(s[x][pos[x]])>calc(s[x][pos[x]+1])) break; pos[x]++; } return calc(s[x][pos[x]]); } int main(){ //freopen ("a.in","r",stdin); int n,m;scanf ("%d%d",&n,&m); for (int i=1;i<=n;i++) scanf ("%d",&a[i]); for (int i=1;i<=n;i++) scanf ("%d",&b[i]); for (int i=1;i<=n;i++) bel[i]=(i-1)/S+1; for (int i=1;i<=bel[n];i++) L[i]=(i-1)*S+1,R[i]=min(n,i*S); for (int i=1;i<=bel[n];i++) build(i); while (m--){ int opt;scanf ("%d",&opt); if (opt==1){ int l,r,w;scanf ("%d%d%d",&l,&r,&w); if (bel[l]==bel[r]){ for (int i=l;i<=r;i++) a[i]+=w; build(bel[l]); }else{ for (int i=l;i<=R[bel[l]];i++) a[i]+=w; for (int i=L[bel[r]];i<=r;i++) a[i]+=w; for (int i=bel[l]+1;i<=bel[r]-1;i++) tag[i]+=w; build(bel[l]),build(bel[r]); } }else if (opt==2){ int x,y;scanf ("%d%d",&x,&y); swap(b[x],b[y]); build(bel[x]),build(bel[y]); }else{ int l,r;scanf ("%d%d",&l,&r);long long ans=-1ll<<60; if (bel[l]==bel[r]) for (int i=l;i<=r;i++) ans=max(ans,(a[i]+tag[bel[i]])*b[i]); else{ for (int i=l;i<=R[bel[l]];i++) ans=max(ans,(a[i]+tag[bel[i]])*b[i]); for (int i=L[bel[r]];i<=r;i++) ans=max(ans,(a[i]+tag[bel[i]])*b[i]); for (int i=bel[l]+1;i<=bel[r]-1;i++) ans=max(ans,querymax(i)); } printf ("%lld\n",ans); } } return 0; }
- 1
信息
- ID
- 11424
- 时间
- 1500ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 2
- 已通过
- 1
- 上传者