1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| #include<bits/stdc++.h> using namespace std; #define int long long #define rep(i,j,k) for(int i=(j);i<=(k);i++) #define per(i,j,k) for(int i=(j);i>=(k);i--) #define pb push_back #define mp make_pair #define fi first #define se second typedef vector<int> vi; typedef pair<int,int> pi;
const int N=2e5+5,inf=1e18; #define mid (l+(r-l)/2) struct line{ int k,b; int val(int x){return k*x+b;} }; struct sgt{ line L[N]; int s[N][2],rt,tot; void ins(int &d,int l,int r,line nw){ if(!d){ d=++tot; L[d]=nw; return; } int val0=L[d].val(mid),val1=nw.val(mid); if(val0>val1){ swap(nw,L[d]); } if(l!=r){ if(nw.k<L[d].k){ ins(s[d][1],mid+1,r,nw); } else{ ins(s[d][0],l,mid,nw); } } } void query(int d,int l,int r,int nd,int &ans){ if(!d){ return; } ans=min(ans,L[d].val(nd)); nd<=mid?query(s[d][0],l,mid,nd,ans):query(s[d][1],mid+1,r,nd,ans); } }le,ri; #undef mid
int n,m; int a[N]; void Input(){ cin>>n>>m; rep(i,1,n){ cin>>a[i]; } } vector<pi> qry[N]; void solve(){ vi pre(n+1,0); rep(i,1,n){ pre[i]=pre[i-1]+a[i]; } int totqry=0; int sum=0,mndlt=0,mxdlt=0; vi ans(m+1,0); rep(i,1,m){ int op;cin>>op; if(op==1){ int x;cin>>x; totqry++; ans[totqry]=pre[n]+n*sum; qry[x].pb(mp(sum,totqry)); } else{ int x;cin>>x; sum+=x; mndlt=min(mndlt,sum); mxdlt=max(mxdlt,sum); } } le.ins(le.rt,mndlt,mxdlt,(line){0,0}); rep(i,1,n){ for(pi x:qry[i]){ int Get=inf; le.query(le.rt,mndlt,mxdlt,x.fi,Get); ans[x.se]-=Get; } le.ins(le.rt,mndlt,mxdlt,{i,pre[i]}); } ri.ins(ri.rt,mndlt,mxdlt,{0,0}); per(i,n,1){ for(pi x:qry[i]){ int Get=inf; ri.query(ri.rt,mndlt,mxdlt,x.fi,Get); ans[x.se]-=Get; } ri.ins(ri.rt,mndlt,mxdlt,{n-i+1,pre[n]-pre[i-1]}); } rep(i,1,totqry){ cout<<ans[i]<<'\n'; } } signed main(){ freopen("subarray.in","r",stdin); freopen("subarray.out","w",stdout); ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); Input(); solve(); return 0; }
|