POJ 1195 Mobile Phones

Source: Internet
Author: User

Links: http://poj.org/problem?id=1195


Mobile Phones
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 16646 Accepted: 7653
Descriptionsuppose 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.

Inputthe input is read from standard input as integers and the answers to the queries be written to standard output as in Tegers. 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

Outputyour 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

To give you a n*n matrix of the area, its row and column subscript is starting from 0 . Each matrix element represents a base station, and the number of mobile phones in the base station is constantly changing for a variety of reasons. Each base station reports to the main base station irregularly to count the number of mobile phones in operation. Your task is to complete the corresponding operation according to the corresponding command. The main thing is to update the value of a matrix element and the sum of the values of the matrix elements of an area.


Idea--it's obvious that this is a bare two-dimensional tree-shaped array. Its operation is to update a value and interval sum. Therefore, we can solve the problem by promoting it directly with a one-dimensional tree-like array. Note: The matrix element subscript starts at 0 .


Complexity analysis--time complexity : O ((log (n)) ^2), spatial complexity : O (n^2)


Attach the AC code:


#include <iostream> #include <cstdio> #include <string> #include <cmath> #include <iomanip > #include <ctime> #include <climits> #include <cstdlib> #include <cstring> #include < algorithm> #include <queue> #include <vector> #include <set> #include <map>//#pragma comment (Linker, "/stack:102400000, 102400000") using namespace std;typedef unsigned int li;typedef long long ll;typedef unsigned l Ong long Ull;typedef long double ld;const double pi = ACOs ( -1.0); const DOUBLE E = exp (1.0); const double EPS = 1e-8;const i NT MAXN = 1030;int mat[maxn][maxn];int order; Type the command int n; Array size int lowbit (int x); 2^k,k indicates that x is the number of 0 at the end of the binary void update (int x, int y, int add); Update status int sum (int x, int y); sum int main () {Ios::sync_with_stdio (false); int x1, y1, x2, y2, add, Ans;while (scanf ("%d", &order) ==1 && 3!=o Rder) {switch (order) {case 0:scanf ("%d", &n), memset (Mat, 0, sizeof (MAT)),//initialized to 0break;case 1:scanf ("%d%d%d", & X1, &y1, &add); Update (x1+1, y1+1, add); X, y may be 0, so add 1, back like Break;case 2:scanf ("%d%d%d%d", &x1, &y1, &x2, &y2); ans = 0;ans + = SUM (x2+1, y2+1); Total sum ans-= SUM (x2+1, y1); ans-= SUM (x1, y2+1); Remove the extra part ans + = SUM (x1, y1); Subtract part, add back, draw to know printf ("%d\n", ans); break;default:printf ("INPUT error!\n"); return 0;} int lowbit (int x) {return (x& (x));} void update (int x, int y, int add) {//x, y denotes matrix subscript, this function is to place the number labeled X, y plus addfor (int i=x; i!=0&&i<=n; i+=lowbit (i)) for ( int j=y; j!=0&&j<=n; J+=lowbit (j)) Mat[i][j] + = add;} int sum (int x, int y) {///x = y denotes matrix subscript, this function is to add the matrix element value of the subscript less than x, y, int res = 0;for (int i=x; i>0; i-=lowbit (i)) for (int j=y; j&gt ; 0; J-=lowbit (j)) Res + = Mat[i][j];return res;}


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

POJ 1195 Mobile Phones

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.