意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

POJ1195Mobilephones二维线段树简单应用

来源:恒创科技 编辑:恒创科技编辑部
2024-02-04 01:27:59


Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The squares form an S * S matrix with the rows and columns numbered from 0 to S-1. Each square contains a base station. The number of active mobile phones inside a square can change because a phone is moved from a square to another or a phone is switched on or off. At times, each base station reports the change in the number of active phones to the main base station along with the row and the column of the matrix.

Write a program, which receives these reports and answers queries about the current total number of active mobile phones in any rectangle-shaped area.



POJ1195Mobilephones二维线段树简单应用

Input

The input is read from standard input as integers and the answers to the queries are written to standard output as integers. The input is encoded as follows. Each input comes on a separate line, and consists of one instruction integer and a number of parameter integers according to the following table.



The values will always be in range, so there is no need to check them. In particular, if A is negative, it can be assumed that it will not reduce the square value below zero. The indexing starts at 0, e.g. for a table of size 4 * 4, we have 0 <= X <= 3 and 0 <= Y <= 3.



Table size: 1 * 1 <= S * S <= 1024 * 1024


Cell value V at any time: 0 <= V <= 32767


Update amount: -32768 <= A <= 32767


No of instructions in input: 3 <= U <= 60002


Maximum number of phones in the whole table: M= 2^30


Output

Your program should not answer anything to lines with an instruction other than 2. If the instruction is 2, then your program is expected to answer the query by writing the answer as a single line containing a single integer to standard output.

Sample Input

0 41 1 2 3 2 0 0 2 2 1 1 1 2 1 1 2 -1 2 1 1 2 3 3

Sample Output

34



二维线段树裸题。

var pd,n,x,y,a,l,b,r,t:longint;
tree:array[0..1024*3+1,0..1024*3+1] of longint;
procedure updatey(xid,id,L,R,gy,num:longint);
var mid:longint;
begin
tree[xid,id]:=tree[xid,id]+num;
if L=R then exit;
mid:=(L+R) div 2;
if gy<=mid then updatey(xid,id*2,L,mid,gy,num)
else updatey(xid,id*2+1,mid+1,R,gy,num);
end;
procedure updatex(id,L,R,gx,gy,num:longint);
var mid:longint;
begin
updatey(id,1,1,n,gy,num);
if L=R then exit;
mid:=(L+R) div 2;
if gx<=mid then updatex(id*2,L,mid,gx,gy,num)
else updatex(id*2+1,mid+1,R,gx,gy,num);
end;
function sumy(xid,id,L,R,sr,er:longint):longint;
var mid:longint;
begin
if (L=sr)and(R=er) then
exit(tree[xid,id]);
mid:=(L+R) div 2;
if er<=mid then exit(sumy(xid,id*2,L,mid,sr,er));
if sr>mid then exit(sumy(xid,id*2+1,mid+1,R,sr,er));
exit(sumy(xid,id*2,L,mid,sr,mid)+sumy(xid,id*2+1,mid+1,R,mid+1,er));
end;
function sumx(id,L,R,sl,el,sr,er:longint):longint;
var mid:longint;
begin
if (L=sl)and(R=el) then
exit(sumy(id,1,1,n,sr,er));
mid:=(L+R) div 2;
if el<=mid then exit(sumx(id*2,L,mid,sl,el,sr,er));
if sl>mid then exit(sumx(id*2+1,mid+1,R,sl,el,sr,er));
exit(sumx(id*2,L,mid,sl,mid,sr,er)+sumx(id*2+1,mid+1,R,mid+1,el,sr,er));
end;
begin
pd:=0;
while pd<>3 do
begin
read(pd);
if pd=0 then readln(n);
if pd=1 then
begin
readln(x,y,a);
updatex(1,1,n,x+1,y+1,a);
end;
if pd=2 then
begin
readln(l,b,r,t);
writeln(sumx(1,1,n,l+1,r+1,b+1,t+1));
end;
end;
end.

上一篇: POJ1002487-3279直接哈希模拟 下一篇: 手机怎么远程登录云服务器?