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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
#include<bits/stdc++.h> #define re register typedef long long ll; template<class T> inline void read(T &x) { x=0; char ch=getchar(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); if(t) x=-x; } template<class T,class ...T1> inline void read(T &x,T1 &...x1){ read(x),read(x1...); } template<class T> inline void write(T x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } template<> inline void write(bool x){ putchar(x?'1':'0'); } template<> inline void write(char c){ putchar(c); } template<> inline void write(char *s){ while(*s!='\0') putchar(*s++); } template<> inline void write(const char *s){ while(*s!='\0') putchar(*s++); } template<class T,class ...T1> inline void write(T x,T1 ...x1){ write(x),write(x1...); } template<class T> inline bool checkMax(T &x,T y){ return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T y){ return x>y?x=y,1:0; } const int MAXN=2e5+10,MAXB=5e2+15; const int Inf=0x3f3f3f3f,uni=510; const ll INF=0x3f3f3f3f3f3f3f3f; int N,Q,a[MAXN]; struct Point { ll x,y; Point(ll _x=0,ll _y=0):x(_x),y(_y){} inline friend const Point operator+(const Point &a,const Point &b) { return Point(a.x+b.x,a.y+b.y); } inline friend const Point operator-(const Point &a,const Point &b) { return Point(a.x-b.x,a.y-b.y); } inline friend const ll operator*(const Point &a,const Point &b) { return a.x*b.y-a.y*b.x; } inline friend const bool operator<=(const Point &a,const Point &b) { return a*b>=0; } }Pt[MAXN]; typedef Point Vector; int pTop; struct Convex { Vector* vec; int mxcnt,siz; ll tag; Convex(){ mxcnt=0; } inline Point operator[](const int &x) const { return Point(vec[x].x,vec[x].y+tag*vec[x].x); } inline void insert(Point p){ checkMax(vec[p.x].y,p.y); } inline void push_back(Point p){ vec[siz++]=p; } inline void clear(int len) { vec[0]=Vector(0,0); for(re int i=1;i<=len;++i) vec[i]=Vector(i,-INF); siz=len+1;tag=0; } inline void convex() { if(siz<=2) return ; int top=1; for(re int i=2;i<siz;++i) { if(vec[i].y==-INF) continue; while(top>=1&&vec[top]-vec[top-1]<=vec[i]-vec[top-1]) --top; vec[++top]=vec[i]; } siz=top+1;tag=0; } inline ll maxv(ll adv) { while(mxcnt<siz-1&&(vec[mxcnt+1].x-vec[mxcnt].x)*(tag+adv)+vec[mxcnt+1].y-vec[mxcnt].y>0) ++mxcnt; return vec[mxcnt].x*(tag+adv)+vec[mxcnt].y; } inline ll maxv_bs(ll adv) { int l=-1,r=siz-1; while(l+1<r) { int mid=(l+r)>>1; if((vec[mid+1].x-vec[mid].x)*(tag+adv)+vec[mid+1].y-vec[mid].y>0) l=mid; else r=mid; } mxcnt=r; return vec[r].x*(tag+adv)+vec[r].y; } }; struct Node { ll lx,rx,val,sum; Node(ll _l=0,ll _r=0,ll _v=0,ll _s=0):lx(_l),rx(_r),val(_v),sum(_s){} inline friend const Node operator+(const Node &a,const Node &b) { return Node( std::max(a.lx,a.sum+b.lx), std::max(b.rx,b.sum+a.rx), std::max(std::max(a.val,b.val),a.rx+b.lx), a.sum+b.sum); } }; struct Segment_tree { #define ls (p<<1) #define rs (p<<1|1) struct Segment { Convex lx,rx,ans; ll tag,sum; }Tr[MAXB<<2]; inline void merge(Convex& c,Convex& a,Convex& b,Vector adv) { int siza=a.siz,sizb=b.siz; for(re int i=0;i<siza;++i) c.push_back(a[i]); for(re int i=0;i<sizb;++i) c.push_back(adv+b[i]); c.convex(); } inline void mincowsky(Convex& c,Convex& a,Convex& b) { int cur1=0,cur2=0,siza=a.siz,sizb=b.siz; c.insert(a[0]+b[0]); while(cur1+1<siza&&cur2+1<sizb) { if(a[cur1+1]-a[cur1]<=b[cur2+1]-b[cur2]) c.insert(a[cur1]+b[++cur2]); else c.insert(a[++cur1]+b[cur2]); } while(cur1+1<siza) c.insert(a[++cur1]+b[cur2]); while(cur2+1<sizb) c.insert(a[cur1]+b[++cur2]); } inline void update(int p,int l,int r) { int mid=(l+r)>>1; merge(Tr[p].lx,Tr[ls].lx,Tr[rs].lx,Point(mid-l+1,Tr[ls].sum)); merge(Tr[p].rx,Tr[rs].rx,Tr[ls].rx,Point(r-mid,Tr[rs].sum)); Tr[p].ans.clear(r-l+1); int siz=Tr[ls].ans.siz; for(re int i=0;i<siz;++i) Tr[p].ans.insert(Tr[ls].ans[i]); siz=Tr[rs].ans.siz; for(re int i=0;i<siz;++i) Tr[p].ans.insert(Tr[rs].ans[i]); mincowsky(Tr[p].ans,Tr[ls].rx,Tr[rs].lx); Tr[p].ans.convex(); Tr[p].lx.mxcnt=Tr[p].rx.mxcnt=Tr[p].ans.mxcnt=0; Tr[p].sum=Tr[ls].sum+Tr[rs].sum; } inline void locate(int p,int l,int r) { Tr[p].lx.vec=Pt+pTop;pTop+=r-l+3; Tr[p].rx.vec=Pt+pTop;pTop+=r-l+3; Tr[p].ans.vec=Pt+pTop;pTop+=r-l+3; if(l==r) return ; int mid=(l+r)>>1; locate(ls,l,mid),locate(rs,mid+1,r); } void build(int p,int l,int r,int bel) { if(l==r) { Tr[p].lx.push_back(Point(0,0)),Tr[p].lx.push_back(Point(1,a[l+(bel-1)*uni])); Tr[p].rx.push_back(Point(0,0)),Tr[p].rx.push_back(Point(1,a[l+(bel-1)*uni])); Tr[p].ans.push_back(Point(0,0)),Tr[p].ans.push_back(Point(1,a[l+(bel-1)*uni])); Tr[p].sum=a[l+(bel-1)*uni]; return ; } int mid=(l+r)>>1; build(ls,l,mid,bel),build(rs,mid+1,r,bel); update(p,l,r); } inline void pushdown(int p,int l,int r) { if(l==r||!Tr[p].tag) return ; int mid=(l+r)>>1; Tr[ls].tag+=Tr[p].tag,Tr[rs].tag+=Tr[p].tag; Tr[ls].lx.tag+=Tr[p].tag,Tr[ls].rx.tag+=Tr[p].tag,Tr[ls].ans.tag+=Tr[p].tag; Tr[ls].sum+=(mid-l+1)*Tr[p].tag,Tr[rs].sum+=(r-mid)*Tr[p].tag; Tr[rs].lx.tag+=Tr[p].tag,Tr[rs].rx.tag+=Tr[p].tag,Tr[rs].ans.tag+=Tr[p].tag; Tr[p].tag=0; } inline void add_part(int p,int l,int r,int ql,int qr,ll v) { if(l==ql&&qr==r) { Tr[p].tag+=v,Tr[p].sum+=(r-l+1)*v; Tr[p].lx.tag+=v,Tr[p].rx.tag+=v,Tr[p].ans.tag+=v; return ; } pushdown(p,l,r); int mid=(l+r)>>1; if(mid>=qr) add_part(ls,l,mid,ql,qr,v); else if(mid+1<=ql) add_part(rs,mid+1,r,ql,qr,v); else { add_part(ls,l,mid,ql,mid,v); add_part(rs,mid+1,r,mid+1,qr,v); } Tr[p].lx.siz=Tr[p].rx.siz=Tr[p].ans.siz=0; update(p,l,r); } inline Node query_all(int l,int r,ll adv) { return Node(Tr[1].lx.maxv(adv),Tr[1].rx.maxv(adv),Tr[1].ans.maxv(adv),Tr[1].sum+(r-l+1)*adv); } inline Node query_seg(int p,int l,int r,int ql,int qr,ll adv) { if(l==ql&&qr==r&&p==1) return query_all(ql,qr,adv); if(l==ql&&qr==r) return Node( Tr[p].lx.maxv_bs(adv),Tr[p].rx.maxv_bs(adv), Tr[p].ans.maxv_bs(adv),Tr[p].sum+(r-l+1)*adv); pushdown(p,l,r); int mid=(l+r)>>1; if(mid>=qr) return query_seg(ls,l,mid,ql,qr,adv); else if(mid+1<=ql) return query_seg(rs,mid+1,r,ql,qr,adv); else return query_seg(ls,l,mid,ql,mid,adv)+query_seg(rs,mid+1,r,mid+1,qr,adv); } inline void clear(int p,int l,int r) { Tr[p].lx.siz=Tr[p].rx.siz=Tr[p].ans.siz=0; Tr[p].lx.mxcnt=Tr[p].rx.mxcnt=Tr[p].ans.mxcnt=0; Tr[p].lx.tag=Tr[p].rx.tag=Tr[p].ans.tag=0; Tr[p].tag=0; if(l==r) return ; int mid=(l+r)>>1; clear(ls,l,mid),clear(rs,mid+1,r); } #undef ls #undef rs }; struct Operation { int opt,l,r,id; ll v; Operation(int _o=0,int _l=0,int _r=0,int _i=0,ll _v=0):opt(_o),l(_l),r(_r),id(_i),v(_v){} inline bool operator<(const Operation &x) const { return v<x.v; } }; int Lst[MAXB],Rst[MAXB],Bel[MAXN],Tot; Node ans[MAXN]; struct Sqrt_Block { Segment_tree Tr; int bel,n,m,cnt[256]; Operation seq[MAXN],tmp[MAXN]; ll mrk,val[MAXN],val_tmp[MAXN]; inline void add_all(ll v){ mrk+=v; } inline void add_part(int l,int r,ll v) { if(!v) return ; if(l==1&&r==n) return add_all(v); seq[m++]=Operation(v,l,r,0,mrk); } inline void query(int l,int r,int id){ seq[m++]=Operation(0,l,r,id,mrk); } inline void carsort(int l,int r) { if(r-l<=1500) return std::sort(seq+l,seq+r),void(); for(re int i=l;i<r;++i) val[i-l]=val_tmp[i-l]=seq[i].v|((1ll*i)<<35); for(re int i=0;i<32;i+=8) { std::memset(cnt,0,sizeof(cnt)); for(re int j=0;j<r-l;++j) ++cnt[(val[j]>>i)&255]; for(re int j=1;j<256;++j) cnt[j]+=cnt[j-1]; for(re int j=r-l-1;j>=0;--j) { val_tmp[cnt[(val[j]>>i)&255]-1]=val[j]; --cnt[(val[j]>>i)&255]; } std::memcpy(val,val_tmp,(r-l+5)*sizeof(ll)); } for(re int i=l;i<r;++i) tmp[i-l]=seq[i]; for(re int i=l;i<r;++i) seq[i]=tmp[(val[i-l]>>35)-l]; } inline void prefix() { ll minmrk=0; for(re int i=0;i<m;++i) checkMin(minmrk,seq[i].v); for(re int i=0;i<m;++i) seq[i].v-=minmrk; for(re int i=Lst[bel];i<=Rst[bel];++i) a[i]+=minmrk; Tr.build(1,1,n,bel); int lst=0; for(re int i=0;i<m;++i) if(seq[i].opt!=0) { if(i!=lst) carsort(lst,i); lst=i+1; } if(lst!=m) carsort(lst,m); } inline void solve() { for(re int i=0;i<m;++i) if(seq[i].opt==0) { if(i&&seq[i-1].opt) { Tr.Tr[1].lx.maxv_bs(seq[i].v), Tr.Tr[1].rx.maxv_bs(seq[i].v), Tr.Tr[1].ans.maxv_bs(seq[i].v); } Node tmp=Tr.query_seg(1,1,n,seq[i].l,seq[i].r,seq[i].v); ans[seq[i].id]=ans[seq[i].id]+tmp; } else Tr.add_part(1,1,n,seq[i].l,seq[i].r,seq[i].opt); } inline void clear(){ m=0;Tr.clear(1,1,n);mrk=0; } }; struct query_seg { int opt,l,r; ll v; query_seg(int _o=0,int _l=0,int _r=0,ll _v=0):opt(_o),l(_l),r(_r),v(_v){} }; Sqrt_Block Blc; query_seg Qr[MAXN]; signed main() { read(N,Q); for(re int i=1;i<=N;++i) read(a[i]),Bel[i]=(i-1)/uni+1; for(re int i=1;i<=Q;++i) { read(Qr[i].opt,Qr[i].l,Qr[i].r); if(Qr[i].opt==1) read(Qr[i].v); } for(re int i=1;i<=Bel[N];++i) { Tot=0; Lst[i]=(i-1)*uni+1,Rst[i]=std::min(i*uni,N); for(re int j=1;j<=Q;++j) { auto q=Qr[j]; if(q.opt==2) ++Tot; if(q.l>Rst[i]||q.r<Lst[i]) continue; int _l=std::max(q.l,Lst[i])-Lst[i]+1,_r=std::min(q.r,Rst[i])-Lst[i]+1; if(q.opt==1) { if(q.l<=Lst[i]&&Rst[i]<=q.r) Blc.add_all(q.v); else Blc.add_part(_l,_r,q.v); } else Blc.query(_l,_r,Tot); } Blc.bel=i;Blc.n=Rst[i]-Lst[i]+1; if(i==1||i==Bel[N]) pTop=0,Blc.Tr.locate(1,1,Blc.n); Blc.prefix();Blc.solve();Blc.clear(); } for(re int i=1;i<=Tot;++i) write(ans[i].val,'\n'); return 0; }
|