[NOIP 2012] 借教室

传送门

https://vijos.org/p/1782

线段树

var
 x:array[0..4000000,1..4]of longint;
 i,j,k:longint;
 n,m,a,b,c,ans:longint;
function min(a,b:longint):longint;
begin
 if a>b then exit(b) else exit(a);
end;

procedure build(a,l,r:longint);
var mid:longint;
begin
 x[a,1]:=l; x[a,2]:=r; x[a,4]:=0;
 if l=r then begin read(x[a,3]); exit; end;
 mid:=(l+r)>>1;
 build(a<<1,l,mid); build(a<<1+1,mid+1,r);
 x[a,3]:=min(x[a<<1,3],x[a<<1+1,3]);
end;

procedure pushdown(a:longint);
begin
 if x[a,1]=x[a,2] then exit;
 dec(x[a<<1,3],x[a,4]); inc(x[a<<1,4],x[a,4]);
 dec(x[a<<1+1,3],x[a,4]); inc(x[a<<1+1,4],x[a,4]);
 x[a,4]:=0;
end;

function query(a,l,r:longint):longint;
var mid:longint;
begin
 if x[a,4]<>0 then pushdown(a);
 if (x[a,1]=l)and(x[a,2]=r) then exit(x[a,3]);
 mid:=(x[a,1]+x[a,2])>>1;
 if r<=mid then exit(query(a<<1,l,r)) else
 if l>mid then exit(query(a<<1+1,l,r))
 else exit(min(query(a<<1,l,mid),query(a<<1+1,mid+1,r)));
end;

procedure update(a,l,r,b:longint);
var mid:longint;
begin
 if x[a,4]<>0 then pushdown(a);
 if (x[a,1]=l)and(x[a,2]=r) then begin dec(x[a,3],b); inc(x[a,4],b); exit; end;
 mid:=(x[a,1]+x[a,2])>>1;
 if r<=mid then update(a<<1,l,r,b) else
 if l>mid then update(a<<1+1,l,r,b)
 else begin update(a<<1,l,mid,b); update(a<<1+1,mid+1,r,b); end;
 x[a,3]:=min(x[a<<1,3],x[a<<1+1,3]);
end;

begin
 readln(n,m);
 build(1,1,n);
 for i:=1 to m do
  begin
   readln(a,b,c);
   ans:=query(1,b,c);
   if ans<a
   then begin writeln(-1); writeln(i); halt; end
   else update(1,b,c,a);
  end;
 writeln(0);
end.

你可能感兴趣的:([NOIP 2012] 借教室)