Mobile phones (two-dimensional tree-like array)

Source: Internet
Author: User

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 a read from standard input as integers and the answers to the queries be written to standard output as integers. 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 have in range, so there is 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 <= 1024x768 * 1024 
Cell value v at any time:0 <= v <= 32767 
UPD Ate amount: -32768 <= A <= 32767 
No of instructions in Input:3 <= U <= 60002 
Maximum nu Mber 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 4
1 1 2 3
2 0 0 2 2 
1 1 1 2
1 1 2-1
2 1 1 2 3 
3

Problem Solving Ideas:

is completely a two-dimensional tree-like array template, but more elaborate.

AC Code:

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace
Std
const int MAXN = 1030;
int C[MAXN][MAXN], s; int lowbit (int a) {return A & (-a),} void Update (int x,int y,int a) {for (int i = x; I <= s; i + = Lowbit (
i)) for (int j = y; J <= S; j + = Lowbit (j)) C[i][j] + = A;
    } int sum (int x,int y) {int sum = 0;
    for (int i = x; i > 0; I-= Lowbit (i)) for (int j = y; j > 0; J-= Lowbit (j)) sum + = C[i][j];
return sum;
    } int main () {int order, x, Y, A, L, B, r, T;
    memset (c, 0, sizeof (c));
    scanf ("0%d", &s);
        while (scanf ("%d", &order)) {if (order = = 3) break;
            if (order = = 1) {scanf ("%d%d%d", &x, &y, &a);
        Update (x + 1, y + 1, a);
            } if (order = = 2) {scanf ("%d%d%d%d", &l, &b, &r, &t); printf ("%d\n", Sum (R + 1,t + 1)-sum (R + 1, b)-sum (L, T + 1) + sum (l, b));
}} return 0;
 }


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.