2013
11-09

# Mobile phones

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.

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

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.

0 4
1 1 2 3
2 0 0 2 2
1 1 1 2
1 1 2 -1
2 1 1 2 3
3


3
4

import java.util.Scanner;
public class Main{
int n;
int[][] C;

public Main(){
}
private int lowbit(int t){
return t&(-t);
}

int Sum(int i, int j){
int result = 0;
for(int x = i; x > 0; x -= lowbit(x)) {
for(int y = j; y > 0; y -= lowbit(y)) {
result += C[x][y];
}
}
return result;
}

private void Modify(int i, int j, int delta){

// A[i][j]+=delta;

for(int x = i; x<=n; x += lowbit(x))
for(int y = j; y <=n; y += lowbit(y)){
C[x][y] += delta;

}
}

public static void  main(String[] args){
Main ma=new Main();
ma.go();
}

private void go(){
int k,a,b,cc,x,y;
Scanner in=new Scanner(System.in);
while(true){
k=in.nextInt();
if(k==3) break;
if(k==0){
n=in.nextInt();
C=new int[n+1][n+1];
}else if(k==1){
a=in.nextInt();
b=in.nextInt();
cc=in.nextInt();
a++;
b++;
Modify(a,b,cc);
}else{
x=in.nextInt();
y=in.nextInt();
a=in.nextInt();
b=in.nextInt();
x++;
y++;
a++;
b++;

System.out.printf("%d\n",Sum(a,b)+Sum(x-1,y-1)-Sum(a,y-1)-Sum(x-1,b));
}
}
}
}

1. 站长，你好！
你创办的的网站非常好，为我们学习算法练习编程提供了一个很好的平台，我想给你提个小建议，就是要能把每道题目的难度标出来就好了，这样我们学习起来会有一个循序渐进的过程！