"POJ 1195" Mobile phones (tree-like array)

Source: Internet
Author: User

"POJ 1195" Mobile phones (tree-like array)


Mobile Phones
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 16761 Accepted: 7713

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


Two-dimensional tree array One before the first one-dimensional writing is the second-dimensional tree-like array, one-dimensional violence ... Ran more than 4,000.

Later checked the first dimension and the second dimension as the cumulative lowbite can be queried when using the large matrix minus the transverse matrix and the left longitudinal so much reduced the upper left corner of the small matrix Plus you can speed up a lot faster

The rise of a class play.


The code is as follows:

#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib>using namespace std;    Class Trie{public:trie () {memset (tr,0,sizeof (tr));        } void run (int t) {int a,b,c,d;            if (!t) {scanf ("%d", &a);        n = A;            } else if (t = = 1) {scanf ("%d%d%d", &a,&b,&c);        ADD (A+1,B+1,C);            } else {scanf ("%d%d%d", &a,&b,&c,&d);        printf ("%d\n", Sum (c+1,d+1) +sum (A, B)-sum (a,d+1)-sum (c+1,b));    }}private:int Tr[1033][1033],n;    int lowbite (int x) {return x& (-X);        } void Add (int x,int y,int data) {int i,j;    for (i = x; i <= n; i + = Lowbite (i)) for (j = y; J <= N; j + = Lowbite (j)) Tr[i][j] + = data;        } int Sum (int x,int y) {int ans = 0;        int i,j; for (i = x; i > 0; I-= Lowbite (i)) for (j = y; j > 0; J-= Lowbite (j)) ans + = tr[i][j];    return ans;    }};int Main () {int t;    Trie *tr = new Trie ();    while (~SCANF ("%d", &t) && t! = 3) {Tr->run (t); } return 0;}

Original # include <iostream> #include <cstring> #include <cstdio> #include <cstdlib>using namespace Std;int tr[1033][1033],n;int lowbite (int x) {return x& (-X);}    void Add (int x,int y,int data) {int i,j; for (i = x; i <= n; i + = Lowbite (i)) for (j = y; J <= N; j + = Lowbite (j)) Tr[i][j] + = data;}    int Sum (int x,int y) {int ans = 0;    int i,j;    for (i = x; i > 0; I-= Lowbite (i)) for (j = y; j > 0; J-= Lowbite (j)) ans + = tr[i][j]; return ans;}    int main () {int t,a,b,c,d;    memset (tr,0,sizeof (tr));            while (~SCANF ("%d", &t) && t! = 3) {if (!t) {scanf ("%d", &a);        n = A;            } else if (t = = 1) {scanf ("%d%d%d", &a,&b,&c);        ADD (A+1,B+1,C);            } else {scanf ("%d%d%d", &a,&b,&c,&d);        printf ("%d\n", Sum (c+1,d+1) +sum (A, B)-sum (a,d+1)-sum (c+1,b)); }    }   return 0;} 


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"POJ 1195" Mobile phones (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.