【NOIP2017提高组模拟12.24】C

时间:2022-12-17 16:49:54

题目就不说了。
前6个操作废的,作用是来增加代码量。
事实证明出题人成功了,我改了一中午就是因为一个变量写错了导致错了一中午。
这说明我还不是一个标准的码农(mengbier.jpg)
第7个操作复杂一些,要维护很多东西。

uses math;
type node=record
mn,mx,l,r,lgs,llgs,rlgs,lazy1,lazy2,lnum,rnum:longint;
s:int64;
end;
var
i,j,k,p,n,m,t,l,r,q,v:longint;
ans:int64;
tr:array[0..600000]of node;
a:Array[0..600000]of longint;
procedure update(x:longint);
begin
tr[x].mx:=max(tr[x*2].mx,tr[x*2+1].mx);
tr[x].mn:=min(tr[x*2].mn,tr[x*2+1].mn);
tr[x].lnum:=tr[x*2].lnum;
tr[x].rnum:=tr[x*2+1].rnum;

tr[x].s:=tr[x*2].s+tr[x*2+1].s;

tr[x].lgs:=max(tr[x*2].lgs,tr[x*2+1].lgs);
if (tr[x*2].rnum=tr[x*2+1].lnum)then
tr[x].lgs:=max(tr[x].lgs,tr[x*2].rlgs+tr[x*2+1].llgs);

tr[x].llgs:=tr[x*2].llgs;

if (tr[x*2].llgs=tr[x*2].r-tr[x*2].l+1)
and(tr[x*2].rnum=tr[x*2+1].lnum)then
tr[x].llgs:=tr[x*2].llgs+tr[x*2+1].llgs;


tr[x].rlgs:=tr[x*2+1].rlgs;

if (tr[x*2+1].rlgs=tr[x*2+1].r-tr[x*2+1].l+1)and
(tr[x*2].rnum=tr[x*2+1].lnum)then
tr[x].rlgs:=tr[x*2+1].rlgs+tr[x*2].rlgs;
end;

procedure pushdown(x:longint);
var
w:longint;
begin
if (tr[x].l=tr[x].r)then exit;
if (tr[x].lazy2<>maxlongint)then
begin
w:=tr[x].lazy2;
tr[x*2].s:=int64(tr[x*2].r-tr[x*2].l+1)*w;
tr[x*2+1].s:=int64(tr[x*2+1].r-tr[x*2+1].l+1)*w;
tr[x*2].lnum:=w;
tr[x*2].rnum:=w;
tr[x*2].mx:=w;
tr[x*2].mn:=w;
tr[x*2+1].lnum:=w;
tr[x*2+1].rnum:=w;
tr[x*2+1].mn:=w;
tr[x*2+1].mx:=w;
tr[x*2].lgs:=tr[x*2].r-tr[x*2].l+1;
tr[x*2].llgs:=tr[x*2].r-tr[x*2].l+1;
tr[x*2].rlgs:=tr[x*2].r-tr[x*2].l+1;

tr[x*2+1].lgs:=tr[x*2+1].r-tr[x*2+1].l+1;
tr[x*2+1].llgs:=tr[x*2+1].r-tr[x*2+1].l+1;
tr[x*2+1].rlgs:=tr[x*2+1].r-tr[x*2+1].l+1;

