POJ 1195-mobile Phones (two-dimensional tree array-interval update interval query)

Source: Internet
Author: User

Mobile Phones
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 17661 Accepted: 8173

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

Topic Meaning:there is a matrix (initialized to 0) that gives some actions:
1 x y a indicates the arr[x][y] plus A;
2 L B R T indicates that the upper-left corner is (l,b), and the lower-right corner is (r,t) of the Matrix.

Problem Solving Ideas:Search for interval update interval of bare two-dimensional tree array

#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm>using namespace STD; #define MAXN 1100int c[maxn][maxn],n,arr[maxn][maxn];int lowbit (int x) {return x& (-X);}    void update (int x,int y,int num) {int i,j; For (I=x, i<=n; i+=lowbit (i)) for (j=y; j<=n; J+=lowbit (j)) C[i][j]+=num;}    int sum (int x,int y) {int i,j,res=0;    For (I=x, i>0; i-=lowbit (i)) for (j=y; j>0; J-=lowbit (j)) Res+=c[i][j]; return res;} int getsum (int x1,int y1,int x2,int y2) {return sum (x2,y2)-sum (x1-1,y2)-sum (x2,y1-1) +sum (x1-1,y1-1);}    int main () {int op,x,y,l,b,r,t,a;            while (~SCANF ("%d", &op)) {if (op==0) {scanf ("%d", &n);        Memset (C,0,sizeof (c));            } else if (op==1) {scanf ("%d%d%d", &x,&y,&a);        Update (X+1,Y+1,A);     } else if (op==2) {scanf ("%d%d%d%d", &l,&b,&r,&t);       int ans=getsum (l+1,b+1,r+1,t+1);        printf ("%d\n", ans); }} return 0;}


POJ 1195-mobile Phones (two-dimensional tree array-interval update interval query)

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.