POJ1195 Mobile phones [two-dimensional line segment tree], poj1195phones

Source: Internet
Author: User

POJ1195 Mobile phones [two-dimensional line segment tree], poj1195phones

Mobile phones
Time Limit:5000 MS   Memory Limit:65536 K
Total Submissions:14291   Accepted:6644

Description

Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. the area is 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 is 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 has es these reports and answers queries about the current total number of active mobile phones in any rectangle-shaped area.

Input

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

The values will always be in range, so there is no need to check them. in particle, if A is negative, it can be assumed that it will 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 shocould not answer anything to lines with an instruction other than 2. if the instruction is 2, then your program is expected to answer the query by writing the answer as a single line containing 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

The solution to the two-dimensional tree array is as follows: http://blog.csdn.net/chang_mu/article/details/37739053question: update at, and query.
Question: The basic two-dimensional line segment tree (I read it for a long time today... it is not convenient to extend a line segment tree to a two-dimensional tree array, but once you think about it, you will find that the idea is not complex ).

# Include <stdio. h> # define maxn 1026 # define lson l, mid, rt <1 # define rson mid + 1, r, rt <1 | 1int tree [maxn * 3] [maxn * 3], size; void getSize () {scanf ("% d", & size );} void updateY (int rootX, int pos, int val, int l, int r, int rt) {tree [rootX] [rt] + = val; if (l = r) return; int mid = (l + r)> 1; if (pos <= mid) updateY (rootX, pos, val, lson); else updateY (rootX, pos, val, rson);} void updateX (int x, Int y, int val, int l, int r, int rt) {// update tasks are handed over to updateYupdateY (rt, y, val, 0, size-1, 1 ); if (l = r) return; int mid = (l + r)> 1; if (x <= mid) updateX (x, y, val, lson ); else updateX (x, y, val, rson);} void update () {int x, y, val; scanf ("% d", & x, & y, & val); updateX (x, y, val, 0, size-1, 1);} int queryY (int rootX, int y1, int y2, int l, int r, int rt) {if (l = y1 & r = y2) return tree [rootX] [rt ]; Int mid = (l + r)> 1; if (y2 <= mid) return queryY (rootX, y1, y2, lson); else if (y1> mid) return queryY (rootX, y1, y2, rson); else {return queryY (rootX, y1, mid, lson) + queryY (rootX, mid + 1, y2, rson) ;}} int queryX (int x1, int x2, int y1, int y2, int l, int r, int rt) {if (x1 = l & x2 = r) return queryY (rt, y1, y2, 0, size-1, 1 ); int mid = (l + r)> 1; if (x2 <= mid) return queryX (x1, x2, y1, y2, Lson); else if (x1> mid) queryX (x1, x2, y1, y2, rson); else {return queryX (x1, mid, y1, y2, lson) + queryX (mid + 1, x2, y1, y2, rson) ;}} void query () {int x1, y1, x2, y2; scanf ("% d", & x1, & y1, & x2, & y2); printf ("% d \ n", queryX (x1, x2, y1, y2, 0, size-1, 1);} void (* funArr []) () = {getSize, update, query}; int main () {int num; while (scanf ("% d", & num), num! = 3) (* funArr [num]) (); return 0 ;}



The advantages and disadvantages of mobile phones

Mobile phones have become increasingly popular in China these days. Wherever you go you can see people using mobile phones. Regular college students, even high school students, have mobile phones.
Mobile phones have brought people a lot of benefits, but the most important is that they are convenient. with mobile phones in their hands, they can keep in touch with anybody they want. if they want to get some information from the Internet, they can easily have their dream realized via mobile phones too. furthermore, if someone has a heart attack or a traffic accident, a call to emergency hospcy or to the police can quickly bring him the help wanhe.
However, mobile phones can also bring people problems. the most serous is the electric wave radiation which is thought to be harmful to users 'brains. another problem is that when people are having a meeting or having a class or at a concert etc, the ring of the mobile phone may interrupt others. so I think people shocould use mobilephones as little as possible and turn them off when they are attending important meetings or attending class
 
My opinion on Mobile phones

Lt is believed that the mobile phones are important in our daily life. because they make people more converient to communicate with each other. but using mobile phones may be bad for our health, if we always use mobile phones, we may be sick. so I think that we shoshould use them abstemiously.

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.