求助啊,样例过不了
查看原帖
求助啊,样例过不了
70635
皮皮羊楼主2018/6/14 18:09
var
s,x,y,n,m,z,i:longint;
lazy,sum:array[0..500000] of longint;
procedure pushup(root:longint);
begin
  sum[root]:=sum[root *2]+sum[root *2+1];
end;
procedure pushdown(root,d:longint);
begin
  if (lazy[root]<>0) then
    begin
      lazy[root *2]:=lazy[root];
      lazy[root *2+1]:=lazy[root];
      sum[root *2+1]:=(d div 2 )*lazy[root];
      sum[root *2]:=(d-(d div 2 ))*lazy[root];
      lazy[root]:=0;
    end;
end;
procedure update(x,y,z,l,r,root:longint);
var mid:longint;
begin
  if (x<=l)and(y>=r) then
    begin
      lazy[root]:=lazy[root]+z;
      write(sum[root],' ');
      sum[root]:=sum[root]+(r-l+1)*z;
      //writeln(x,' ',y,' ',root,' ',l,' ',r,' ',z,' ',sum[root]);
      exit;
    end;
  pushdown(root,r-l+1);
  mid:=(l+r) div 2 ;
  if x<=mid then update(x,y,z,l,mid,root*2);
  if y>mid then update(x,y,z,mid+1,r,root*2+1);
  pushup(root);
end;
function query(x,y,l,r,root:longint):longint;
var ret,mid:longint;
begin
  if (x<=l)and(y>=r) then exit(sum[root]);
  pushdown(root,r-l+1);
  mid:=(l+r) div 2 ;
  ret:=0;
  if x<=mid then ret:=ret+query(x,y,l,mid,root *2);
  if y>mid then ret:=ret+query(x,y,mid+1,r,root *2+1);
  exit(ret);
end;
procedure build(l,r,root:longint);
var mid:longint;
begin
  lazy[root]:=0;
  if l=r then begin read(sum[root]);exit;end;
  mid:=(l+r) div 2 ;
  build(l,mid,root*2);
  build(mid+1,r,root *2+1);
end;
begin
  readln(n,m);
  build(1,n,1);
  for i:=1 to m do
    begin
      read(s);
      writeln('query:',query(1,5,1,5,1));
      if s=1 then
        begin
          readln(x,y,z);
          update(x,y,z,1,n,1);
        end
      else
        begin
          readln(x,y);
          writeln(query(x,y,1,n,1));
        end;
    end;
end.

2018/6/14 18:09
加载中...