天天看點

JZOJ4711Binary 樹狀數組+二進制處理

這題花了我一個下午+半個晚上,結果改到最後我發現是空間開小了。。。mdzz

好累啊,,我直接貼了。。

JZOJ4711Binary 樹狀數組+二進制處理

題解:

JZOJ4711Binary 樹狀數組+二進制處理

代碼:

var
        //i,j,k,p,n,m:longint;
        c:Array[..,..]of longint;
        a:Array[..]of longint;
        b:array[..]of longint;
        i,j,n,m,x,y,p:longint;
        ans,v,s1:int64;
procedure swap(var x,y:longint);
var
        t:longint;
begin
        t:=x;
        x:=y;
        y:=t;
end;
function lowbit(x:longint):longint;
begin
        exit(x and (-x));
end;

procedure change(p,k,v:longint);
var
    v1:longint;
begin
    v1:=<<(p+);
    while (k<=v1)do 
    begin
        inc(c[p,k],v);
        k:=k+lowbit(k);
    end;
end;
function gets(p,k:longint):longint;
var
    s:longint;
begin
    s:=;
    while (k>)do 
    begin
        inc(s,c[p,k]);
        dec(k,lowbit(k));
    end;
    exit(s);
end;
procedure turn(x,v:longint);
var
    j,v1:longint;
begin
    for j:= to  do
    begin
        v1:=<<j;
        change(j,a[x] mod (v1*)+,v);
    end;
end;
function find(p,l,r:longint):longint;
var
    v:longint;
begin
    inc(l);
    inc(r);
    v:=<<(p+);
    if (l>r)then swap(l,r);
    if (r<=)then exit(gets(p,r+v)-gets(p,l+v-))

    else if (l<=)then exit(gets(p,r)+gets(p,v)-gets(p,l+v-))
    else exit(gets(p,r)-gets(p,l-));
end;
begin
    readln(n,m);
    for i:= to n do
    begin
        read(a[i]);
        turn(i,);
    end;
    for i:= to m do
    begin
        read(p,x,y);
        if (p=)then
        begin
            turn(x,-);
            a[x]:=y;
            turn(x,);
        end
        else
        begin
            ans:=;
            for j:= to  do
            begin
                v:=<<j;
                if ((v and y)=)then continue; 
                s1:=find(j,v-x mod(v*),v*-x mod (v*)-);
                ans:=ans+s1*v;
            end;
            writeln(ans);
        end;
    end;
end.
           

雖然感覺很馬虎,但是今天讓我非常無語。。沒有心情寫題解。。。