Mobile phones
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 receives 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 particular, 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 should 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 4
1 1 2 3
2 0 0 2 2
1 1 1 2
1 1 2 -1
2 1 1 2 3
3
Sample Output
3
4
Source
题目类型:二维树状数组
算法分析:这是一道使用二维树状数组求解的经典问题,直接使用二维树状数组的单点更新和二维区间求和即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 7; const int maxn = 1660; int c[maxn][maxn], n; int lowbit (int val) { return val & (-val); } void UpDate (int x, int y, int val) { while (x <= n) { int temp = y; while (temp <= n) { c[x][temp] += val; temp += lowbit (temp); } x += lowbit (x); } } int Query (int x, int y) { int sum = 0; while (x > 0) { int temp = y; while (temp > 0) { sum += c[x][temp]; temp -= lowbit (temp); } x -= lowbit (x); } return sum; } int main() { // ifstream cin ("aaa.txt"); // freopen ("aaa.txt", "r", stdin); int cmd; while (scanf ("%d", &cmd) != EOF) { if (cmd == 0) { scanf ("%d", &n); memset (c, 0, sizeof (c)); } else if (cmd == 3) { break; } else if (cmd == 1) { int x, y, val; scanf ("%d%d%d", &x, &y, &val); x++, y++; UpDate (x, y, val); } else { int L, R, B, T; scanf ("%d%d%d%d", &L, &B, &R, &T); L++, B++, R++, T++; printf ("%d\n", Query (R, T) + Query (L - 1, B - 1) - Query (L - 1, T) - Query (R, B - 1)); } } return 0; } |
- « 上一篇:poj1182
- poj1218:下一篇 »