POJ 1195 Mobile Phones (two-dimensional tree-like array)

Source: Internet
Author: User

Mobile Phones
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 15968 Accepted: 7373

Description

Suppose the fourth generation mobile phone base stations in the Tampere area operate as follows. The area was 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 was 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 on the current total number of active mobile phones I N any rectangle-shaped area.

Input

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

The values would always are in range and so there are no need to check them. In particular, if A was negative, it can be assumed that it'll 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 isn't answer anything to lines with a instruction other than 2. If the instruction is 2 and then your program was expected to answer the query by writing the answer as a single line Containi Ng a single integer to standard output.

Sample Input

0 41 1 2 32 0 0 2 2 1 1 1 21 1 2-12 1 1 2 3 3

Sample Output

34

Source

IOI 2001



Test Instructions: The first line input two data id,n, respectively, represents the number and the size of the matrix, the following data each row of the first data represents the number, 1 will enter 3 number to think, there, Z for the matrix in the position row x, column y element plus Z (initialization matrix all elements are 0), 2 delegates will enter 4 number xx,yy,x,y. Represents the upper-left corner coordinates of X, y, and lower-right coordinates for all elements in the xx,yy matrix


#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include < stdlib.h>using namespace Std;const int maxn = 1025;int n,id;int c[maxn][maxn];int lowbit (int x) {return x& (-X);}        void Updata (int i,int j,int k) {while (i<=n) {int PJ = j;            while (pj<=n) {C[I][PJ] + = k;        PJ + = Lowbit (PJ);    } i + = Lowbit (i);    }}int getsum (int i,int j) {int sum = 0;        while (i>0) {int PJ = j;            while (pj>0) {sum + = C[I][PJ];        PJ-= Lowbit (PJ);    } I-= Lowbit (i); } return sum;}    int main () {scanf ("%d%d", &id,&n);    int x,y,z,xx,yy;        while (scanf ("%d", &id)!=eof) {if (id = = 3) {break;            } if (id = = 1) {scanf ("%d%d%d", &x,&y,&z);            x + +;            y++;        Updata (x, y, z); } else if (id = = 2) {SCANF ("%d%d%d%d", &x,&y,&xx,&yy);            x + +;            y++;            xx++;            yy++;        printf ("%d\n", Getsum (XX,YY)-getsum (x-1,yy)-Getsum (xx,y-1) + getsum (x-1,y-1)); }} return 0;}



POJ 1195 Mobile Phones (two-dimensional tree-like array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.