比赛 20110728 评测结果 WWWTTTTTTT
题目名称 蝗灾 最终得分 0
用户昵称 老虎小飞 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2011-07-28 12:08:41
显示代码纯文本
var
h,z,d,fd:array[0..510000]of longint;
next,mo,v:array[0..410000]of longint;
f:array[0..2000000]of boolean;
ch:array[0..2000000,0..1]of longint;
l,r:array[0..2000000]of longint;
n,w,i,j,q,x,y,xx,yy,tb,tx,a,ans,t:longint;

procedure add(i,x:longint);
var
m:longint;
begin
    f[i]:=true;
    if l[i]=r[i] then exit;
    m:=(l[i]+r[i]) div 2;
    if x<=m then begin
        if ch[i,0]=0 then begin
            inc(tx);ch[i,0]:=tx;
            l[tx]:=l[i];r[tx]:=m;
        end;
        add(ch[i,0],x);
    end
    else begin
        if ch[i,1]=0 then begin
            inc(tx);ch[i,1]:=tx;
            l[tx]:=m+1;r[tx]:=r[i];
        end;
        add(ch[i,1],x)
    end;
end;

procedure ddd(a,q:longint);
var
e,b,e0:longint;
begin
    if q=1 then begin
        e:=h[a]; inc(t);
        while e<>0 do begin
            b:=mo[e];
            if (b>=x)and(b<=xx) then inc(ans,v[e]);
            if fd[b]=t then begin
                inc(v[d[b]],v[e]);
                next[e0]:=next[e];
            end
            else begin
                fd[b]:=t;
                d[b]:=e;
            end;
            e0:=e;e:=next[e];
        end;
    end
    else begin
        e:=z[a]; inc(t);
        while e<>0 do begin
            b:=mo[e];
            if (b>=y)and(b<=yy) then inc(ans,v[e]);
            if fd[b]=t then begin
                inc(v[d[b]],v[e]);
                next[e0]:=next[e];
            end
            else begin
                fd[b]:=t;
                d[b]:=e;
            end;
            e0:=e;e:=next[e];
        end;
    end;
end;

procedure suan(i,x,y,q:longint);
var
m:longint;
begin
    if (i=0)or(not f[i]) then exit;
    if l[i]=r[i] then ddd(l[i],q)
    else begin
        m:=(l[i]+r[i]) div 2;
        if x<=m then suan(ch[i,0],x,y,q);
        if y>m then suan(ch[i,1],x,y,q);
    end;
end;

begin
    assign(input,'locust.in');reset(input);
    assign(output,'locust.out');rewrite(output);
    read(w,n);
    tx:=2;
    l[1]:=1;r[1]:=w; l[2]:=1;r[2]:=w;
    for i:=1 to n do begin
        read(q);
        if q=1 then begin
            read(x,y,a);
            inc(tb);
            next[tb]:=h[x];h[x]:=tb;mo[tb]:=y;v[tb]:=a;
            inc(tb);
            next[tb]:=z[y];z[y]:=tb;mo[tb]:=x;v[tb]:=a;
            add(1,x);add(2,y);
        end
        else begin
            read(x,y,xx,yy);
            ans:=0;
            if xx-x<yy-y then begin
                suan(1,x,xx,1);
            end
            else begin
                suan(2,y,yy,2);
            end;
            writeln(ans);
        end;
    end;
    close(input);close(output);
end.