POJ 1195 Mobile phones (two-dimensional tree array), poj1195

Source: Internet
Author: User

POJ 1195 Mobile phones (two-dimensional tree array), poj1195

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 sum and modification of two-dimensional tree Arrays can be extended from one dimension to two-dimensional.


#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <set>#include <stack>#include <cctype>#include <algorithm>#define lson o<<1, l, m#define rson o<<1|1, m+1, rusing namespace std;typedef long long LL;const int mod = 99999997;const int MAX = 1000000000;const int maxn = 100005;int ca, s, op;int c[1100][1100];void add(int i, int j, int v) {    while(i <= s) {        int y = j;        while(y <= s) {            c[i][y] += v;            y += y&-y;        }        i += i&-i;    }}int query(int i ,int j) {    int ans = 0;    while(i > 0) {        int y = j;        while(y > 0) {            ans += c[i][y];            y -= y&-y;        }        i -= i&-i;    }    return ans;}int main(){    cin >> ca >> s;    while(1) {        scanf("%d", &op);        if(op == 1) {            int a, b, v;            scanf("%d%d%d", &a, &b, &v);            add(a+1, b+1, v);        } else if(op == 2) {            int x1, x2, y1, y2;            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);            int ans = query(x2+1, y2+1) + query(x1, y1) - query(x2+1, y1) - query(x1, y2+1);            printf("%d\n", ans);        } else break;    }    return 0;}


Zookeeper
Who has the correspondence between data structure algorithms and POJ questions?

An asterisk indicates a typical question or a better algorithm.

1195 Mobile phones tree Array
1455
1521 Entropy huffman
1703 Find them, Catch them and query the set
1785 Binary Search Heap Construction
1794 Castle Wils Reverse Order
1961 Period KMP recurrence factor
1984 * Navigation Nightmare and check the set + coordinate translation
1986 * Distance Queries LCA
1988 * Cube Stacking and query set applications
1990 * MooFest line segment tree
2010 * Moo University-Financial Aid Max heap-min heap
2182 Lost Cows line segment tree
2183 Bovine Math Geniuses hash
2188 Cow Laundry Reverse Order
2227 The Wedding Juicer heap + floodfill
2236 Wireless Network and query set
2266 * Quadtree Recursion
2269 * Friends expression
2270 Quadtree II or: Florida Jones strikes back will be 2266 vice versa
2299 Ultra-QuickSort Merge Sorting
2352 Stars tree Array
2395 Out of Hay and query set
2482 Stars in Your Window static 2 Cross Tree
2513 Colored Sticks and check the set
2524 Ubiquitous Religions and query set
2528 Mayor's posters line segment tree
2567 Code the Tree
2750 * Potted Flower line segment tree
2777 Count Color line segment tree
2796 Feel Good RMQ
2823 Sliding Window heap or double-end queue
2828 Buy Tickets line segment tree
2886 * Who Gets the Most Candies? Line Segment tree
2892 * Tunnel Warfare tree Array
3214 * Heap post-order traversal. Each node deducts the corresponding sub-guarantee attribute, and then calculates the longest non-descending sequence for the traversal result.
3253 Fence Repair huffman
3263 Tallest Cow line segment tree
3274 * Gold Balanced Lineup hash
3277 City Horizon line segment tree
3320 Jessica's Reading Problem queue operation or minimum heap
3321 * Apple Tree Array
3332 Parsing Real Numbers DFA
3344 Chessboard Dance queue Simulation
3349 Snowflake Snow Snowflakes hash (or brute force)
3437 Tree Grafting dfs Tree construction
3461 Oulipo KMP
3468 A Simple Problem with Integers segment tree Interval Update, lazy operation
3631 Cuckoo Hashing and query Sets
3667 Hotel line segment tree
3690 Constellations trie match
3695 Rectangles matrix cutting... remaining full text>

How does POJ 2481 cows do? Can a tree array + a quick row timeout be accompanied by the Pascal program?

/**
Sort sorting + tree array, no timeout
Sort by e in descending order. If e is equal, the priority of s is smaller, and then the sorted array is scanned in order. For each cow, you only need to find
S is less than or equal to the number of cows, but note that two cows, e and s are equal.
**/
The programming language doesn't matter. If you have good pascal skills, the following
Code, should be well understood,:
# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
Const int n= 100010;
// Tree Array
Int tree [N];
Int getsum (int idx ){
Int sum = 0;
For (int I = idx; I> 0; I-= (I &-I ))
Sum + = tree [I];
Return sum;
}

Int maxv;
Void update (int idx, int val ){
For (int I = idx; I <= maxv; I ++ = (I &-I ))
Tree [I] + = val;
}

Struct range {
Int ord; // store the serial number of each cow before sorting.
Int s, e;
} Cow [N];
// Sorting predicate Definition
Int cmp (const range & x, const range & y ){
If (x. e = y. e)
Return x. s <y. s;
Return x. e> y. e;
}

Int main ()
{
Int n, res [N];
While (scanf ("% d", & n )! = EOF & n ){
Memset (tree, 0, sizeof (int) * (n + 2 ));
Maxv = 0;
For (int I = 0; I <n; ++ I ){
Scanf ("% d", & cow [I]. s, & cow [I]. e );
++ Cow [I]. s, ++ cow [I]. e;
If (cow [I]. s> maxv) maxv = cow [I]. s;
Cow [I]. ord = I;
}
Std: sort (cow, cow + n, cmp );
For (int I = 0; I <n; ++ I ){
If (cow [I]. s = cow [I-1]. s & cow [I]. e = cow [I-1]. e) // note that the two cows are the same
Res [cow [I]. ord] = res [cow [I-1]. ord];
Else
Res [cow [I]. ord] = getsum (cow [I]. s-1 );
Update (cow [I]. s, 1 );
}
Printf ("% d", res [0]);
... The remaining full text>

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.