bzoj 3038 树状数组+并查集

同bzoj 3211

鬼知道为什么数列中的数不大于1e12,但是要用int64来存开根号的数 

var
        n,m,op,l,r,c    :longint;
        f               :array[0..100010] of longint;
        a               :array[0..100010] of int64;
        t               :array[0..100010] of int64;
        i               :longint;
function get_father(x:longint):longint;
begin
   if x=f[x] then exit(x);
   f[x]:=get_father(f[x]);
   exit(f[x]);
end;

procedure add(x:longint;v:int64);
begin
   while (x<=n) do
   begin
      t[x]:=t[x]+v;
      inc(x,x and (-x));
   end;
end;

function find(x:longint):int64;
var
        ans:int64;
begin
   ans:=0;
   while (x>0) do
   begin
      inc(ans,t[x]);
      dec(x,x and (-x));
   end;
   exit(ans);
end;

procedure modify(l,r:longint);
var
        t:int64;
        i:longint;
begin
   i:=get_father(l);
   while (i<=r) do
   begin
      t:=trunc(sqrt(a[i]));
      add(i,-a[i]+t);
      a[i]:=t;
      if (a[i]<=1) then f[i]:=get_father(i+1);
      i:=get_father(i+1);
   end;
end;


begin
   read(n);
   for i:=1 to n+1 do f[i]:=i;
   for i:=1 to n do
   begin
      read(a[i]);
      add(i,a[i]);
   end;
   read(m);
   for i:=1 to m do
   begin
      read(op,l,r);
      if l>r then
      begin
         c:=l; l:=r; r:=c;
      end;
      if op=0 then modify(l,r)
        else writeln(find(r)-find(l-1));
   end;
end.
——by Eirlys

你可能感兴趣的:(树状数组,并查集,bzoj)