Mobile phones POJ - 1195 二维树状数组求和

本文介绍了一个用于第四代移动电话基站的手机数量更新及查询系统。该系统通过接收基站报告来实时更新各区域内的活跃手机数量,并能够快速响应关于任意矩形区域内当前总活跃手机数的查询请求。
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

板子题
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <set>
 7 #include <iostream>
 8 #include <map>
 9 #include <stack>
10 #include <string>
11 #include <vector>
12 #define pi acos(-1.0)
13 #define eps 1e-6
14 #define fi first
15 #define se second
16 #define lson l,m,rt<<1
17 #define rson m+1,r,rt<<1|1
18 #define bug         printf("******\n")
19 #define mem(a,b)    memset(a,b,sizeof(a))
20 #define fuck(x)     cout<<"["<<x<<"]"<<endl
21 #define f(a)        a*a
22 #define sf(n)       scanf("%d", &n)
23 #define sff(a,b)    scanf("%d %d", &a, &b)
24 #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
25 #define pf          printf
26 #define FRE(i,a,b)  for(i = a; i <= b; i++)
27 #define FREE(i,a,b) for(i = a; i >= b; i--)
28 #define FRL(i,a,b)  for(i = a; i < b; i++)
29 #define FRLL(i,a,b) for(i = a; i > b; i--)
30 #define FIN freopen("DATA.txt","r",stdin)
31 #define lowbit(x)   x&-x
32 #pragma comment (linker,"/STACK:102400000,102400000")
33 
34 using namespace std;
35 typedef long long LL ;
36 const int maxn = 2e3 + 10;
37 int n, k, c[maxn][maxn];
38 void updata(int x, int y, int z) {
39     for (int i = x ; i <= n ; i += lowbit(i))
40         for (int j = y ; j <= n ; j += lowbit(j))
41             c[i][j] += z;
42 }
43 int sum(int x, int y) {
44     int ret = 0;
45     for (int i = x ; i > 0 ; i -= lowbit(i))
46         for (int j = y ; j > 0 ; j -= lowbit(j))
47             ret += c[i][j];
48     return ret;
49 }
50 int main() {
51     scanf("%d%d", &k, &n);
52     while(1) {
53         scanf("%d", &k);
54         if (k == 1) {
55             int x, y, z;
56             sfff(x, y, z);
57             x++, y++;
58             updata(x, y, z);
59         }
60         if (k == 2) {
61             int x1,y1,x2,y2;
62             scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
63             x1++, y1++, x2++, y2++;
64             int ans = sum(x2, y2) - sum(x1 - 1, y2) - sum(x2, y1 - 1) + sum(x1 - 1, y1 - 1);
65             printf("%d\n", ans);
66         }
67         if (k == 3) break;
68     }
69     return 0;
70 }

 



转载于:https://www.cnblogs.com/qldabiaoge/p/9415313.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值