tr[x*2].lazy1:=0;
tr[x*2+1].lazy1:=0;
tr[x*2].lazy2:=w;
tr[x*2+1].lazy2:=w;
tr[x].lazy2:=maxlongint;
end;
if (tr[x].lazy1<>0)then
begin
w:=tr[x].lazy1;
tr[x*2].s:=tr[x*2].s+int64(tr[x*2].r-tr[x*2].l+1)*w;
tr[x*2+1].s:=tr[x*2+1].s+int64(tr[x*2+1].r-tr[x*2+1].l+1)*w;
inc(tr[x*2].lnum,w);
inc(tr[x*2].rnum,w);
inc(tr[x*2].mn,w);
inc(tr[x*2].mx,w);
inc(tr[x*2+1].lnum,w);
inc(tr[x*2+1].rnum,w);
inc(tr[x*2+1].mn,w);
inc(tr[x*2+1].mx,w);
inc(tr[x*2].lazy1,w);
inc(tr[x*2+1].lazy1,w);
tr[x].lazy1:=0;
end;
end;
procedure build(x,l,r:longint);
var
m:longint;
begin
tr[x].l:=l;
tr[x].r:=r;
tr[x].lazy2:=maxlongint;
if l=r then
begin
tr[x].mx:=a[l];
tr[x].mn:=a[l];
tr[x].s:=a[l];
tr[x].lnum:=a[l];
tr[x].rnum:=a[l];
tr[x].lgs:=1;
tr[x].llgs:=1;
tr[x].rlgs:=1;
exit;
end;
m:=(l+r)div 2;
build(x*2,l,m);
build(x*2+1,m+1,r);
update(x);
end;
procedure ins(x,l,r,v:longint);
var
m:longint;
begin
pushdown(x);
if (tr[x].l=l)and(tr[x].r=r) then
begin
tr[x].s:=tr[x].s+(r-l+1)*v;
inc(tr[x].lnum,v);
inc(tr[x].rnum,v);
inc(tr[x].mn,v);
inc(tr[x].mx,v);
inc(tr[x].lazy1,v);
exit;
end;
m:=(tr[x].l+tr[x].r)div 2;
if r<=m then ins(x*2,l,r,v)
else if l>m then ins(x*2+1,l,r,v)
else begin
ins(x*2,l,m,v);
ins(x*2+1,m+1,r,v);
end;
update(x);
end;
procedure fill(x,l,r,v:longint);
var
m:longint;
begin
pushdown(x);
if (tr[x].l=l)and(tr[x].r=r)then
begin
tr[x].s:=(r-l+1)*v;
tr[x].lnum:=v;
tr[x].rnum:=v;
tr[x].mn:=v;
tr[x].mx:=v;
tr[x].lgs:=r-l+1;
tr[x].llgs:=r-l+1;
tr[x].rlgs:=r-l+1;
tr[x].lazy2:=v;
exit;
end;
m:=(tr[x].l+tr[x].r)div 2;
if r<=m then fill(x*2,l,r,v)
else if l>m then fill(x*2+1,l,r,v)
else begin
fill(x*2,l,m,v);
fill(x*2+1,m+1,r,v);
end;
update(x);
end;
function findmn(x,l,r:longint):longint;
var
m:longint;
begin
pushdown(x);
if (l=tr[x].l)and(tr[x].r=r) then
exit(tr[x].mn);
m:=(tr[x].l+tr[x].r)div 2;
if r<=m then exit(findmn(x*2,l,r))
else if l>m then exit(findmn(x*2+1,l,r))
else begin
exit(min(findmn(x*2,l,m),findmn(x*2+1,m+1,r)));
end;
end;

function findmx(x,l,r:longint):longint;
var
m:longint;
begin
pushdown(x);
if (l=tr[x].l)and(tr[x].r=r) then
exit(tr[x].mx);
m:=(tr[x].l+tr[x].r)div 2;
if r<=m then exit(findmx(x*2,l,r))
else if l>m then exit(findmx(x*2+1,l,r))
else begin
exit(max(findmx(x*2,l,m),findmx(x*2+1,m+1,r)));
end;
end;
function finds(x,l,r:longint):int64;
var
m:longint;
begin
pushdown(x);
if (l=tr[x].l)and(tr[x].r=r) then
exit(tr[x].s);
m:=(tr[x].l+tr[x].r)div 2;
if r<=m then exit(finds(x*2,l,r))
else if l>m then exit(finds(x*2+1,l,r))
else begin
exit(finds(x*2,l,m)+finds(x*2+1,m+1,r));
end;
end;

function query(x,l,r:longint):longint;
var
m,ans:longint;
begin
pushdown(x);
if (tr[x].l=l)and(tr[x].r=r)then exit(tr[x].lgs);
m:=(tr[x].l+tr[x].r)div 2;
if (r<=m) then exit(query(x*2,l,r))
else if (l>m)then exit(query(x*2+1,l,r))
else
begin
ans:=max(query(x*2,l,m),query(x*2+1,m+1,r));
if (tr[x*2].rnum=tr[x*2+1].lnum)then
ans:=max(ans,min(tr[x*2].rlgs,m-l+1)+min(tr[x*2+1].llgs,r-m));
exit(ans);
end;
end;
begin
readln(n,q);
for i:=1 to n do read(a[i]);
build(1,1,n);
for i:=1 to q do
begin
read(t,l,r);
if t<=3 then
begin
read(v);
if t=2 then v:=-v;
if t=3 then fill(1,l,r,v)
else
ins(1,l,r,v);
end
else if t=4 then
writeln(finds(1,l,r))
else if t=5 then writeln(findmn(1,l,r))
else if t=6 then writeln(findmx(1,l,r))
else if t=7 then writeln(query(1,l,r));
end;
end.